[#333] Fix files param in http client part two

Signed-off-by: Kirill Sosnovskikh <k.sosnovskikh@yadro.com>
This commit is contained in:
k.sosnovskikh 2024-12-05 16:34:36 +03:00
parent 61353cb38c
commit ee7d9df4a9

View file

@ -84,12 +84,12 @@ class HttpClient:
return request_body
@classmethod
def _parse_files(cls, files: Mapping | Sequence | None) -> str | None:
if not files:
return None
def _parse_files(cls, files: Mapping | Sequence | None) -> dict:
filepaths = {}
if not files:
return filepaths
if isinstance(files, Sequence):
items = files
elif isinstance(files, Mapping):
@ -100,11 +100,10 @@ class HttpClient:
for name, file in items:
if isinstance(file, io.IOBase):
filepaths[name] = file.name
if isinstance(file, tuple):
elif isinstance(file, Sequence):
filepaths[name] = file[1].name
return json.dumps(filepaths, default=str, indent=4)
return filepaths
@classmethod
def _attach_response(cls, response: httpx.Response, **kwargs):
@ -128,23 +127,18 @@ class HttpClient:
+ f"Response Headers: {response_headers}\n\n"
+ (f"Response Body: {response_body}\n\n" if response_body else "")
)
curl_request = cls._create_curl_request(request.url, request.method, request.headers, request_body, files)
curl_request = cls._create_curl_request(request.url, request.method, request.headers, request_body, request_files)
reporter.attach(report, "Requests Info")
reporter.attach(curl_request, "CURL")
@classmethod
def _create_curl_request(cls, url: str, method: str, headers: httpx.Headers, data: str, files: dict = None) -> str:
def _create_curl_request(cls, url: str, method: str, headers: httpx.Headers, data: str, files: dict) -> str:
headers = " ".join(f'-H "{name.title()}: {value}"' for name, value in headers.items())
data = f" -d '{data}'" if data else ""
if files:
for name, file in files.items():
if isinstance(file, io.IOBase):
data += f' -F "{name}=@{file.name}"'
if isinstance(file, tuple):
data += f' -F "{name}=@{file[1].name}"'
for name, path in files.items():
data += f' -F "{name}=@{path}"'
# Option -k means no verify SSL
return f"curl {url} -X {method} {headers}{data} -k"