def make_request(self, uri=None, proto='HTTP', request_type='GET', headers=None, output_format='raw', return_type=False): 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': handler = HTTPSConnection(self.host, self.port, timeout=10) else: handler = HTTPConnection(self.host, self.port, timeout=10) if uri.find('?') > 0: token_string = '&X-Plex-Token=' + self.token else: token_string = '?X-Plex-Token=' + self.token try: if headers: handler.request(request_type, uri + token_string, headers=headers) else: handler.request(request_type, uri + token_string) response = handler.getresponse() request_status = response.status request_content = response.read() content_type = response.getheader('content-type') except IOError, e: logger.warn(u"Failed to access uri endpoint %s with error %s" % (uri, e)) return None if request_status == 200: 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 else: logger.warn(u"Failed to access uri endpoint %s. Status code %r" % (uri, request_status)) return []
def get_plextv_user_data(self): plextv_response = self.get_plex_auth() if plextv_response: try: user_data = helpers.convert_xml_to_dict(plextv_response) except IOError, e: logger.warn("Error parsing XML for Plex.tv user data: %s" % e) return False return user_data
def make_request( self, uri=None, proto="HTTP", request_type="GET", headers=None, output_format="raw", return_type=False ): 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": handler = HTTPSConnection(self.host, self.port, timeout=10) else: handler = HTTPConnection(self.host, self.port, timeout=10) if uri.find("?") > 0: token_string = "&X-Plex-Token=" + self.token else: token_string = "?X-Plex-Token=" + self.token try: if headers: handler.request(request_type, uri + token_string, headers=headers) else: handler.request(request_type, uri + token_string) response = handler.getresponse() request_status = response.status request_content = response.read() content_type = response.getheader("content-type") except IOError, e: logger.warn(u"Failed to access uri endpoint %s with error %s" % (uri, e)) return None if request_status == 200: 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 else: logger.warn(u"Failed to access uri endpoint %s. Status code %r" % (uri, request_status)) return None
request_content = response.read() content_type = response.getheader('content-type') except IOError, e: logger.warn(u"Failed to access uri endpoint %s with error %s" % (uri, e)) return None except Exception, 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 == 200: 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
return None except Exception, 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 == 200: 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" %