From 482c11dbec3978a7b2a9d800190415276e65b8ef Mon Sep 17 00:00:00 2001 From: l3uddz Date: Fri, 20 Apr 2018 22:03:34 +0100 Subject: [PATCH] ensure server url ends with / --- media/radarr.py | 14 ++++++++------ media/sonarr.py | 17 ++++++++++------- misc/str.py | 7 +++++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/media/radarr.py b/media/radarr.py index bc0c3af..981af7e 100644 --- a/media/radarr.py +++ b/media/radarr.py @@ -1,9 +1,11 @@ +import os.path + import backoff import requests -import os.path -from misc.log import logger from misc import helpers +from misc import str as misc_str +from misc.log import logger log = logger.get_logger(__name__) @@ -27,7 +29,7 @@ class Radarr: try: # request system status to validate api_key req = requests.get( - os.path.join(self.server_url, 'api/system/status'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/system/status'), headers=self.headers, timeout=60 ) @@ -45,7 +47,7 @@ class Radarr: try: # make request req = requests.get( - os.path.join(self.server_url, 'api/movie'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/movie'), headers=self.headers, timeout=60 ) @@ -67,7 +69,7 @@ class Radarr: try: # make request req = requests.get( - os.path.join(self.server_url, 'api/profile'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/profile'), headers=self.headers, timeout=60 ) @@ -111,7 +113,7 @@ class Radarr: # make request req = requests.post( - os.path.join(self.server_url, 'api/movie'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/movie'), headers=self.headers, json=payload, timeout=60 diff --git a/media/sonarr.py b/media/sonarr.py index 9315c62..6caa082 100644 --- a/media/sonarr.py +++ b/media/sonarr.py @@ -1,8 +1,10 @@ +import os.path + import backoff import requests -import os.path from misc import helpers +from misc import str as misc_str from misc.log import logger log = logger.get_logger(__name__) @@ -25,7 +27,8 @@ class Sonarr: def validate_api_key(self): try: # request system status to validate api_key - req = requests.get(os.path.join(self.server_url, 'api/system/status'), headers=self.headers, timeout=60) + req = requests.get(os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/system/status'), + headers=self.headers, timeout=60) log.debug("Request Response: %d", req.status_code) if req.status_code == 200 and 'version' in req.json(): @@ -40,7 +43,7 @@ class Sonarr: try: # make request req = requests.get( - os.path.join(self.server_url, 'api/series'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/series'), headers=self.headers, timeout=60 ) @@ -62,7 +65,7 @@ class Sonarr: try: # make request req = requests.get( - os.path.join(self.server_url, 'api/profile'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/profile'), headers=self.headers, timeout=60 ) @@ -88,7 +91,7 @@ class Sonarr: try: # make request req = requests.get( - os.path.join(self.server_url, 'api/tag'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/tag'), headers=self.headers, timeout=60 ) @@ -115,7 +118,7 @@ class Sonarr: try: # make request req = requests.get( - os.path.join(self.server_url, 'api/tag'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/tag'), headers=self.headers, timeout=60 ) @@ -159,7 +162,7 @@ class Sonarr: # make request req = requests.post( - os.path.join(self.server_url, 'api/series'), + os.path.join(misc_str.ensure_endswith(self.server_url, "/"), 'api/series'), headers=self.headers, json=payload, timeout=60 diff --git a/misc/str.py b/misc/str.py index 91c9cab..e278409 100644 --- a/misc/str.py +++ b/misc/str.py @@ -26,3 +26,10 @@ def is_ascii(string): log.exception(u"Exception checking if %r was ascii: ", string) return False return True + + +def ensure_endswith(data, endswith_key): + if not data.strip().endswith(endswith_key): + return "%s%s" % (data.strip(), endswith_key) + else: + return data