Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions custom_components/recycle/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ async def get_faq(self, organisation_id: str):
# ------ Address API --------------------------------------------------------- #

async def validate_address(self, info: ApiAddress):
token = await self.get_token()
headers = self._headers(token)
headers = self._headers()
params = self._address_params(info)
async with self._session.head(API_URL_BASE + 'streets/validate', params=params, headers=headers, raise_for_status=False) as request:
return 200 <= request.status < 300
Expand Down Expand Up @@ -147,34 +146,13 @@ async def get_endpoint(self, endpoint: str, params: dict[str, any] | None = None
if page is not None:
params['page'] = page

token = await self.get_token()
headers = self._headers(token)
headers = self._headers()
return await self._request(API_URL_BASE + endpoint, params=params, headers=headers)

async def get_token(self, renew: bool = False) -> str:
# check if cached token is still valid
if self._cached_token and pytz.utc.localize(datetime.utcnow()) < self._cached_expiry and not renew:
return self._cached_token

# request new token
headers = {
'User-Agent': API_USER_AGENT,
'X-Secret': API_X_SECRET,
'X-Consumer': API_X_CONSUMER,
}
response = await self._request(API_URL_BASE + 'access-token', headers=headers)

# update cached token and expiry info
self._cached_token = response['accessToken']
self._cached_expiry = parser.parse(response['expiresAt']) - timedelta(minutes=5)

return self._cached_token

@staticmethod
def _headers(token: str) -> dict[str, str]:
def _headers() -> dict[str, str]:
return {
'User-Agent': API_USER_AGENT,
'Authorization': token,
'X-Consumer': API_X_CONSUMER,
}

Expand Down