def __init__(self, doGzip): self.__buf = BytesIO() if doGzip: self.__gzip = gzip.GzipFile(mode='wb', fileobj=self.__buf) stm = self.__gzip else: stm = self.__buf self.__gzip = None self.xg = BeatBoxXmlGenerator(stm, "utf-8") self.xg.startDocument() self.__elems = []
def test_gzip(self): # note this doesn't use self.w as that's not configured to zip w = beatbox.XmlWriter(True) w.startPrefixMapping("q", "urn:test") w.startElement("urn:test", "root") w.writeStringElement("urn:test", "child", "text") w.endElement() zipped = w.endDocument() gz = gzip.GzipFile(fileobj=BytesIO(zipped)) xml = gz.read() self.assertEqual(b'<?xml version="1.0" encoding="utf-8"?>\n' b'<q:root xmlns:q="urn:test"><q:child>text</q:child></q:root>', xml)
def post(self, conn=None, alwaysReturnList=False): """Complete the envelope and send the request does all the grunt work, serializes the request, makes a http request, passes the response to tramp checks for soap fault todo: check for mU='1' headers returns the relevant result from the body child """ headers = { "User-Agent": "BeatBox/" + __version__, "SOAPAction": '""', "Content-Type": "text/xml; charset=utf-8" } if beatbox.gzipResponse: headers['accept-encoding'] = 'gzip' if beatbox.gzipRequest: headers['content-encoding'] = 'gzip' close = False (scheme, host, path, params, query, frag) = urlparse(self.serverUrl) if conn is None: conn = makeConnection(scheme, host) close = True rawRequest = self.makeEnvelope() # print(rawRequest) conn.request("POST", self.serverUrl, rawRequest, headers) response = conn.getresponse() rawResponse = response.read() if response.getheader('content-encoding', '') == 'gzip': rawResponse = gzip.GzipFile(fileobj=BytesIO(rawResponse)).read() if close: conn.close() tramp = xmltramp.parse(rawResponse) try: faultString = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultstring) faultCode = str( tramp[_tSoapNS.Body][_tSoapNS.Fault].faultcode).split(':')[-1] raise SoapFaultError(faultCode, faultString) except KeyError: pass # first child of body is XXXXResponse result = tramp[_tSoapNS.Body][0] # it contains either a single child, or for a batch call multiple children if alwaysReturnList or len(result) > 1: return result[:] else: return result[0]