Skip to content

Commit

Permalink
fix: Updated user agent for sent requests
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Nov 8, 2024
1 parent a2c4f7c commit 0dd8587
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions custom_components/octopus_energy/api_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@
}}
'''

user_agent_value = "bottlecapdave-home-assistant-octopus-energy"
user_agent_value = "bottlecapdave-ha-octopus-energy"

def get_valid_from(rate):
return rate["valid_from"]
Expand Down Expand Up @@ -636,7 +636,10 @@ def _create_client_session(self):
return self._session

with self._session_lock:
self._session = aiohttp.ClientSession(timeout=self._timeout, headers=self._default_headers)
if self._session is not None:
return self._session

self._session = aiohttp.ClientSession(headers=self._default_headers, skip_auto_headers=['User-Agent'])
return self._session

async def async_refresh_token(self):
Expand All @@ -650,11 +653,16 @@ async def async_refresh_token(self):
return

try:
print(f"{now()} - create session - start")
client = self._create_client_session()
print(f"{now()} - create session - end")
url = f'{self._base_url}/v1/graphql/'
payload = { "query": api_token_query.format(api_key=self._api_key) }
print(f"{now()} - http refresh - start")
async with client.post(url, json=payload) as token_response:
print(f"{now()} - http refresh - end")
token_response_body = await self.__async_read_response__(token_response, url)
print(f"{now()} - read response")
if (token_response_body is not None and
"data" in token_response_body and
"obtainKrakenToken" in token_response_body["data"] and
Expand Down Expand Up @@ -761,19 +769,29 @@ def map_gas_meters(self, meter_point):

async def async_get_account(self, account_id):
"""Get the user's account"""
print(f"{now()} - get token - start")
await self.async_refresh_token()
print(f"{now()} - get token - end")

local_now: datetime = now()
local_date = local_now.date()

try:

print(f"{now()} - create session - start")
client = self._create_client_session()
print(f"{now()} - create session - end")
url = f'{self._base_url}/v1/graphql/'
# Get account response
payload = { "query": account_query.format(account_id=account_id) }
headers = { "Authorization": f"JWT {self._graphql_token}" }

print(f"{now()} - post http - start")
async with client.post(url, json=payload, headers=headers) as account_response:
print(f"{now()} - post http - end")
account_response_body = await self.__async_read_response__(account_response, url)

print(f"{now()} - read response")

_LOGGER.debug(f'account: {account_response_body}')

Expand Down

0 comments on commit 0dd8587

Please sign in to comment.