[dev] implements parse_single_request (#1,#3,#6)

split the .http file into blocks
gets method and URL from requests
cleans comments
fix API's
This commit is contained in:
Coli Alessandro 2025-02-27 16:11:12 +01:00
parent 4c7513dbc0
commit 3b640e537a

View File

@ -12,7 +12,7 @@ class HttpFileRequest:
def http_parser(filename: str) -> list[HttpFileRequest]:
def extract_headers(raw_text: str) -> dict :
def extract_headers(raw_text: list[str]) -> dict :
'''
Extract the headers of the .http file
@ -22,22 +22,40 @@ def http_parser(filename: str) -> list[HttpFileRequest]:
Returns:
dict: containing the parsed headers
'''
return None
return {}
def parse_body(raw_text: str) -> dict :
def parse_body(raw_text: str) -> bytes :
'''
parse the body of the .http file
'''
return None
return b""
def parse_single_request(raw_text: str) -> HttpFileRequest:
'''Parse a single request from .http file format to HttpFileRequest '''
lines = raw_text.strip().splitlines()
lines = [line.strip() for line in lines if not line.strip().startswith("#")]
method, url = lines[0].split(" ")
raw_headers = []
raw_body = []
is_body = False
for line in lines[1:]:
if not line.strip():
is_body = True
continue
if not is_body:
raw_headers.append(line)
else:
raw_body.append(line)
return HttpFileRequest(
method=method,
url=url,
headers={},
body=b"",
headers=extract_headers(raw_headers),
body=parse_body("\n".join(raw_body)),
)
http_file = Path(filename)
@ -46,18 +64,11 @@ def http_parser(filename: str) -> list[HttpFileRequest]:
if not http_file.is_file():
raise IsADirectoryError(f"Path is not a file: {filename}")
http_contents = http_file.read_text()
http_lines = [
line for line in http_contents.splitlines() if not line.startswith("#")
]
http_lines = [line for line in http_lines if line.strip()]
first_line = http_lines[0]
method, url = first_line.split(" ")
raw_requests = http_contents.split("###")
parsed_requests = []
for raw_req in raw_requests:
parsed_requests.append(parse_single_request(raw_req))
return [
HttpFileRequest(
method=method,
url=url,
headers={},
body=b"",
)
]
return parsed_requests