python-plexapi icon indicating copy to clipboard operation
python-plexapi copied to clipboard

Code duplication: query of myplex.py and server.py

Open glensc opened this issue 2 years ago • 0 comments

What is your feature request?

The methods look identical. could the code be reused? mixin? use self._server.query in myplex?

--- server.py	2023-01-11 18:40:11
+++ myplex.py	2023-01-11 18:40:18
@@ -1,15 +1,10 @@
-    def query(self, key, method=None, headers=None, timeout=None, **kwargs):
-        """ Main method used to handle HTTPS requests to the Plex server. This method helps
-            by encoding the response to utf-8 and parsing the returned XML into and
-            ElementTree object. Returns None if no data exists in the response.
-        """
-        url = self.url(key)
+    def query(self, url, method=None, headers=None, timeout=None, **kwargs):
         method = method or self._session.get
         timeout = timeout or TIMEOUT
-        log.debug('%s %s', method.__name__.upper(), url)
+        log.debug('%s %s %s', method.__name__.upper(), url, kwargs.get('json', ''))
         headers = self._headers(**headers or {})
         response = method(url, headers=headers, timeout=timeout, **kwargs)
-        if response.status_code not in (200, 201, 204):
+        if response.status_code not in (200, 201, 204):  # pragma: no cover
             codename = codes.get(response.status_code)[0]
             errtext = response.text.replace('\n', ' ')
             message = f'({response.status_code}) {codename}; {response.url} {errtext}'
@@ -19,5 +14,7 @@
                 raise NotFound(message)
             else:
                 raise BadRequest(message)
+        if headers.get('Accept') == 'application/json':
+            return response.json()
         data = response.text.encode('utf8')
         return ElementTree.fromstring(data) if data.strip() else None

Are there any workarounds?

No response

Code Snippets

No response

Additional Context

No response

glensc avatar Jan 11 '23 16:01 glensc