Skip to content

Commit

Permalink
client uses aiohttp to fetch data
Browse files Browse the repository at this point in the history
  • Loading branch information
aallen90 committed Oct 11, 2024
1 parent 85dc5d5 commit 9b4a578
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions custom_components/poolmath/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import aiohttp
import json
import logging
import re

import httpx

from .const import (
ATTR_TARGET_MAX,
Expand Down Expand Up @@ -66,19 +66,21 @@ def __init__(self, url, name=DEFAULT_NAME, timeout=DEFAULT_TIMEOUT):
async def async_update(self):
"""Fetch latest json formatted data from the Pool Math API"""

async with httpx.AsyncClient() as client:
LOG.info(
f'GET {self._json_url} (timeout={self._timeout}; name={self.name}; id={self.pool_id})'
)
response = await client.request(
'GET', self._json_url, timeout=self._timeout, follow_redirects=True
)
LOG.debug(f'GET {self._json_url} response: {response.status_code}')

if response.status_code == httpx.codes.OK:
return json.loads(response.text)

return None
async with aiohttp.ClientSession() as session:
try:
LOG.info(
f'GET {self._json_url} (timeout={self._timeout}; name={self.name}; id={self.pool_id})'
)
async with session.get(self._json_url, timeout=self._timeout) as response:
LOG.debug(f'GET {self._json_url} response: {response.status}')
if response.status == 200:
return await response.json()
else:
LOG.error(f"Error: Received status code {response.status} from API")
return None
except aiohttp.ClientError as e:
LOG.error(f"Error fetching data from PoolMath API: {e}")
return None

async def process_log_entry_callbacks(self, poolmath_json, async_callback):
"""Call provided async callback once for each type of log entry"""
Expand Down

0 comments on commit 9b4a578

Please sign in to comment.