From bd23036414886285d877a5d5186e978013fc3186 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Sat, 16 Apr 2022 11:54:14 +0200 Subject: [PATCH] :rotating_light: Merged with-statements --- classquiz/kahoot_importer/get.py | 17 ++++++------ classquiz/kahoot_importer/search.py | 9 +++--- classquiz/routers/quiz.py | 2 -- classquiz/routers/utils.py | 13 ++++----- classquiz/storage/deta_storage.py | 43 +++++++++++++++-------------- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/classquiz/kahoot_importer/get.py b/classquiz/kahoot_importer/get.py index 6d58226..05ad663 100644 --- a/classquiz/kahoot_importer/get.py +++ b/classquiz/kahoot_importer/get.py @@ -10,11 +10,12 @@ class _Response(BaseModel): async def get(game_id: str) -> _Response | None: - async with ClientSession() as session: - async with session.get(f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true") as response: - if response.status == 200: - return _Response(**await response.json()) - elif response.status == 404: - return None - else: - raise Exception(f"Unexpected response status: {response.status}") + async with ClientSession() as session, session.get( + f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true" + ) as response: + if response.status == 200: + return _Response(**await response.json()) + elif response.status == 404: + return None + else: + raise Exception(f"Unexpected response status: {response.status}") diff --git a/classquiz/kahoot_importer/search.py b/classquiz/kahoot_importer/search.py index ed83426..a2807df 100644 --- a/classquiz/kahoot_importer/search.py +++ b/classquiz/kahoot_importer/search.py @@ -30,8 +30,7 @@ async def search( :param limit: Less or equals 100 :return: """ - async with ClientSession() as session: - async with session.get( - f"https://create.kahoot.it/rest/kahoots/?query={query}&limit={limit}&cursor={cursor}&searchCluster={search_cluster}&includeExtendedCounters=false&inventoryItemId={inventory_item_id}" # noqa : E501 - ) as response: - return _Response(**await response.json()) + async with ClientSession() as session, session.get( + f"https://create.kahoot.it/rest/kahoots/?query={query}&limit={limit}&cursor={cursor}&searchCluster={search_cluster}&includeExtendedCounters=false&inventoryItemId={inventory_item_id}" # noqa : E501 + ) as response: + return _Response(**await response.json()) diff --git a/classquiz/routers/quiz.py b/classquiz/routers/quiz.py index 4964e8d..cc174fd 100644 --- a/classquiz/routers/quiz.py +++ b/classquiz/routers/quiz.py @@ -132,8 +132,6 @@ async def update_quiz(quiz_id: str, quiz_input: QuizInput, user: User = Depends( if quiz is None: return JSONResponse(status_code=404, content={"detail": "quiz not found"}) else: - # print(quiz_input) - # print(quiz) quiz_input.description = bleach.clean(quiz_input.description, tags=[], strip=True) quiz_input.title = bleach.clean(quiz_input.title, tags=[], strip=True) meilisearch.index(settings.meilisearch_index).update_documents([await get_meili_data(quiz)]) diff --git a/classquiz/routers/utils.py b/classquiz/routers/utils.py index bf01348..d628d73 100644 --- a/classquiz/routers/utils.py +++ b/classquiz/routers/utils.py @@ -50,10 +50,9 @@ class IpResponse(BaseModel): @router.get("/ip-lookup/{ip}", response_model=IpResponse) async def get_ip_data(ip: str, _: User = Depends(get_current_user)): - async with ClientSession() as session: - async with session.get(f"http://ip-api.com/json/{ip}") as response: - data = await response.json() - try: - return IpResponse(**data) - except ValidationError: - return JSONResponse(status_code=response.status, content=data) + async with ClientSession() as session, session.get(f"http://ip-api.com/json/{ip}") as response: + data = await response.json() + try: + return IpResponse(**data) + except ValidationError: + return JSONResponse(status_code=response.status, content=data) diff --git a/classquiz/storage/deta_storage.py b/classquiz/storage/deta_storage.py index 6136f36..5fa4049 100644 --- a/classquiz/storage/deta_storage.py +++ b/classquiz/storage/deta_storage.py @@ -18,14 +18,15 @@ class DetaStorage: :param file_name: The name of the file to be downloaded :return: Either bytes f successfull download or None if failed """ - async with ClientSession(headers=self.headers) as session: - async with session.get(f"{self.deta_url}/files/download?name={file_name}") as response: - if response.status == 200: - return BytesIO(await response.read()) - elif response.status == 404: - return None - else: - raise Exception("Download failed") + async with ClientSession(headers=self.headers) as session, session.get( + f"{self.deta_url}/files/download?name={file_name}" + ) as response: + if response.status == 200: + return BytesIO(await response.read()) + elif response.status == 404: + return None + else: + raise Exception("Download failed") async def upload(self, file: bytes, file_name: str) -> None: """ @@ -33,17 +34,19 @@ class DetaStorage: :param file_name: The name of the file :return: """ - async with ClientSession(headers=self.headers) as session: - async with session.post(f"{self.deta_url}/files?name={file_name}", data=file) as response: - if response.status == 201: - return None - else: - raise Exception("Upload failed") + async with ClientSession(headers=self.headers) as session, session.post( + f"{self.deta_url}/files?name={file_name}", data=file + ) as response: + if response.status == 201: + return None + else: + raise Exception("Upload failed") async def delete(self, file_names: [str]) -> None: - async with ClientSession(headers=self.headers) as session: - async with session.delete(f"{self.deta_url}/files", json={"names": file_names}) as response: - if response.status == 200: - return None - else: - raise Exception("Delete failed") + async with ClientSession(headers=self.headers) as session, session.delete( + f"{self.deta_url}/files", json={"names": file_names} + ) as response: + if response.status == 200: + return None + else: + raise Exception("Delete failed")