Skip to content

Commit 45462da

Browse files
Merge pull request #10 from MarcoMuellner/6-aiohttp-is-not-implementedsupported
6 aiohttp is not implementedsupported
2 parents a8c0ff9 + 48501f3 commit 45462da

File tree

8 files changed

+493
-35
lines changed

8 files changed

+493
-35
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ __Documentation:__ [here][documentation]
3535
- __Support for multiple rest frameworks.__ __OpenAPI python generator__ currently supports the following:
3636
- [httpx](https://pypi.org/project/httpx/)
3737
- [requests](https://pypi.org/project/requests/)
38+
- [aiohttp](https://pypi.org/project/aiohttp/)
3839
- __Async and sync code generation support__, depending on the framework. It will automatically create both for frameworks that support both.
3940
- __Easily extendable using Jinja2 templates__. The code is designed to be easily extendable and should support even more languages and frameworks in the future.
4041
- __Fully tested__. Every generated code is automatically tested against the OpenAPI spec and we have 100% coverage.

docs/tutorial/index.md

+39-25
Original file line numberDiff line numberDiff line change
@@ -647,48 +647,62 @@ take a look at the `User.py` and the `Team.py` files:
647647

648648
``` py
649649
from typing import *
650+
651+
from pydantic import BaseModel, Field
652+
653+
654+
class User(BaseModel):
655+
"""
656+
User model
650657

651-
from pydantic import BaseModel
658+
"""
652659

660+
id: int = Field(alias="id")
653661

654-
class User(BaseModel):
655-
"""
656-
User model
662+
username: str = Field(alias="username")
657663

658-
"""
664+
email: str = Field(alias="email")
665+
666+
password: str = Field(alias="password")
667+
668+
is_active: Optional[bool] = Field(alias="is_active", default=None)
669+
670+
created_at: Optional[str] = Field(alias="created_at", default=None)
659671

660-
id: int
661-
username: str
662-
email: str
663-
password: str
664-
is_active: Optional[bool] = None
665-
created_at: Optional[str] = None
666672
```
667673

668674
=== "Team.py"
669675

670676
``` py
671677
from typing import *
672-
673-
from pydantic import BaseModel
674-
678+
679+
from pydantic import BaseModel, Field
680+
675681
from .User import User
676682

677683

678684
class Team(BaseModel):
679-
"""
680-
Team model
685+
"""
686+
Team model
681687

682-
"""
688+
"""
689+
690+
id: int = Field(alias="id")
691+
692+
name: str = Field(alias="name")
693+
694+
description: str = Field(alias="description")
695+
696+
is_active: Optional[bool] = Field(alias="is_active", default=None)
697+
698+
created_at: Optional[str] = Field(alias="created_at", default=None)
699+
700+
updated_at: Optional[str] = Field(alias="updated_at", default=None)
701+
702+
users: Optional[List[User]] = Field(alias="users", default=None)
703+
704+
captain: Optional[User] = Field(alias="captain", default=None)
683705

684-
id: int
685-
name: str
686-
description: str
687-
is_active: Optional[bool] = None
688-
created_at: Optional[str] = None
689-
updated_at: Optional[str] = None
690-
users: Optional[List[User]] = None
691-
captain: Optional[User] = None
692706

693707
```
694708

poetry.lock

+411-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "openapi-python-generator"
3-
version = "0.3.10"
3+
version = "0.4.0"
44
description = "Openapi Python Generator"
55
authors = ["Marco Müllner <[email protected]>"]
66
license = "MIT"
@@ -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"]
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

+1-4
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

+2
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

+1-4
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)