Skip to content

Commit

Permalink
chore: update async client
Browse files Browse the repository at this point in the history
  • Loading branch information
ghadeer-x committed Mar 4, 2024
1 parent e4ee06d commit 8e92d6f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/openapi_python_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@click.argument("output")
@click.option(
"--library",
default=HTTPLibrary.httpx,
default=HTTPLibrary.aiohttp,
type=HTTPLibrary,
show_default=True,
help="HTTP library to use in the generation of the client.",
Expand All @@ -36,7 +36,7 @@
def main(
source: str,
output: str,
library: Optional[HTTPLibrary] = HTTPLibrary.httpx,
library: Optional[HTTPLibrary] = HTTPLibrary.aiohttp,
env_token_name: Optional[str] = None,
use_orjson: bool = False,
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@
}

query_params = {key:value for (key,value) in query_params.items() if value is not None}

filtered_headers = {key:value for (key,value) in headers.items() if value is not None}
timeout = aiohttp.ClientTimeout(total=3*60, connect=5, sock_connect=5, sock_read=5)
async with aiohttp.ClientSession(headers=headers, timeout=timeout) as session:

{% if async_client %}
async with aiohttp.ClientSession(headers=headers, timeout=timeout) as session:
async with session.request(
{% else %}
with httpx.Client(base_url=base_path, verify=api_config.verify, timeout=api_config.timeout) as client:
response = client.request(
{% endif %}
'{{ method }}',
base_path + path,
params=query_params,
Expand All @@ -36,24 +44,40 @@
json = {{ body_param }}
{% endif %}
{% endif %}
) as inital_response:
)
as inital_response:
if inital_response.status != {{ return_type.status_code }}:
raise HTTPException(response=inital_response)

{% if return_type.type is none or return_type.type.converted_type is none %}
return None
{% elif return_type.type is not none and return_type.type.converted_type == "str" %}
{% elif return_type.type is not none and return_type.type.converted_type == "str" and async_client %}
response = await inital_response.text()
return response
{% elif return_type.complex_type %}
{% elif return_type.type is not none and return_type.type.converted_type == "str" and not async_client %}
return inital_response.text

{% elif return_type.complex_type and async_client %}
{%- if return_type.list_type is none %}
response = await inital_response.json()
return {{ return_type.type.converted_type }}(**response) if response is not None else {{ return_type.type.converted_type }}()
{%- else %}
response = await inital_response.json()
return [{{ return_type.list_type }}(**item) for item in response]
{%- endif %}
{% else %}

{% elif return_type.complex_type and not async_client%}
{%- if return_type.list_type is none %}
response = inital_response.json()
return {{ return_type.type.converted_type }}(**response) if response is not None else {{ return_type.type.converted_type }}()
{%- else %}
response = inital_response.json()
return [{{ return_type.list_type }}(**item) for item in response]
{%- endif %}

{% elif async_client%}
response = await inital_response.json()
return res
return response
{% else %}
return inital_response.json()
{% endif %}

0 comments on commit 8e92d6f

Please sign in to comment.