Skip to content

Commit

Permalink
Clear auth token for any login error, or any comm err during generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Jan 12, 2020
1 parent fdbb775 commit 8acefe1
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/amcrest/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self, host, port, user,
retries_connection=None, timeout_protocol=None):

self._token_lock = threading.Lock()
self._cmd_id_lock = threading.Lock()
self._cmd_id = 0
self._host = clean_url(host)
self._port = port
self._user = user
Expand Down Expand Up @@ -81,16 +83,13 @@ def _generate_token(self):
_LOGGER.debug('%s Trying Basic Authentication', self)
self._token = requests.auth.HTTPBasicAuth(self._user, self._password)
try:
resp = self._command(cmd).content.decode('utf-8')
except LoginError:
_LOGGER.debug('%s Trying Digest Authentication', self)
self._token = requests.auth.HTTPDigestAuth(
self._user, self._password)
try:
resp = self._command(cmd).content.decode('utf-8')
except LoginError as error:
self._token = None
raise error
except LoginError:
_LOGGER.debug('%s Trying Digest Authentication', self)
self._token = requests.auth.HTTPDigestAuth(
self._user, self._password)
resp = self._command(cmd).content.decode('utf-8')
except CommError:
self._token = None
raise
Expand Down Expand Up @@ -143,37 +142,41 @@ def command(self, cmd, retries=None, timeout_cmd=None, stream=False):
return self._command(cmd, retries, timeout_cmd, stream)

def _command(self, cmd, retries=None, timeout_cmd=None, stream=False):
session = requests.Session()
session.verify = self._verify
url = self.__base_url(cmd)
_LOGGER.debug("%s HTTP query %s", self, url)
with self._cmd_id_lock:
cmd_id = self._cmd_id = self._cmd_id + 1
_LOGGER.debug("%s HTTP query %i: %s", self, cmd_id, url)
if retries is None:
retries = self._retries_default
for loop in range(1, 2 + retries):
_LOGGER.debug("%s Running query attempt %s", self, loop)
_LOGGER.debug("%s Running query %i attempt %s", self, cmd_id, loop)
try:
resp = session.get(
resp = requests.get(
url,
auth=self._token,
stream=stream,
timeout=timeout_cmd or self._timeout_default,
verify=self._verify,
)
if resp.status_code == 401:
_LOGGER.debug(
"%s Query %i: Unauthorized (401)", self, cmd_id)
self._token = None
raise LoginError
resp.raise_for_status()
except requests.RequestException as error:
msg = re.sub(r'at 0x[0-9a-fA-F]+', 'at ADDRESS', repr(error))
_LOGGER.debug(
"%s Query %i failed due to error: %r", self, cmd_id, error)
if loop > retries:
_LOGGER.debug(
"%s Query failed due to error: %s", self, msg)
raise CommError(error)
msg = re.sub(r'at 0x[0-9a-fA-F]+', 'at ADDRESS', repr(error))
_LOGGER.warning("%s Trying again due to error: %s", self, msg)
continue
else:
break

_LOGGER.debug(
"%s Query worked. Exit code: <%s>", self, resp.status_code)
"%s Query %i worked. Exit code: <%s>", self, cmd_id, resp.status_code)
return resp

def command_audio(self, cmd, file_content, http_header,
Expand Down

0 comments on commit 8acefe1

Please sign in to comment.