def _http_format_output(self, response_content, response_headers): """Formats the request response to the desired type""" try: if self.output_format == 'text': output = response_content.decode('utf-8', 'ignore') elif self.output_format == 'dict': output = helpers.convert_xml_to_dict(response_content) elif self.output_format == 'json': output = helpers.convert_xml_to_json(response_content) elif self.output_format == 'xml': output = helpers.parse_xml(response_content) else: output = response_content if self.callback: return self.callback(output) if self.return_type: return output, response_headers['Content-Type'] return output except Exception as e: if not self._silent: logger.warn( u"Failed format response from uri %s to %s error %s" % (self.uri, self.output_format, e)) return None
def make_request(self, uri=None, proto='HTTP', request_type='GET', headers=None, output_format='raw', return_type=False, no_token=False, timeout=None): if timeout is None: timeout = plexpy.CONFIG.PMS_TIMEOUT valid_request_types = ['GET', 'POST', 'PUT', 'DELETE'] if request_type.upper() not in valid_request_types: logger.debug( u"HTTP request made but unsupported request type given.") return None if uri: if proto.upper() == 'HTTPS': if not self.ssl_verify and hasattr( ssl, '_create_unverified_context'): context = ssl._create_unverified_context() handler = HTTPSConnection(host=self.host, port=self.port, timeout=timeout, context=context) logger.warn( u"PlexPy HTTP Handler :: Unverified HTTPS request made. This connection is not secure." ) else: handler = HTTPSConnection(host=self.host, port=self.port, timeout=timeout) else: handler = HTTPConnection(host=self.host, port=self.port, timeout=timeout) if not no_token: if headers: headers.update({'X-Plex-Token': self.token}) else: headers = {'X-Plex-Token': self.token} try: if headers: handler.request(request_type, uri, headers=headers) else: handler.request(request_type, uri) response = handler.getresponse() request_status = response.status request_content = response.read() content_type = response.getheader('content-type') except IOError as e: logger.warn(u"Failed to access uri endpoint %s with error %s" % (uri, e)) return None except Exception as e: logger.warn( u"Failed to access uri endpoint %s. Is your server maybe accepting SSL connections only? %s" % (uri, e)) return None except: logger.warn( u"Failed to access uri endpoint %s with Uncaught exception." % uri) return None if request_status in (200, 201): try: if output_format == 'dict': output = helpers.convert_xml_to_dict(request_content) elif output_format == 'json': output = helpers.convert_xml_to_json(request_content) elif output_format == 'xml': output = helpers.parse_xml(request_content) else: output = request_content if return_type: return output, content_type return output except Exception as e: logger.warn( u"Failed format response from uri %s to %s error %s" % (uri, output_format, e)) return None else: logger.warn( u"Failed to access uri endpoint %s. Status code %r" % (uri, request_status)) return None else: logger.debug(u"HTTP request made but no enpoint given.") return None
def make_request( self, uri=None, proto="HTTP", request_type="GET", headers=None, output_format="raw", return_type=False, no_token=False, timeout=None, ): if timeout is None: timeout = plexpy.CONFIG.PMS_TIMEOUT valid_request_types = ["GET", "POST", "PUT", "DELETE"] if request_type.upper() not in valid_request_types: logger.debug(u"HTTP request made but unsupported request type given.") return None if uri: if proto.upper() == "HTTPS": if not self.ssl_verify and hasattr(ssl, "_create_unverified_context"): context = ssl._create_unverified_context() handler = HTTPSConnection(host=self.host, port=self.port, timeout=timeout, context=context) logger.warn(u"PlexPy HTTP Handler :: Unverified HTTPS request made. This connection is not secure.") else: handler = HTTPSConnection(host=self.host, port=self.port, timeout=timeout) else: handler = HTTPConnection(host=self.host, port=self.port, timeout=timeout) if not no_token: if headers: headers.update({"X-Plex-Token": self.token}) else: headers = {"X-Plex-Token": self.token} try: if headers: handler.request(request_type, uri, headers=headers) else: handler.request(request_type, uri) response = handler.getresponse() request_status = response.status request_content = response.read() content_type = response.getheader("content-type") except IOError as e: logger.warn(u"Failed to access uri endpoint %s with error %s" % (uri, e)) return None except Exception as e: logger.warn( u"Failed to access uri endpoint %s. Is your server maybe accepting SSL connections only? %s" % (uri, e) ) return None except: logger.warn(u"Failed to access uri endpoint %s with Uncaught exception." % uri) return None if request_status in (200, 201): try: if output_format == "dict": output = helpers.convert_xml_to_dict(request_content) elif output_format == "json": output = helpers.convert_xml_to_json(request_content) elif output_format == "xml": output = helpers.parse_xml(request_content) else: output = request_content if return_type: return output, content_type return output except Exception as e: logger.warn(u"Failed format response from uri %s to %s error %s" % (uri, output_format, e)) return None else: logger.warn(u"Failed to access uri endpoint %s. Status code %r" % (uri, request_status)) return None else: logger.debug(u"HTTP request made but no enpoint given.") return None