def __str__(self): """ __str__: Magic method: This is meant to produce a human readable representation of the object. This will alter the internal xml slightly in oreder to pretty print the xml. """ pretty_print(self.request_xml.getroot()) return etree.tostring(self.request_xml.getroot())
def __repr__(self): """ __repr__: Magic method: This is meant to produce a machine readable representation of the object. Note, however, that if __str__ was called previously that the xml will have been altered in order to pretty print the xml and this will be pretty printed as well. """ return etree.tostring(self.request_xml.getroot())
def send(self, secure=True): """ send: public function This function sends self.request_xml to self._url using self._creds for authentication and authorization. Currently only basic auth is supported. """ import ssl context = ssl.create_default_context() if not secure: context.check_hostname = False context.verify_mode = ssl.CERT_NONE xml = etree.tostring(self.request_xml.getroot(), encoding="UTF-8") req = urllib2.Request(url=self._url, data=xml) creds = self._credentials.strip() req.add_header('Authorization', 'Basic %s' % (creds)) response_xml = urllib2.urlopen(req, timeout=self._timeout, context=context) response_xml = response_xml.read() return response_xml
def pprint(xml_str): tree = etree.fromstring(xml_str) pretty_print(tree) return etree.tostring(tree)