Skip to content

Commit

Permalink
Added: Timeout on HTTPS requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ualex73 committed Aug 3, 2019
1 parent 266936b commit d34f29e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions goslideapi/goslideapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

# API Link: https://documenter.getpostman.com/view/6223391/S1Lu2pSf
BASEURL = 'https://api.goslide.io/api/{}'
DEFAULT_TIMEOUT = 30


class GoSlideCloud:
"""API Wrapper for the Go Slide devices."""

def __init__(self, username, password):
def __init__(self, username, password, timeout=DEFAULT_TIMEOUT):
"""Create the object with required parameters."""
self._username = username
self._password = password
self._timeout = timeout
self._authenticated = False
self._accesstoken = ''
self._authfailed = False
Expand All @@ -35,6 +37,9 @@ async def _dorequest(self, reqtype, urlsuffix, data=None):
_LOGGER.debug("REQ: API=%s, type=%s, data=%s",
BASEURL.format(urlsuffix), reqtype, json.dumps(data))

# Set a reasonable timeout, otherwise it can take > 300 seconds
atimeout = aiohttp.ClientTimeout(total=self._timeout)

# Known error codes from the Cloud API:
# 401 - Authentication failed
# 403 - Forbidden, most likely we want to control a slide
Expand All @@ -47,7 +52,8 @@ async def _dorequest(self, reqtype, urlsuffix, data=None):
async with aiohttp.request(reqtype,
BASEURL.format(urlsuffix),
headers=headers,
json=data) as resp:
json=data,
timeout=atimeout) as resp:
if resp.status in [200, 424]:
textdata = await resp.text()
_LOGGER.debug("RES: API=%s, type=%s, HTTPCode=%s, Data=%s",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setuptools.setup(
name='goslide-api',
version='0.2.0',
version='0.2.1',
url='https://github.com/ualex73/goslide-api',
license='Apache License 2.0',
author='Alexander Kuiper',
Expand Down

0 comments on commit d34f29e

Please sign in to comment.