🚨 Merged with-statements

This commit is contained in:
Mawoka
2022-04-16 11:54:14 +02:00
parent 69f0aa8846
commit bd23036414
5 changed files with 42 additions and 42 deletions
+9 -8
View File
@@ -10,11 +10,12 @@ class _Response(BaseModel):
async def get(game_id: str) -> _Response | None: async def get(game_id: str) -> _Response | None:
async with ClientSession() as session: async with ClientSession() as session, session.get(
async with session.get(f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true") as response: f"https://create.kahoot.it/rest/kahoots/{game_id}/card/?includeKahoot=true"
if response.status == 200: ) as response:
return _Response(**await response.json()) if response.status == 200:
elif response.status == 404: return _Response(**await response.json())
return None elif response.status == 404:
else: return None
raise Exception(f"Unexpected response status: {response.status}") else:
raise Exception(f"Unexpected response status: {response.status}")
+4 -5
View File
@@ -30,8 +30,7 @@ async def search(
:param limit: Less or equals 100 :param limit: Less or equals 100
:return: :return:
""" """
async with ClientSession() as session: async with ClientSession() as session, session.get(
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
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:
) as response: return _Response(**await response.json())
return _Response(**await response.json())
-2
View File
@@ -132,8 +132,6 @@ async def update_quiz(quiz_id: str, quiz_input: QuizInput, user: User = Depends(
if quiz is None: if quiz is None:
return JSONResponse(status_code=404, content={"detail": "quiz not found"}) return JSONResponse(status_code=404, content={"detail": "quiz not found"})
else: else:
# print(quiz_input)
# print(quiz)
quiz_input.description = bleach.clean(quiz_input.description, tags=[], strip=True) quiz_input.description = bleach.clean(quiz_input.description, tags=[], strip=True)
quiz_input.title = bleach.clean(quiz_input.title, 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)]) meilisearch.index(settings.meilisearch_index).update_documents([await get_meili_data(quiz)])
+6 -7
View File
@@ -50,10 +50,9 @@ class IpResponse(BaseModel):
@router.get("/ip-lookup/{ip}", response_model=IpResponse) @router.get("/ip-lookup/{ip}", response_model=IpResponse)
async def get_ip_data(ip: str, _: User = Depends(get_current_user)): async def get_ip_data(ip: str, _: User = Depends(get_current_user)):
async with ClientSession() as session: async with ClientSession() as session, session.get(f"http://ip-api.com/json/{ip}") as response:
async with session.get(f"http://ip-api.com/json/{ip}") as response: data = await response.json()
data = await response.json() try:
try: return IpResponse(**data)
return IpResponse(**data) except ValidationError:
except ValidationError: return JSONResponse(status_code=response.status, content=data)
return JSONResponse(status_code=response.status, content=data)
+23 -20
View File
@@ -18,14 +18,15 @@ class DetaStorage:
:param file_name: The name of the file to be downloaded :param file_name: The name of the file to be downloaded
:return: Either bytes f successfull download or None if failed :return: Either bytes f successfull download or None if failed
""" """
async with ClientSession(headers=self.headers) as session: async with ClientSession(headers=self.headers) as session, session.get(
async with session.get(f"{self.deta_url}/files/download?name={file_name}") as response: f"{self.deta_url}/files/download?name={file_name}"
if response.status == 200: ) as response:
return BytesIO(await response.read()) if response.status == 200:
elif response.status == 404: return BytesIO(await response.read())
return None elif response.status == 404:
else: return None
raise Exception("Download failed") else:
raise Exception("Download failed")
async def upload(self, file: bytes, file_name: str) -> None: async def upload(self, file: bytes, file_name: str) -> None:
""" """
@@ -33,17 +34,19 @@ class DetaStorage:
:param file_name: The name of the file :param file_name: The name of the file
:return: :return:
""" """
async with ClientSession(headers=self.headers) as session: async with ClientSession(headers=self.headers) as session, session.post(
async with session.post(f"{self.deta_url}/files?name={file_name}", data=file) as response: f"{self.deta_url}/files?name={file_name}", data=file
if response.status == 201: ) as response:
return None if response.status == 201:
else: return None
raise Exception("Upload failed") else:
raise Exception("Upload failed")
async def delete(self, file_names: [str]) -> None: async def delete(self, file_names: [str]) -> None:
async with ClientSession(headers=self.headers) as session: async with ClientSession(headers=self.headers) as session, session.delete(
async with session.delete(f"{self.deta_url}/files", json={"names": file_names}) as response: f"{self.deta_url}/files", json={"names": file_names}
if response.status == 200: ) as response:
return None if response.status == 200:
else: return None
raise Exception("Delete failed") else:
raise Exception("Delete failed")