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