mirror of https://github.com/stijndcl/didier
Allow specifying return type for http methods
parent
14e0472954
commit
994ff01de1
|
@ -1,6 +1,6 @@
|
|||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncGenerator
|
||||
from typing import AsyncGenerator, Type, TypeVar
|
||||
|
||||
from aiohttp import ClientResponse, ClientSession, ContentTypeError
|
||||
|
||||
|
@ -12,6 +12,9 @@ logger = logging.getLogger(__name__)
|
|||
__all__ = ["ensure_get", "ensure_post"]
|
||||
|
||||
|
||||
T = TypeVar("T", str, dict)
|
||||
|
||||
|
||||
def request_successful(response: ClientResponse) -> bool:
|
||||
"""Check if a request was successful or not"""
|
||||
return 200 <= response.status < 300
|
||||
|
@ -19,12 +22,12 @@ def request_successful(response: ClientResponse) -> bool:
|
|||
|
||||
@asynccontextmanager
|
||||
async def ensure_get(
|
||||
http_session: ClientSession, endpoint: str, *, log_exceptions: bool = True
|
||||
) -> AsyncGenerator[dict, None]:
|
||||
http_session: ClientSession, endpoint: str, *, return_type: Type[T] = dict, log_exceptions: bool = True
|
||||
) -> AsyncGenerator[T, None]:
|
||||
"""Context manager that automatically raises an exception if a GET-request fails"""
|
||||
async with http_session.get(endpoint) as response:
|
||||
try:
|
||||
content = await response.json()
|
||||
content = (await response.json()) if return_type == dict else (await response.text())
|
||||
except ContentTypeError:
|
||||
content = await response.text()
|
||||
|
||||
|
@ -43,14 +46,15 @@ async def ensure_post(
|
|||
endpoint: str,
|
||||
payload: dict,
|
||||
*,
|
||||
return_type: Type[T] = dict,
|
||||
log_exceptions: bool = True,
|
||||
expect_return: bool = True
|
||||
) -> AsyncGenerator[dict, None]:
|
||||
) -> AsyncGenerator[T, None]:
|
||||
"""Context manager that automatically raises an exception if a POST-request fails"""
|
||||
async with http_session.post(endpoint, data=payload) as response:
|
||||
if not request_successful(response):
|
||||
try:
|
||||
content = await response.json()
|
||||
content = (await response.json()) if return_type == dict else (await response.text())
|
||||
except ContentTypeError:
|
||||
content = await response.text()
|
||||
|
||||
|
|
Loading…
Reference in New Issue