Skip to content

Commit

Permalink
Add timeout to downloading custom background images
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jul 27, 2021
1 parent c489c34 commit 9bf1f19
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/utils/http.py
@@ -1,3 +1,5 @@
import asyncio

import aiofiles
import aiohttp
import aiohttp.client_exceptions
Expand All @@ -9,14 +11,15 @@
aiohttp.client_exceptions.InvalidURL,
aiohttp.client_exceptions.TooManyRedirects,
AssertionError,
asyncio.TimeoutError,
UnicodeError,
)


async def download(url: str, path: AsyncPath) -> bool:
async with aiohttp.ClientSession() as session:
try:
async with session.get(url) as response:
async with session.get(url, timeout=10) as response:

if response.status == 200:
f = await aiofiles.open(path, mode="wb") # type: ignore
Expand All @@ -27,7 +30,7 @@ async def download(url: str, path: AsyncPath) -> bool:
logger.error(f"{response.status} response from {url}")

except EXCEPTIONS as e:
message = str(e).strip("() ")
message = str(e).strip("() ") or e.__class__.__name__
logger.error(f"Invalid response from {url}: {message}")

return False

0 comments on commit 9bf1f19

Please sign in to comment.