Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling for authentication and no longer use session for retries #141

Merged
merged 1 commit into from
Jan 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 21 additions & 18 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)
_LOGGER.debug("%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