Skip to content

Commit

Permalink
Implement request timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
bm1549 committed Aug 11, 2021
1 parent 10f7ded commit 3009b0e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
8 changes: 5 additions & 3 deletions frigidaire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,20 @@ class Frigidaire:
This was reverse-engineered from the Frigidaire 2.0 App
"""

def __init__(self, username: str, password: str, session_key: Optional[str] = None):
def __init__(self, username: str, password: str, session_key: Optional[str] = None, timeout: Optional[float] = None):
"""
Initializes a new instance of the Frigidaire API and authenticates against it
:param username: The username to login to Frigidaire. Generally, this is an email
:param password: The password to login to Frigidaire
:param session_key: The previously authenticated session key to connect to Frigidaire. If not specified,
authentication is required
:param timeout: The amount of time in seconds to wait before timing out a request
"""
self.username = username
self.password = password
self.device_id: str = str(uuid.uuid4())
self.session_key: Optional[str] = session_key
self.timeout: Optional[float] = timeout

self.authenticate()

Expand Down Expand Up @@ -358,7 +360,7 @@ def get_request(self, path: str) -> Union[Dict, List]:
:param path: The path to the resource, including query params
:return: The contents of 'data' in the resulting json
"""
response = requests.get(f'{API_URL}{path}', headers=self.headers, verify=False)
response = requests.get(f'{API_URL}{path}', headers=self.headers, verify=False, timeout=self.timeout)
return self.parse_response(response)

def post_request(self, path: str, data: Dict) -> Union[Dict, List]:
Expand All @@ -368,5 +370,5 @@ def post_request(self, path: str, data: Dict) -> Union[Dict, List]:
:param data: The data to include in the body of the request
:return: The contents of 'data' in the resulting json
"""
response = requests.post(f'{API_URL}{path}', data=json.dumps(data), headers=self.headers, verify=False)
response = requests.post(f'{API_URL}{path}', data=json.dumps(data), headers=self.headers, verify=False, timeout=self.timeout)
return self.parse_response(response)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name='frigidaire',
version='0.11',
version='0.12',
author="Brian Marks",
description="Python API for the Frigidaire 2.0 App",
license="MIT",
Expand Down
1 change: 1 addition & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
username,
password,
# session_key, # uncomment this if testing with an already authenticated session key
# timeout=5, # uncomment this if testing the request timeout
)

# tests connectivity
Expand Down

0 comments on commit 3009b0e

Please sign in to comment.