-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code simplification and improvements
- Loading branch information
Showing
4 changed files
with
203 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import logging | ||
import aiohttp | ||
import async_timeout | ||
|
||
REQUEST_TIMEOUT = 30 | ||
_LOGGER = logging.getLogger(__name__) | ||
|
||
async def fetch(session, url): | ||
try: | ||
with async_timeout.timeout(15): | ||
|
||
async def get(url): | ||
timeout = aiohttp.ClientTimeout(total=REQUEST_TIMEOUT) | ||
async with aiohttp.ClientSession(timeout=timeout) as session: | ||
try: | ||
async with session.get( | ||
url, headers={"Accept": "application/json"} | ||
) as response: | ||
return await response.text() | ||
except: | ||
pass | ||
|
||
|
||
async def request(url): | ||
async with aiohttp.ClientSession() as session: | ||
return await fetch(session, url) | ||
if response.status == 200: | ||
return await response.text() | ||
_LOGGER.error("GET %s: %s", url, response.status) | ||
return | ||
except aiohttp.ClientConnectorError as err: | ||
_LOGGER.error("Connection error: %s", err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.