mirror of
https://github.com/httpie/cli.git
synced 2025-03-13 14:28:50 +01:00
parent
e984c574a0
commit
7af719e0c5
@ -1,8 +1,7 @@
|
|||||||
|
from __future__ import annotations
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import re
|
import re
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class HttpFileRequest:
|
class HttpFileRequest:
|
||||||
@ -16,9 +15,18 @@ class HttpFileRequest:
|
|||||||
|
|
||||||
def http_parser(filename: str) -> list[HttpFileRequest]:
|
def http_parser(filename: str) -> list[HttpFileRequest]:
|
||||||
|
|
||||||
def split_requests(http_file_contents:str) -> list[str]:
|
def split_requests(http_file_contents: str) -> list[str]:
|
||||||
"""makes a dictionnary from the raw http file that breaks it down into individual requests and returns a dictionary of their names """
|
"""Splits an HTTP file into individual requests but keeps the '###' in each request."""
|
||||||
return re.split(r"^###", http_file_contents, re.MULTILINE)
|
parts = re.split(r"(^###.*)", http_file_contents, flags=re.MULTILINE)
|
||||||
|
requests = []
|
||||||
|
|
||||||
|
for i in range(1, len(parts), 2):
|
||||||
|
header = parts[i].strip()
|
||||||
|
body = parts[i + 1].strip() if i + 1 < len(parts) else ""
|
||||||
|
requests.append(f"{header}\n{body}")
|
||||||
|
|
||||||
|
return requests
|
||||||
|
|
||||||
|
|
||||||
def get_dependencies(raw_http_request:str, poss_names: list[str]) -> list[str] | None:
|
def get_dependencies(raw_http_request:str, poss_names: list[str]) -> list[str] | None:
|
||||||
"""returns a list of all the names of the requests that must be fufilled before this one can be sent"""
|
"""returns a list of all the names of the requests that must be fufilled before this one can be sent"""
|
||||||
@ -63,7 +71,17 @@ def http_parser(filename: str) -> list[HttpFileRequest]:
|
|||||||
Returns:
|
Returns:
|
||||||
dict: containing the parsed headers
|
dict: containing the parsed headers
|
||||||
'''
|
'''
|
||||||
return {}
|
headers = {}
|
||||||
|
|
||||||
|
for line in raw_text:
|
||||||
|
if not line.strip() or ':' not in line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
header_name, header_value = line.split(':', 1)
|
||||||
|
|
||||||
|
headers[header_name.strip()] = header_value.strip()
|
||||||
|
|
||||||
|
return headers
|
||||||
|
|
||||||
def parse_body(raw_text: str) -> bytes :
|
def parse_body(raw_text: str) -> bytes :
|
||||||
'''
|
'''
|
||||||
@ -97,6 +115,8 @@ def http_parser(filename: str) -> list[HttpFileRequest]:
|
|||||||
url=url,
|
url=url,
|
||||||
headers=extract_headers(raw_headers),
|
headers=extract_headers(raw_headers),
|
||||||
body=parse_body("\n".join(raw_body)),
|
body=parse_body("\n".join(raw_body)),
|
||||||
|
dependencies={},
|
||||||
|
name=get_name(raw_text)
|
||||||
)
|
)
|
||||||
|
|
||||||
http_file = Path(filename)
|
http_file = Path(filename)
|
||||||
@ -106,10 +126,17 @@ def http_parser(filename: str) -> list[HttpFileRequest]:
|
|||||||
raise IsADirectoryError(f"Path is not a file: {filename}")
|
raise IsADirectoryError(f"Path is not a file: {filename}")
|
||||||
http_contents = http_file.read_text()
|
http_contents = http_file.read_text()
|
||||||
|
|
||||||
raw_requests = http_contents.split("###")
|
raw_requests = split_requests(replace_global(http_contents))
|
||||||
|
raw_requests = [req.strip() for req in raw_requests if req.strip()]
|
||||||
parsed_requests = []
|
parsed_requests = []
|
||||||
|
req_names = []
|
||||||
|
|
||||||
for raw_req in raw_requests:
|
for raw_req in raw_requests:
|
||||||
parsed_requests.append(parse_single_request(raw_req))
|
new_req = parse_single_request(raw_req)
|
||||||
|
new_req.dependencies = get_dependencies(raw_req,req_names)
|
||||||
|
if(new_req.name != None):
|
||||||
|
req_names.append(new_req.name)
|
||||||
|
|
||||||
|
parsed_requests.append(new_req)
|
||||||
|
|
||||||
return parsed_requests
|
return parsed_requests
|
||||||
|
Loading…
Reference in New Issue
Block a user