Skip to content

Commit 7fc6099

Browse files
committed
Migrate to httpx
1 parent bceb2a7 commit 7fc6099

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

luftdaten/__init__.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Wrapper to get the measurings from a Luftdaten station."""
2-
import asyncio
32
import logging
43

5-
import aiohttp
6-
import async_timeout
4+
import httpx
75

86
from . import exceptions
97

@@ -14,10 +12,8 @@
1412
class Luftdaten(object):
1513
"""A class for handling the data retrieval."""
1614

17-
def __init__(self, sensor_id, loop, session):
15+
def __init__(self, sensor_id):
1816
"""Initialize the connection."""
19-
self._loop = loop
20-
self._session = session
2117
self.sensor_id = sensor_id
2218
self.data = None
2319
self.values = {}
@@ -26,20 +22,23 @@ def __init__(self, sensor_id, loop, session):
2622

2723
async def get_data(self):
2824
"""Retrieve the data."""
25+
url = '{}/{}/'.format(self.url, self.sensor_id)
26+
2927
try:
30-
url = '{}/{}/'.format(self.url, self.sensor_id)
31-
_LOGGER.debug(
32-
"Requesting luftdaten.info: %s", url)
33-
with async_timeout.timeout(60, loop=self._loop):
34-
response = await self._session.get(url)
28+
async with httpx.AsyncClient() as client:
29+
response = await client.get(str(url))
30+
except httpx.ConnectError:
31+
raise exceptions.LuftdatenConnectionError(f"Connection to {url} failed")
3532

36-
_LOGGER.debug(
37-
"Response from luftdaten.info: %s", response.status)
38-
self.data = await response.json()
39-
_LOGGER.debug(self.data)
40-
except (asyncio.TimeoutError, aiohttp.ClientError):
41-
_LOGGER.error("Can not load data from luftdaten.info")
42-
raise exceptions.LuftdatenConnectionError()
33+
if response.status_code == httpx.codes.OK:
34+
try:
35+
_LOGGER.debug(response.json())
36+
self.data = response.json()
37+
except TypeError:
38+
_LOGGER.error("Can not load data from Luftdaten API")
39+
raise exceptions.LuftdatenConnectionError(
40+
"Unable to get the data from Luftdaten API"
41+
)
4342

4443
if not self.data:
4544
for measurement in self.values.keys():

0 commit comments

Comments
 (0)