Skip to content

Commit

Permalink
Merge pull request #21 from fdegier/bugfix/cookies
Browse files Browse the repository at this point in the history
Fix re-initiating cookies
  • Loading branch information
fdegier authored Jun 17, 2024
2 parents 255fd4d + c3c9809 commit 7a23aa1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
36 changes: 16 additions & 20 deletions jablotronpy/jablotronpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, username: str, password: str, pin_code: Union[str, int]):
"""
self.headers = {
"x-vendor-id": "JABLOTRON:Jablotron",
"x-client-version": "MYJ-PUB-ANDROID-12",
"x-client-version": "MYJ-PUB-ANDROID-15",
"accept-encoding": "*",
"Accept": "application/json",
"Content-Type": "application/json",
Expand All @@ -36,6 +36,8 @@ def set_cookies(self):
Retrieve the session_id and set it in the header as a cookie
:return:
"""
if 'Cookie' in self.headers:
del self.headers['Cookie']
session_id = self.get_session_id()
self.headers['Cookie'] = f'PHPSESSID={session_id}'

Expand Down Expand Up @@ -65,26 +67,20 @@ def _make_request(self, end_point: str, headers: dict, payload: dict, retry: int
return False, None

data = r.json()
if data.get('http-code', 0) == 401:
if data.get('http-code', 0) in [400, 401]:
print(f"Error: {data}, setting cookies and retrying.")
self.set_cookies()
if retry >= 3:
logger.error(f"Exhausted all retry options, response: {data.json()}")
if 'errors' in data:
logger.error(data['errors'])
return False, None
else:
retry += 1
return self._make_request(end_point=end_point, headers=headers, payload=payload, retry=retry)
else:
logger.error(f"An unexpected error occurred, status code: {r.status_code}")
if retry >= 3:
logger.error(f"Exhausted all retry options, response: {data.json()}")
if 'errors' in data:
logger.error(data['errors'])
return False, None
else:
retry += 1
return self._make_request(end_point=end_point, headers=headers, payload=payload, retry=retry)
logger.error(f"An unexpected error occurred, status code: {r.status_code}, response: {data}, {r.text}")

if retry >= 3:
logger.error(f"Exhausted all retry options, response: {data.json()}")
if 'errors' in data:
logger.error(data['errors'])
return False, None
else:
retry += 1
return self._make_request(end_point=end_point, headers=headers, payload=payload, retry=retry)

def get_session_id(self) -> str:
"""
Expand Down Expand Up @@ -275,7 +271,7 @@ def get_programmable_gates(self, service_id: int, service_type: str = "JA100") -
"service-states": True
}
)
if status and 'programmableGates' in data:
if status:
return data
raise UnexpectedResponse("Unable to retrieve programmable gates.")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="JablotronPy",
version="0.6.2",
version="0.6.3",
author="F. de Gier",
author_email="freddegier@me.com",
description="A client to interact with the Jablotron API to control Jablotron alarm systems",
Expand Down

0 comments on commit 7a23aa1

Please sign in to comment.