Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of httpx.AsyncClient.build_request #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions example/client/api/pet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ def _build_for_delete_pet(self, pet_id: int, api_key: str = None) -> Awaitable[N
headers["api_key"] = str(api_key)

return self.api_client.request(
type_=None,
method="DELETE",
url="/pet/{petId}",
path_params=path_params,
headers=headers,
type_=None, method="DELETE", url="/pet/{petId}", path_params=path_params, headers=headers,
)

def _build_for_find_pets_by_status(self, status: List[str]) -> Awaitable[List[m.Pet]]:
Expand All @@ -40,38 +36,23 @@ def _build_for_find_pets_by_status(self, status: List[str]) -> Awaitable[List[m.
"""
query_params = {"status": str(status)}

return self.api_client.request(
type_=List[m.Pet],
method="GET",
url="/pet/findByStatus",
params=query_params,
)
return self.api_client.request(type_=List[m.Pet], method="GET", url="/pet/findByStatus", params=query_params,)

def _build_for_find_pets_by_tags(self, tags: List[str]) -> Awaitable[List[m.Pet]]:
"""
Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
"""
query_params = {"tags": str(tags)}

return self.api_client.request(
type_=List[m.Pet],
method="GET",
url="/pet/findByTags",
params=query_params,
)
return self.api_client.request(type_=List[m.Pet], method="GET", url="/pet/findByTags", params=query_params,)

def _build_for_get_pet_by_id(self, pet_id: int) -> Awaitable[m.Pet]:
"""
Returns a single pet
"""
path_params = {"petId": str(pet_id)}

return self.api_client.request(
type_=m.Pet,
method="GET",
url="/pet/{petId}",
path_params=path_params,
)
return self.api_client.request(type_=m.Pet, method="GET", url="/pet/{petId}", path_params=path_params,)

def _build_for_update_pet(self, body: m.Pet) -> Awaitable[None]:
body = jsonable_encoder(body)
Expand Down
28 changes: 6 additions & 22 deletions example/client/api/store_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,14 @@ def _build_for_delete_order(self, order_id: int) -> Awaitable[None]:
path_params = {"orderId": str(order_id)}

return self.api_client.request(
type_=None,
method="DELETE",
url="/store/order/{orderId}",
path_params=path_params,
type_=None, method="DELETE", url="/store/order/{orderId}", path_params=path_params,
)

def _build_for_get_inventory(
self,
) -> Awaitable[Dict[str, int]]:
def _build_for_get_inventory(self,) -> Awaitable[Dict[str, int]]:
"""
Returns a map of status codes to quantities
"""
return self.api_client.request(
type_=Dict[str, int],
method="GET",
url="/store/inventory",
)
return self.api_client.request(type_=Dict[str, int], method="GET", url="/store/inventory",)

def _build_for_get_order_by_id(self, order_id: int) -> Awaitable[m.Order]:
"""
Expand All @@ -46,10 +37,7 @@ def _build_for_get_order_by_id(self, order_id: int) -> Awaitable[m.Order]:
path_params = {"orderId": str(order_id)}

return self.api_client.request(
type_=m.Order,
method="GET",
url="/store/order/{orderId}",
path_params=path_params,
type_=m.Order, method="GET", url="/store/order/{orderId}", path_params=path_params,
)

def _build_for_place_order(self, body: m.Order) -> Awaitable[m.Order]:
Expand All @@ -65,9 +53,7 @@ async def delete_order(self, order_id: int) -> None:
"""
return await self._build_for_delete_order(order_id=order_id)

async def get_inventory(
self,
) -> Dict[str, int]:
async def get_inventory(self,) -> Dict[str, int]:
"""
Returns a map of status codes to quantities
"""
Expand All @@ -91,9 +77,7 @@ def delete_order(self, order_id: int) -> None:
coroutine = self._build_for_delete_order(order_id=order_id)
return get_event_loop().run_until_complete(coroutine)

def get_inventory(
self,
) -> Dict[str, int]:
def get_inventory(self,) -> Dict[str, int]:
"""
Returns a map of status codes to quantities
"""
Expand Down
39 changes: 7 additions & 32 deletions example/client/api/user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,20 @@ def _build_for_delete_user(self, username: str) -> Awaitable[None]:
"""
path_params = {"username": str(username)}

return self.api_client.request(
type_=None,
method="DELETE",
url="/user/{username}",
path_params=path_params,
)
return self.api_client.request(type_=None, method="DELETE", url="/user/{username}", path_params=path_params,)

def _build_for_get_user_by_name(self, username: str) -> Awaitable[m.User]:
path_params = {"username": str(username)}

return self.api_client.request(
type_=m.User,
method="GET",
url="/user/{username}",
path_params=path_params,
)
return self.api_client.request(type_=m.User, method="GET", url="/user/{username}", path_params=path_params,)

def _build_for_login_user(self, username: str, password: str) -> Awaitable[str]:
query_params = {"username": str(username), "password": str(password)}

return self.api_client.request(
type_=str,
method="GET",
url="/user/login",
params=query_params,
)
return self.api_client.request(type_=str, method="GET", url="/user/login", params=query_params,)

def _build_for_logout_user(
self,
) -> Awaitable[None]:
return self.api_client.request(
type_=None,
method="GET",
url="/user/logout",
)
def _build_for_logout_user(self,) -> Awaitable[None]:
return self.api_client.request(type_=None, method="GET", url="/user/logout",)

def _build_for_update_user(self, username: str, body: m.User) -> Awaitable[None]:
"""
Expand Down Expand Up @@ -112,9 +91,7 @@ async def get_user_by_name(self, username: str) -> m.User:
async def login_user(self, username: str, password: str) -> str:
return await self._build_for_login_user(username=username, password=password)

async def logout_user(
self,
) -> None:
async def logout_user(self,) -> None:
return await self._build_for_logout_user()

async def update_user(self, username: str, body: m.User) -> None:
Expand Down Expand Up @@ -155,9 +132,7 @@ def login_user(self, username: str, password: str) -> str:
coroutine = self._build_for_login_user(username=username, password=password)
return get_event_loop().run_until_complete(coroutine)

def logout_user(
self,
) -> None:
def logout_user(self,) -> None:
coroutine = self._build_for_logout_user()
return get_event_loop().run_until_complete(coroutine)

Expand Down
2 changes: 1 addition & 1 deletion example/client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def request( # noqa F811
if path_params is None:
path_params = {}
url = (self.host or "") + url.format(**path_params)
request = Request(method, url, **kwargs)
request = self._async_client.build_request(method, url, **kwargs)
return await self.send(request, type_)

@overload
Expand Down
2 changes: 1 addition & 1 deletion openapi-python-templates/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ApiClient:
if path_params is None:
path_params = {}
url = (self.host or "") + url.format(**path_params)
request = Request(method, url, **kwargs)
request = self._async_client.build_request(method, url, **kwargs)
return await self.send(request, type_)

@overload
Expand Down
1 change: 1 addition & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict

from fastapi.openapi.models import OAuthFlowPassword

from generated_client.api_client import ApiClient
from generated_client.auth import AuthMiddleware, AuthState

Expand Down
3 changes: 2 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from asyncio import get_event_loop
from typing import Tuple, Type

from mypy.ipc import TracebackType

import generated_client.models as models
from generated_client.api_client import ApiClient, SyncApis
from mypy.ipc import TracebackType


class Client(SyncApis):
Expand Down