Skip to content

Commit

Permalink
Merge pull request #20 from crevetor/main
Browse files Browse the repository at this point in the history
Add ability to set timeout for requests
  • Loading branch information
rewardone committed Feb 14, 2024
2 parents ef035f6 + 9a0d0d2 commit caca114
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/ayla_iot_unofficial/ayla_iot_unofficial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
- https://docs.aylanetworks.com/cloud-services/api-browser/
"""

from aiohttp import ClientSession # async http
from requests import post, request, Response # http request library
from datetime import datetime, timedelta # datetime operations
from typing import Dict, List, Optional # object types
from aiohttp import ClientSession, ClientTimeout # async http
from requests import post, request, Response # http request library
from datetime import datetime, timedelta # datetime operations
from typing import Dict, List, Optional # object types

from .const import (
EU_USER_FIELD_BASE,
EU_ADS_BASE,
EU_RULESSERVICE_BASE,
USER_FIELD_BASE,
ADS_BASE,
RULESSERVICE_BASE
RULESSERVICE_BASE,
DEFAULT_TIMEOUT
)

# Custom error handling
Expand All @@ -35,12 +36,12 @@

_session = None

def new_ayla_api(username: str, password: str, app_id: str, app_secret: str, websession: Optional[ClientSession] = None, europe: bool = False):
def new_ayla_api(username: str, password: str, app_id: str, app_secret: str, websession: Optional[ClientSession] = None, europe: bool = False, timeout=DEFAULT_TIMEOUT):
"""Get an AylaApi object"""
if europe:
return AylaApi(username, password, app_id, app_secret, websession=websession, europe=europe)
return AylaApi(username, password, app_id, app_secret, websession=websession, europe=europe, timeout=timeout)
else:
return AylaApi(username, password, app_id, app_secret, websession=websession)
return AylaApi(username, password, app_id, app_secret, websession=websession, timeout=timeout)


class AylaApi:
Expand All @@ -53,7 +54,8 @@ def __init__(
app_id: str,
app_secret: str,
websession: Optional[ClientSession] = None,
europe: bool = False):
europe: bool = False,
timeout: int=DEFAULT_TIMEOUT):
self._email = username # username should always be an email address
self._password = password
self._access_token = None # type: Optional[str]
Expand All @@ -70,13 +72,14 @@ def __init__(
self.user_field_url = USER_FIELD_BASE
self.ads_url = ADS_BASE
self.rulesservice_url = RULESSERVICE_BASE
self.timeout = timeout
self._vacuum_devices = ["Vacuum","SharkIQ"]
self._softener_devices = ["Softener","Smart HE","Water Softener"]

async def ensure_session(self) -> ClientSession:
"""Ensure that we have an aiohttp ClientSession"""
if self.websession is None:
self.websession = ClientSession()
self.websession = ClientSession(timeout=ClientTimeout(total=self.timeout))
return self.websession

@property
Expand Down Expand Up @@ -216,7 +219,7 @@ def _get_headers(self, fn_kwargs) -> Dict[str, str]:
def self_request(self, method: str, url: str, **kwargs) -> Response:
"""Perform an arbitrary request using the requests library synchronously"""
headers = self._get_headers(kwargs)
return request(method, url, headers=headers, **kwargs)
return request(method, url, headers=headers, timeout=self.timeout, **kwargs)

async def async_request(self, http_method: str, url: str, **kwargs):
"""Perform an arbitrary request using the aiohttp library asynchronously"""
Expand Down
4 changes: 3 additions & 1 deletion src/ayla_iot_unofficial/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

USER_FIELD_BASE = "https://user-field.aylanetworks.com"
ADS_BASE = "https://ads-field.aylanetworks.com"
RULESSERVICE_BASE = "https://rulesservice-field.aylanetworks.com"
RULESSERVICE_BASE = "https://rulesservice-field.aylanetworks.com"

DEFAULT_TIMEOUT = 5*60

0 comments on commit caca114

Please sign in to comment.