From 1752d651a9e1883fc262dd1fd3b9cc53bd63d20a Mon Sep 17 00:00:00 2001 From: stijndcl Date: Mon, 29 Aug 2022 02:26:16 +0200 Subject: [PATCH] Undo last change --- didier/data/apis/hydra.py | 1 - didier/utils/http/requests.py | 16 ++++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/didier/data/apis/hydra.py b/didier/data/apis/hydra.py index 63a45ac..620e0df 100644 --- a/didier/data/apis/hydra.py +++ b/didier/data/apis/hydra.py @@ -12,5 +12,4 @@ async def fetch_menu(http_session: ClientSession, day_dt: date) -> Menu: """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" async with ensure_get(http_session, endpoint, log_exceptions=False) as response: - print(response) return Menu.parse_obj(response) diff --git a/didier/utils/http/requests.py b/didier/utils/http/requests.py index 45eb38b..ffcb498 100644 --- a/didier/utils/http/requests.py +++ b/didier/utils/http/requests.py @@ -1,6 +1,6 @@ import logging from contextlib import asynccontextmanager -from typing import AsyncGenerator, Type, TypeVar +from typing import AsyncGenerator from aiohttp import ClientResponse, ClientSession, ContentTypeError @@ -12,9 +12,6 @@ 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 @@ -22,12 +19,12 @@ def request_successful(response: ClientResponse) -> bool: @asynccontextmanager async def ensure_get( - http_session: ClientSession, endpoint: str, *, return_type: Type[T] = dict, log_exceptions: bool = True -) -> AsyncGenerator[T, None]: + http_session: ClientSession, endpoint: str, *, log_exceptions: bool = True +) -> AsyncGenerator[dict, 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()) if return_type == dict else (await response.text()) + content = await response.json() except ContentTypeError: content = await response.text() @@ -46,15 +43,14 @@ async def ensure_post( endpoint: str, payload: dict, *, - return_type: Type[T] = dict, log_exceptions: bool = True, expect_return: bool = True -) -> AsyncGenerator[T, None]: +) -> AsyncGenerator[dict, 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()) if return_type == dict else (await response.text()) + content = await response.json() except ContentTypeError: content = await response.text()