Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Added user-agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Apr 12, 2024
1 parent 56dac65 commit c23f0e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 12 additions & 5 deletions custom_components/o2/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from time import time
from homeassistant.helpers.entity import DeviceInfo
from .const import DOMAIN
from .const import DOMAIN, USER_AGENT, INTEGRATION_VERSION

_LOGGER = logging.getLogger(__name__)

Expand All @@ -27,7 +27,7 @@ def create_session(self, username=None, password=None):
}

# Login to O2
login_response = self._session.post(login_url, data=login_data, allow_redirects=False)
login_response = self._session.post(login_url, data=login_data, headers={ 'User-Agent': self._getUserAgent() }, allow_redirects=False)
if login_response.status_code != 303:
raise ApiException("Failed to login to O2. Status code:", login_response.status_code)

Expand All @@ -40,6 +40,7 @@ def create_session(self, username=None, password=None):
self._password = password

self._token_birth = time()

return True

def get_device_info(self):
Expand All @@ -60,21 +61,27 @@ def update_device_info(self, resp):
def get_csrf(self):
# Fetch account page
data_page_url = 'https://mymobile2.o2.co.uk/'
data_page_response = self._session.get(data_page_url)
data_page_response = self._session.get(data_page_url, headers={ 'User-Agent': self._getUserAgent() })

if data_page_response.status_code != 200:
raise ApiException("Failed to retrieve account page. Status code:", data_page_response.status_code)

return data_page_response.text.split("Liferay.authToken = '")[1].split("'")[0]

def _getUserAgent(self):
return f"{USER_AGENT}/{INTEGRATION_VERSION}"

def _post(self, url):
# Refresh token if its over 30 mins old
if time() - self._token_birth > 1800:
if time() - self._token_birth > 2700:
self.create_session()

csrfToken = self.get_csrf()

return self._session.post(url, headers={'X-Csrf-Token': csrfToken})
return self._session.post(url, headers={
'X-Csrf-Token': csrfToken,
'User-Agent': self._getUserAgent()
})

def get_allowances(self):
resp = self._post('https://mymobile2.o2.co.uk/web/guest/account?p_p_id=O2UKAccountPortlet_INSTANCE_0ssTPnzpDk4K&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=getBoltOnsAndCurrentTariff&p_p_cacheability=cacheLevelPage')
Expand Down
2 changes: 2 additions & 0 deletions custom_components/o2/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
DOMAIN = "o2"
CONFIG_VERSION = 1
ENTITY_TYPES = ["sensor"]
USER_AGENT = "dan-r-homeassistant-o2"
INTEGRATION_VERSION = "0.0.1"

DATA_COORDINATOR = "coordinator"
DATA_APICLIENT = "apiclient"

0 comments on commit c23f0e5

Please sign in to comment.