Skip to content

Commit 6985a23

Browse files
committed
:feat: Implemented aiohttp
1 parent a8c0ff9 commit 6985a23

File tree

6 files changed

+452
-9
lines changed

6 files changed

+452
-9
lines changed

poetry.lock

Lines changed: 411 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ pytest-cov = "^3.0.0"
5151
fastapi = "^0.78.0"
5252
uvicorn = "^0.18.1"
5353
respx = "^0.20.1"
54+
aiohttp = "^3.8.3"
5455

5556
[tool.poetry.scripts]
5657
openapi-python-generator = "openapi_python_generator.__main__:main"
5758

59+
5860
[tool.coverage.paths]
5961
source = ["src", "*/site-packages"]
6062
tests = ["tests", "*/tests"]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
async def {{ operation_id }}({{ params }}) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
2+
base_path = APIConfig.base_path
3+
path = f'{{ path_name }}'
4+
headers = {
5+
'Content-Type': 'application/json',
6+
'Accept': 'application/json',
7+
'Authorization': f'Bearer { APIConfig.get_access_token() }',
8+
}
9+
query_params : Dict[str,Any] = {
10+
{% if query_params|length > 0 %}
11+
{{ query_params | join(',\n') | safe }}
12+
{% endif %}
13+
}
14+
15+
query_params = {key:value for (key,value) in query_params.items() if value is not None}
16+
17+
async with aiohttp.ClientSession(headers=headers) as session:
18+
async with session.request(
19+
'{{ method }}',
20+
base_path + path,
21+
params=query_params,
22+
) as inital_response:
23+
if inital_response.status != {{ return_type.status_code }}:
24+
raise HTTPException(inital_response.status, f'{{ operationId }} failed with status code: {inital_response.status}')
25+
response = await inital_response.json()
26+
27+
{% if return_type.complex_type %}
28+
{% if return_type.list_type is none %}
29+
return {{ return_type.type.converted_type }}(**response.json()) if response.json() is not None else {{ return_type.type.converted_type }}()
30+
{% else %}
31+
return [{{ return_type.list_type }}(**item) for item in response.json()]
32+
{% endif %}
33+
{% else %}
34+
return response.json()
35+
{% endif %}

tests/regression/test_issue_7.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ def runner() -> CliRunner:
1515

1616
@pytest.mark.parametrize(
1717
"library",
18-
[
19-
HTTPLibrary.httpx,
20-
HTTPLibrary.requests,
21-
],
18+
[HTTPLibrary.httpx, HTTPLibrary.requests, HTTPLibrary.aiohttp],
2219
)
2320
def test_issue_7(runner: CliRunner, model_data_with_cleanup, library) -> None:
2421
"""

tests/test_generated_code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def test_get_auth_token_without_env(model_data_with_cleanup):
3535
(HTTPLibrary.requests, False),
3636
(HTTPLibrary.httpx, True),
3737
(HTTPLibrary.requests, True),
38+
(HTTPLibrary.aiohttp, True),
39+
(HTTPLibrary.aiohttp, False),
3840
],
3941
)
4042
@respx.mock

tests/test_main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ def runner() -> CliRunner:
1616

1717
@pytest.mark.parametrize(
1818
"library",
19-
[
20-
HTTPLibrary.httpx,
21-
HTTPLibrary.requests,
22-
],
19+
[HTTPLibrary.httpx, HTTPLibrary.requests, HTTPLibrary.aiohttp],
2320
)
2421
def test_main_succeeds(runner: CliRunner, model_data_with_cleanup, library) -> None:
2522
"""It exits with a status code of zero."""

0 commit comments

Comments
 (0)