From 9dc33dbec8cda932396fbd1dafdc46c9ff05a316 Mon Sep 17 00:00:00 2001 From: fritsvp <12990041+fritsvp@users.noreply.github.com> Date: Thu, 21 Dec 2023 12:26:02 +0100 Subject: [PATCH] Update goslideapi.py Fixes for v2 without authentication --- goslideapi/goslideapi.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/goslideapi/goslideapi.py b/goslideapi/goslideapi.py index 72e183a..b7b75a7 100644 --- a/goslideapi/goslideapi.py +++ b/goslideapi/goslideapi.py @@ -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 @@ -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 @@ -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