Initial commit: Add curl-to-code-cli project
This commit is contained in:
69
.curl_to_code/parser/models.py
Normal file
69
.curl_to_code/parser/models.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from enum import Enum
|
||||
from typing import Optional, List, Dict, Any
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class RequestMethod(str, Enum):
|
||||
GET = "GET"
|
||||
POST = "POST"
|
||||
PUT = "PUT"
|
||||
PATCH = "PATCH"
|
||||
DELETE = "DELETE"
|
||||
HEAD = "HEAD"
|
||||
OPTIONS = "OPTIONS"
|
||||
|
||||
|
||||
class Header(BaseModel):
|
||||
key: str
|
||||
value: str
|
||||
|
||||
def to_tuple(self) -> tuple[str, str]:
|
||||
return (self.key, self.value)
|
||||
|
||||
|
||||
class AuthType(str, Enum):
|
||||
BASIC = "basic"
|
||||
BEARER = "bearer"
|
||||
DIGEST = "digest"
|
||||
|
||||
|
||||
class Auth(BaseModel):
|
||||
auth_type: AuthType = AuthType.BASIC
|
||||
username: Optional[str] = None
|
||||
password: Optional[str] = None
|
||||
token: Optional[str] = None
|
||||
|
||||
def get_header(self) -> Optional[Header]:
|
||||
if self.auth_type == AuthType.BASIC and self.username and self.password:
|
||||
import base64
|
||||
credentials = f"{self.username}:{self.password}"
|
||||
encoded = base64.b64encode(credentials.encode()).decode()
|
||||
return Header(key="Authorization", value=f"Basic {encoded}")
|
||||
elif self.auth_type == AuthType.BEARER and self.token:
|
||||
return Header(key="Authorization", value=f"Bearer {self.token}")
|
||||
return None
|
||||
|
||||
|
||||
class DataType(str, Enum):
|
||||
RAW = "raw"
|
||||
JSON = "json"
|
||||
FORM = "form"
|
||||
FILE = "file"
|
||||
|
||||
|
||||
class RequestData(BaseModel):
|
||||
data_type: DataType = DataType.RAW
|
||||
content: str = ""
|
||||
json_data: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
class ParsedCurl(BaseModel):
|
||||
url: str
|
||||
method: RequestMethod = RequestMethod.GET
|
||||
headers: List[Header] = Field(default_factory=list)
|
||||
auth: Optional[Auth] = None
|
||||
data: Optional[RequestData] = None
|
||||
cookies: Dict[str, str] = Field(default_factory=dict)
|
||||
user_agent: Optional[str] = None
|
||||
follow_redirects: bool = True
|
||||
timeout: Optional[int] = None
|
||||
Reference in New Issue
Block a user