Undo last change

pull/128/head
stijndcl 2022-08-29 02:26:16 +02:00
parent 994ff01de1
commit 1752d651a9
2 changed files with 6 additions and 11 deletions

View File

@ -12,5 +12,4 @@ async def fetch_menu(http_session: ClientSession, day_dt: date) -> Menu:
"""Fetch the menu for a given day""" """Fetch the menu for a given day"""
endpoint = f"https://hydra.ugent.be/api/2.0/resto/menu/nl/{day_dt.year}/{day_dt.month}/{day_dt.day}.json" endpoint = f"https://hydra.ugent.be/api/2.0/resto/menu/nl/{day_dt.year}/{day_dt.month}/{day_dt.day}.json"
async with ensure_get(http_session, endpoint, log_exceptions=False) as response: async with ensure_get(http_session, endpoint, log_exceptions=False) as response:
print(response)
return Menu.parse_obj(response) return Menu.parse_obj(response)

View File

@ -1,6 +1,6 @@
import logging import logging
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import AsyncGenerator, Type, TypeVar from typing import AsyncGenerator
from aiohttp import ClientResponse, ClientSession, ContentTypeError from aiohttp import ClientResponse, ClientSession, ContentTypeError
@ -12,9 +12,6 @@ logger = logging.getLogger(__name__)
__all__ = ["ensure_get", "ensure_post"] __all__ = ["ensure_get", "ensure_post"]
T = TypeVar("T", str, dict)
def request_successful(response: ClientResponse) -> bool: def request_successful(response: ClientResponse) -> bool:
"""Check if a request was successful or not""" """Check if a request was successful or not"""
return 200 <= response.status < 300 return 200 <= response.status < 300
@ -22,12 +19,12 @@ def request_successful(response: ClientResponse) -> bool:
@asynccontextmanager @asynccontextmanager
async def ensure_get( async def ensure_get(
http_session: ClientSession, endpoint: str, *, return_type: Type[T] = dict, log_exceptions: bool = True http_session: ClientSession, endpoint: str, *, log_exceptions: bool = True
) -> AsyncGenerator[T, None]: ) -> AsyncGenerator[dict, None]:
"""Context manager that automatically raises an exception if a GET-request fails""" """Context manager that automatically raises an exception if a GET-request fails"""
async with http_session.get(endpoint) as response: async with http_session.get(endpoint) as response:
try: try:
content = (await response.json()) if return_type == dict else (await response.text()) content = await response.json()
except ContentTypeError: except ContentTypeError:
content = await response.text() content = await response.text()
@ -46,15 +43,14 @@ async def ensure_post(
endpoint: str, endpoint: str,
payload: dict, payload: dict,
*, *,
return_type: Type[T] = dict,
log_exceptions: bool = True, log_exceptions: bool = True,
expect_return: bool = True expect_return: bool = True
) -> AsyncGenerator[T, None]: ) -> AsyncGenerator[dict, None]:
"""Context manager that automatically raises an exception if a POST-request fails""" """Context manager that automatically raises an exception if a POST-request fails"""
async with http_session.post(endpoint, data=payload) as response: async with http_session.post(endpoint, data=payload) as response:
if not request_successful(response): if not request_successful(response):
try: try:
content = (await response.json()) if return_type == dict else (await response.text()) content = await response.json()
except ContentTypeError: except ContentTypeError:
content = await response.text() content = await response.text()