mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-16 02:02:58 -07:00
Refactor notifier requests and better logging
This commit is contained in:
parent
6f0c650a72
commit
dc25ef857f
2 changed files with 132 additions and 59 deletions
|
@ -120,6 +120,88 @@ def request_response(url, method="get", auto_raise=True,
|
|||
logger.error("Request raised exception: %s", e)
|
||||
|
||||
|
||||
def request_response2(url, method="get", auto_raise=True,
|
||||
whitelist_status_code=None, lock=fake_lock, **kwargs):
|
||||
"""
|
||||
Convenient wrapper for `requests.get', which will capture the exceptions
|
||||
and log them. On success, the Response object is returned. In case of a
|
||||
exception, None is returned.
|
||||
|
||||
Additionally, there is support for rate limiting. To use this feature,
|
||||
supply a tuple of (lock, request_limit). The lock is used to make sure no
|
||||
other request with the same lock is executed. The request limit is the
|
||||
minimal time between two requests (and so 1/request_limit is the number of
|
||||
requests per seconds).
|
||||
"""
|
||||
|
||||
# Convert whitelist_status_code to a list if needed
|
||||
if whitelist_status_code and type(whitelist_status_code) != list:
|
||||
whitelist_status_code = [whitelist_status_code]
|
||||
|
||||
# Disable verification of SSL certificates if requested. Note: this could
|
||||
# pose a security issue!
|
||||
kwargs['verify'] = bool(plexpy.CONFIG.VERIFY_SSL_CERT)
|
||||
|
||||
# Map method to the request.XXX method. This is a simple hack, but it
|
||||
# allows requests to apply more magic per method. See lib/requests/api.py.
|
||||
request_method = getattr(requests, method.lower())
|
||||
|
||||
response = None
|
||||
err_msg = http_err = req_msg = None
|
||||
|
||||
try:
|
||||
with lock:
|
||||
response = request_method(url, **kwargs)
|
||||
|
||||
# If status code != OK, then raise exception, except if the status code
|
||||
# is white listed.
|
||||
if whitelist_status_code and auto_raise:
|
||||
if response.status_code not in whitelist_status_code:
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except:
|
||||
raise
|
||||
elif auto_raise:
|
||||
response.raise_for_status()
|
||||
|
||||
except requests.exceptions.SSLError as e:
|
||||
if kwargs["verify"]:
|
||||
err_msg = "Unable to connect to remote host because of a SSL error."
|
||||
else:
|
||||
err_msg = "Unable to connect to remote host because of a SSL error, " \
|
||||
"with certificate verification turned off: {}".format(e)
|
||||
|
||||
except requests.ConnectionError:
|
||||
err_msg = "Unable to connect to remote host. Check if the remote host is up and running."
|
||||
|
||||
except requests.Timeout:
|
||||
err_msg = "Request to the remote host timed out."
|
||||
|
||||
except requests.HTTPError as e:
|
||||
if e.response is not None:
|
||||
if e.response.status_code >= 500:
|
||||
http_err = "[{e.response.status_code}] {e.response.reason} (remote server error).".format(e=e)
|
||||
|
||||
elif e.response.status_code >= 400:
|
||||
http_err = "[{e.response.status_code}] {e.response.reason} (local client error).".format(e=e)
|
||||
|
||||
else:
|
||||
http_err = "Unknown HTTP error."
|
||||
|
||||
err_msg = "Request raised a HTTP error: {}".format(http_err)
|
||||
|
||||
if plexpy.VERBOSE:
|
||||
req_msg = server_message(e.response, return_msg=True)
|
||||
|
||||
else:
|
||||
err_msg = "Request raised a HTTP error: Unknown response."
|
||||
|
||||
except requests.RequestException as e:
|
||||
err_msg = "Request raised an exception: {}".format(e)
|
||||
|
||||
return response, err_msg, req_msg
|
||||
|
||||
|
||||
def request_soup(url, **kwargs):
|
||||
"""
|
||||
Wrapper for `request_response', which will return a BeatifulSoup object if
|
||||
|
@ -195,7 +277,7 @@ def request_feed(url, **kwargs):
|
|||
return feedparser.parse(response.content)
|
||||
|
||||
|
||||
def server_message(response):
|
||||
def server_message(response, return_msg=False):
|
||||
"""
|
||||
Extract server message from response and log in to logger with DEBUG level.
|
||||
|
||||
|
@ -235,4 +317,7 @@ def server_message(response):
|
|||
if len(message) > 150:
|
||||
message = message[:150] + "..."
|
||||
|
||||
if return_msg:
|
||||
return message
|
||||
|
||||
logger.debug("Server responded with message: %s", message)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue