Skip to content

Commit

Permalink
Merge pull request #4 from fritsvp/master
Browse files Browse the repository at this point in the history
Updated local API to v2.
  • Loading branch information
ualex73 authored Dec 21, 2023
2 parents 7944d23 + 9dc33db commit 9d9144d
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions goslideapi/goslideapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ async def household_set(self, name, address, lat, lon):
class GoSlideLocal:
"""API Wrapper for the Go Slide devices, local connectivity."""

def __init__(self, timeout=DEFAULT_TIMEOUT, authexception=True, apiversion=1):
def __init__(self, timeout=DEFAULT_TIMEOUT, authexception=True, apiversion=2):
"""Create the object with required parameters."""
self._timeout = timeout
self._authexception = authexception
Expand Down Expand Up @@ -589,13 +589,16 @@ async def _request(self, hostname, password, reqtype, uri, data=None):
# format URL with hostname/ip and uri value
url = "http://{}{}".format(hostname, uri)

# First request, should return a 401 error
respstatus, resptext = await self._dorequest(reqtype, url)
# First request, should return a 401 error for v1
# First request is not required for v2

# Default is version 1, when we do WWW-Authentication
if self._apiversion == 1:

# Only a 401 response is correct
#do request to obtain a WWW-authentication header:
respstatus, resptext = await self._dorequest(reqtype, url)

#Only a 401 response is correct
if respstatus == 401:

# The resptext contains the WWW-Authentication header
Expand All @@ -616,17 +619,28 @@ async def _request(self, hostname, password, reqtype, uri, data=None):
else:
# We expected a 401 Digest Auth here
_LOGGER.error(
"Failed request with Local API. Received HTTPCode=%s, expected HTTPCode=401",
"Failed request with Local API v1. Received HTTPCode=%s, expected HTTPCode=401. Maybe try switching to v2?",
respstatus,
)
else:
if respstatus == 200:
return resptext

# Anything else is an error
elif self._apiversion == 2:

respstatus, resptext = await self._dorequest(
reqtype, url, data=data
)

if respstatus == 200:
return resptext

# Anything else is an error
_LOGGER.error(
"Failed request Local API v2. HTTPCode=%s",
respstatus,
)

else:
_LOGGER.error(
"Failed request with Local API. HTTPCode=%s",
respstatus,
"Only v1 and v2 is supported.",
)

return None
Expand Down

0 comments on commit 9d9144d

Please sign in to comment.