Skip to content

Commit

Permalink
Catch exceptions, reduce calls to PostNL and block the connection poo… (
Browse files Browse the repository at this point in the history
#14)

* Catch exceptions, reduce calls to PostNL and block the connection pool if full

* Refresh after 90 instead of 120 seconds
  • Loading branch information
arjenbos authored Jan 18, 2024
1 parent bbc67eb commit 7d05284
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 49 deletions.
15 changes: 12 additions & 3 deletions custom_components/postnl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import time

import requests
import urllib3
from aiohttp.client_exceptions import ClientError, ClientResponseError
from gql.transport.exceptions import TransportQueryError
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
Expand Down Expand Up @@ -28,17 +30,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> True:
implementation = await async_get_config_entry_implementation(hass, entry)
session = OAuth2Session(hass, entry, implementation)
auth = AsyncConfigEntryAuth(session)
await auth.check_and_refresh_token()

try:
await auth.check_and_refresh_token()
except requests.exceptions.ConnectionError as exception:
raise ConfigEntryNotReady(f"Unable to retrieve oauth data from PostNL") from exception

hass.data[DOMAIN][entry.entry_id] = auth

_LOGGER.debug('Using access token: %s', auth.access_token)

postnl_login_api = PostNLLoginAPI(auth.access_token)
userinfo = await hass.async_add_executor_job(postnl_login_api.userinfo)
try:
userinfo = await hass.async_add_executor_job(postnl_login_api.userinfo)
except (requests.exceptions.RequestException, urllib3.exceptions.MaxRetryError) as exception:
raise ConfigEntryNotReady(f"Unable to retrieve user information from PostNL.") from exception

if "error" in userinfo:
raise ConfigEntryNotReady
raise ConfigEntryNotReady(f"Error in retrieving user information from PostNL.")

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
107 changes: 62 additions & 45 deletions custom_components/postnl/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import logging
from datetime import timedelta

import requests
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.update_coordinator import (DataUpdateCoordinator,
UpdateFailed)

from . import AsyncConfigEntryAuth, PostNLGraphql
from .const import DOMAIN
Expand All @@ -22,13 +24,12 @@ def __init__(self, hass: HomeAssistant) -> None:
hass,
_LOGGER,
name="PostNL",
update_interval=timedelta(seconds=120),
update_interval=timedelta(seconds=90),
)

async def _async_update_data(self) -> dict[str, tuple[Package]]:
_LOGGER.debug('Get API data')
try:

auth: AsyncConfigEntryAuth = self.hass.data[DOMAIN][self.config_entry.entry_id]
await auth.check_and_refresh_token()

Expand All @@ -53,54 +54,70 @@ async def _async_update_data(self) -> dict[str, tuple[Package]]:
_LOGGER.debug('Found %d packages', len(data['sender']) + len(data['receiver']))

return data
except Exception as exception:
raise UpdateFailed(exception)
except requests.exceptions.RequestException as exception:
raise UpdateFailed(f"Unable to update PostNL data") from exception

async def transform_shipment(self, shipment) -> Package:
_LOGGER.debug('Updating %s', shipment.get('key'))

track_and_trace_details = await self.hass.async_add_executor_job(self.jouw_api.track_and_trace, shipment['key'])

if not track_and_trace_details.get('colli'):
_LOGGER.debug('No colli found.')
_LOGGER.debug(track_and_trace_details)

colli = track_and_trace_details.get('colli', {}).get(shipment['barcode'], {})

if colli:
if colli.get("routeInformation"):
route_information = colli.get("routeInformation")
planned_date = route_information.get("plannedDeliveryTime")
planned_from = route_information.get("plannedDeliveryTimeWindow", {}).get("startDateTime")
planned_to = route_information.get("plannedDeliveryTimeWindow", {}).get('endDateTime')
expected_datetime = route_information.get('expectedDeliveryTime')
elif colli.get('eta'):
planned_date = colli.get('eta', {}).get('start')
planned_from = colli.get('eta', {}).get('start')
planned_to = colli.get('eta', {}).get('end')
expected_datetime = None
try:
if shipment.get('delivered'):
_LOGGER.debug('%s already delivered, no need to call jouw.postnl.', shipment.get('key'))

return Package(
key=shipment.get('key'),
name=shipment.get('title'),
url=shipment.get('detailsUrl'),
status_message="Pakket is bezorgd",
delivered=shipment.get('delivered'),
delivery_date=shipment.get('deliveredTimeStamp')
)

track_and_trace_details = await self.hass.async_add_executor_job(self.jouw_api.track_and_trace,
shipment['key'])

if not track_and_trace_details.get('colli'):
_LOGGER.debug('No colli found.')
_LOGGER.debug(track_and_trace_details)

colli = track_and_trace_details.get('colli', {}).get(shipment['barcode'], {})

if colli:
if colli.get("routeInformation"):
route_information = colli.get("routeInformation")
planned_date = route_information.get("plannedDeliveryTime")
planned_from = route_information.get("plannedDeliveryTimeWindow", {}).get("startDateTime")
planned_to = route_information.get("plannedDeliveryTimeWindow", {}).get('endDateTime')
expected_datetime = route_information.get('expectedDeliveryTime')
elif colli.get('eta'):
planned_date = colli.get('eta', {}).get('start')
planned_from = colli.get('eta', {}).get('start')
planned_to = colli.get('eta', {}).get('end')
expected_datetime = None
else:
planned_date = shipment.get('deliveryWindowFrom', None)
planned_from = shipment.get('deliveryWindowFrom', None)
planned_to = shipment.get('deliveryWindowTo', None)
expected_datetime = None
else:
_LOGGER.debug('Barcode not found in track and trace details.')
_LOGGER.debug(track_and_trace_details)
planned_date = shipment.get('deliveryWindowFrom', None)
planned_from = shipment.get('deliveryWindowFrom', None)
planned_to = shipment.get('deliveryWindowTo', None)
expected_datetime = None
else:
_LOGGER.debug('Barcode not found in track and trace details.')
_LOGGER.debug(track_and_trace_details)
planned_date = shipment.get('deliveryWindowFrom', None)
planned_from = shipment.get('deliveryWindowFrom', None)
planned_to = shipment.get('deliveryWindowTo', None)
expected_datetime = None

return Package(
key=shipment.get('key'),
name=shipment.get('title'),
url=shipment.get('detailsUrl'),
status_message=colli.get('statusPhase', {}).get('message', "Unknown"),
delivered=shipment.get('delivered'),
delivery_date=shipment.get('deliveredTimeStamp'),
planned_date=planned_date,
planned_from=planned_from,
planned_to=planned_to,
expected_datetime=expected_datetime
)

return Package(
key=shipment.get('key'),
name=shipment.get('title'),
url=shipment.get('detailsUrl'),
status_message=colli.get('statusPhase', {}).get('message', "Unknown"),
delivered=shipment.get('delivered'),
delivery_date=shipment.get('deliveredTimeStamp'),
planned_date=planned_date,
planned_from=planned_from,
planned_to=planned_to,
expected_datetime=expected_datetime
)
except requests.exceptions.RequestException as exception:
raise UpdateFailed(f"Unable to update PostNL data") from exception
3 changes: 2 additions & 1 deletion custom_components/postnl/jouw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(self, access_token: str):
total=5,
backoff_factor=3
),
pool_maxsize=50
pool_maxsize=25,
pool_block=True
)
)
self.client.headers = {
Expand Down

0 comments on commit 7d05284

Please sign in to comment.