🚨 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 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}")
+4 -5
View File
@@ -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())
-2
View File
@@ -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)])
+6 -7
View File
@@ -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)
+23 -20
View File
@@ -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")