def CallRawMethod(self, soap_message): """Make an API call by posting raw SOAP XML message. Args: soap_message: str SOAP XML message. Returns: tuple response from the API method. """ # Acquire thread lock. self.__lock.acquire() try: buf = SoapBuffer( xml_parser=self.__config['xml_parser'], pretty_xml=Utils.BoolTypeConvert(self.__config['use_pretty_xml'])) http_header = { 'post': '%s' % self.__url, 'host': 'sandbox.google.com', 'user_agent': '%s v%s; WebService.py' % (LIB_SHORT_NAME, LIB_VERSION), 'content_type': 'text/xml; charset=\"UTF-8\"', 'content_length': '%d' % len(soap_message), 'soap_action': '' } version = self.__url.split('/')[-2] if SanityCheck.IsNewApi(version): http_header['host'] = 'adwords-%s' % http_header['host'] index = self.__url.find('adwords.google.com') if index > -1: http_header['host'] = 'adwords.google.com' self.__url = ''.join(['https://', http_header['host'], self.__url]) start_time = time.strftime('%Y-%m-%d %H:%M:%S') buf.write( ('%s Outgoing HTTP headers %s\nPOST %s\nHost: %s\nUser-Agent: ' '%s\nContent-type: %s\nContent-length: %s\nSOAPAction: %s\n%s\n%s ' 'Outgoing SOAP %s\n%s\n%s\n' % ('*'*3, '*'*46, http_header['post'], http_header['host'], http_header['user_agent'], http_header['content_type'], http_header['content_length'], http_header['soap_action'], '*'*72, '*'*3, '*'*54, soap_message, '*'*72))) # Construct header and send SOAP message. web_service = httplib.HTTPS(http_header['host']) web_service.putrequest('POST', http_header['post']) web_service.putheader('Host', http_header['host']) web_service.putheader('User-Agent', http_header['user_agent']) web_service.putheader('Content-type', http_header['content_type']) web_service.putheader('Content-length', http_header['content_length']) web_service.putheader('SOAPAction', http_header['soap_action']) web_service.endheaders() web_service.send(soap_message) # Get response. status_code, status_message, header = web_service.getreply() response = web_service.getfile().read() header = str(header).replace('\r', '') buf.write(('%s Incoming HTTP headers %s\n%s %s\n%s\n%s\n%s Incoming SOAP' ' %s\n%s\n%s\n' % ('*'*3, '*'*46, status_code, status_message, header, '*'*72, '*'*3, '*'*54, response, '*'*72))) stop_time = time.strftime('%Y-%m-%d %H:%M:%S') # Catch local errors prior to going down to the SOAP layer, which may not # exist for this error instance. if not buf.IsHandshakeComplete() or not buf.IsSoap(): # The buffer contains non-XML data, most likely an HTML page. This # happens in the case of 502 errors. html_error = Utils.GetErrorFromHtml(buf.GetBufferAsStr()) if html_error: msg = '%s' % html_error else: msg = 'Unknown error.' raise Error(msg) self.__ManageSoap(buf, start_time, stop_time, {'data': buf.GetBufferAsStr()}) finally: # Release thread lock. if self.__lock.locked(): self.__lock.release() return (response,)
def testMalformedBuffer(self): """Tests whether we can handle a malformed SOAP buffer.""" buf = SoapBuffer() buf.write('JUNK') self.assertRaises(MalformedBufferError, buf.GetCallResponseTime)