def http_response(self, response, url, callback): if response.code == 200: if callback is not None: treq.text_content(response).addCallbacks(callback) else: anode.Log(logging.ERROR).log("Plugin", "error", lambda: "[{}] error processing HTTP response [{}] with [{}]".format(self.name, url, response.code))
def run_balance_test(self, user=None, default_route=None, side_effect=None): yield self.connect('127.0.0.1', self.pbPort) yield self.prepareRoutingsAndStartConnector(user, default_route, side_effect) # Set baseurl params = { 'username': self.params['username'], 'password': self.params['password'], } baseurl = 'http://127.0.0.1:1401/balance' # Send a balance check request agent = Agent(reactor) client = HTTPClient(agent) response = yield client.get(baseurl, params=params) response_text = yield text_content(response) response_code = response.code # Wait 5 seconds before stopping SmppClientConnectors yield waitFor(5) yield self.stopSmppClientConnectors() defer.returnValue((response_text, response_code))
def _read_response(response): logger.info("Received Data: %s", response) if response.code != 200: logger.info("Received Data with error") raise MailChimpApiError("{}".format(response.phrase)) return treq.text_content(response).addCallback(json.loads)
def test_rate_interceptorpb_not_connected(self): _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/rate' params = { 'to': '06155423', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.get(url, params=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Asserts self.assertEqual(lastErrorStatus, 503) self.assertEqual(lastResponse, '"InterceptorPB not connected !"') self.assertEqual(_ic, self.stats_http.get('interceptor_count')) self.assertEqual(_iec + 1, self.stats_http.get('interceptor_error_count'))
def create(self, path='/'): self.data['address'] = str(self.data['address']) self.data['address_obj'] = urlparse(self.data['address']) # Build the necessary request headers for contacting the HTTP resource options = self.data['options'] if 'auth_method' in options: if options['auth_method'].lower() == 'basic' and options['auth_username'] and options['auth_password']: self._treq_options['auth'] = (options['auth_username'], options['auth_password']) if 'timeout' in options: try: self._treq_options['timeout'] = int(options['timeout']) except: pass if 'user-agent' in options: self._treq_options['headers'] = {'User-Agent': [str(options['user-agent'])]} # Fetch the first page and determine which template parser is needed resp = yield request(method='GET', url=self._buildURL(path), **self._treq_options) if resp.code != 200: raise CrawlException('Response code was not 200: %s' % str(resp.code)) self._resp_headers = dict((key.lower(), value[0].lower()) for (key, value) in resp.headers._rawHeaders.items()) response_body = yield text_content(resp) self._indexParser = HttpParse().get_parser(response_headers=self._resp_headers, response_body=response_body) if self._indexParser: defer.returnValue(True) defer.returnValue(False)
def test_rate_syntax_error(self): _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/rate' params = { 'to': '06155423', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.get(url, params=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Asserts self.assertEqual(lastErrorStatus, 400) self.assertEqual( lastResponse, '"Failed running interception script, check log for details"') self.assertEqual(_ic, self.stats_http.get('interceptor_count')) self.assertEqual(_iec + 1, self.stats_http.get('interceptor_error_count'))
def test_rate_success(self): _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript(self.update_message_sript) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/rate' params = { 'to': '06155423', 'username': self.u1.username, 'password': self.u1_password } # We should receive an error since interceptorpb is not connected agent = Agent(reactor) client = HTTPClient(agent) response = yield client.get(url, params=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Asserts self.assertEqual(lastErrorStatus, 200) self.assertEqual(_ic + 1, self.stats_http.get('interceptor_count')) self.assertEqual(_iec, self.stats_http.get('interceptor_error_count'))
def run_rate_test(self, user=None, content=None, source_address=None, destination_address=None, default_route=None, side_effect=None): yield self.connect('127.0.0.1', self.pbPort) yield self.prepareRoutingsAndStartConnector(user, default_route, side_effect) # Set content if content is not None: self.params['content'] = content else: del self.params['content'] if source_address is not None: self.params['from'] = source_address if destination_address is not None: self.params['to'] = destination_address baseurl = 'http://127.0.0.1:1401/rate' # Send a MT # We should receive a msg id agent = Agent(reactor) client = HTTPClient(agent) response = yield client.get(baseurl, params=self.params) response_text = yield text_content(response) response_code = response.code # Wait 5 seconds before stopping SmppClientConnectors yield waitFor(5) yield self.stopSmppClientConnectors() defer.returnValue((response_text, response_code))
def run_test(self, content, datacoding=None, port=1401): yield self.connect('127.0.0.1', self.pbPort) yield self.prepareRoutingsAndStartConnector() # Set content self.params['content'] = content # Set datacoding if datacoding is None and 'coding' in self.params: del self.params['coding'] if datacoding is not None: self.params['coding'] = datacoding # Prepare baseurl baseurl = 'http://127.0.0.1:%s/send' % port # Send a MT # We should receive a msg id agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(baseurl, data=self.params) text = yield text_content(response) msgStatus = text[:7] # Wait 2 seconds before stopping SmppClientConnectors exitDeferred = defer.Deferred() reactor.callLater(2, exitDeferred.callback, None) yield exitDeferred yield self.stopSmppClientConnectors() # Run tests self.assertEqual(msgStatus, 'Success')
def request_sent(self, response): self._stat['sent'] += 1 d = treq.text_content(response) d.addCallback(self.body_received) d.addErrback(lambda x: None) # ignore errors return d
def handle_response_error(self, error_text, response, *args, **kwargs): d = treq.text_content(response).addCallback( self.log_response, u'Error in Response from URL:%s %s' % (self.url, error_text), log.err, ) d.addCallback(self.retry_request)
def test_text_content_default_encoding_no_param(self): self.response.headers = Headers({'Content-Type': ['text/plain']}) d = text_content(self.response) self.protocol.dataReceived('\xa1') self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'\xa1')
def test_text_content(self): self.response.headers = Headers({b"Content-Type": [b"text/plain; charset=utf-8"]}) d = text_content(self.response) self.protocol.dataReceived(b"\xe2\x98\x83") self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u"\u2603")
def print_response(response): if DEBUG: print() print('---') print(response.code) print(response.headers) text = yield treq.text_content(response) print(text) print('---')
def test_text_content_default_encoding_no_header(self): self.response.headers = Headers() d = text_content(self.response) self.protocol.dataReceived('\xa1') self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'\xa1')
def test_text_content_default_encoding_no_param(self): self.response.headers = Headers({b"Content-Type": [b"text/plain"]}) d = text_content(self.response) self.protocol.dataReceived(b"\xa1") self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u"\xa1")
def print_response(response): if DEBUG: print print '---' print response.code print response.headers text = yield treq.text_content(response) print text print '---'
def test_text_content_default_encoding_no_header(self): self.response.headers = Headers() d = text_content(self.response) self.protocol.dataReceived(b'\xa1') self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'\xa1')
def test_content_application_json_default_encoding(self): self.response.headers = Headers( {b'Content-Type': [b'application/json']}) d = text_content(self.response) self.protocol.dataReceived(b'gr\xc3\xbcn') self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'grün')
def test_text_content_default_encoding_no_param(self): self.response.headers = Headers( {'Content-Type': ['text/plain']}) d = text_content(self.response) self.protocol.dataReceived('\xa1') self.protocol.connectionLost(Failure(ResponseDone())) self.successResultOf(d, u'\xa1')
def test_text_content(self): self.response.headers = Headers( {b'Content-Type': [b'text/plain; charset=utf-8']}) d = text_content(self.response) self.protocol.dataReceived(b'\xe2\x98\x83') self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'\u2603')
def test_text_content(self): self.response.headers = Headers( {'Content-Type': ['text/plain; charset=utf-8']}) d = text_content(self.response) self.protocol.dataReceived('\xe2\x98\x83') self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'\u2603')
def _fetch_json_thread_headers_success(self, resp): """ Here will I arrive when fetching the json information has been successful and the body needs to be retrieved. treq.text_content does that for us, that has to do with the API of Agents. """ self._update_header_dict(resp) json_url = resp.request.original.uri d = treq.text_content(resp) d.addCallback(self._fetch_json_thread_body_success, json_url)
def run_send_test(self, user=None, content='anycontent', hex_content=None, dlr_level=None, dlr_method=None, source_address=None, priority=None, schedule_delivery_time=None, validity_period=None, destination_address=None, default_route=None, side_effect=None): yield self.connect('127.0.0.1', self.pbPort) yield self.prepareRoutingsAndStartConnector(user, default_route, side_effect) # Set params if content is None: del (self.params['content']) else: self.params['content'] = content if hex_content is not None: self.params['hex-content'] = hex_content if dlr_level is not None: self.params['dlr-level'] = dlr_level if dlr_method is not None: self.params['dlr-method'] = dlr_method if source_address is not None: self.params['from'] = source_address if priority is not None: self.params['priority'] = priority if schedule_delivery_time is not None: self.params['sdt'] = schedule_delivery_time if validity_period is not None: self.params['validity-period'] = validity_period if destination_address is not None: self.params['to'] = destination_address baseurl = 'http://127.0.0.1:1401/send' # Send a MT # We should receive a msg id agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(baseurl, data=self.params) response_text = yield text_content(response) response_code = response.code # Wait 5 seconds before stopping SmppClientConnectors yield waitFor(5) yield self.stopSmppClientConnectors() defer.returnValue((response_text, response_code))
def upload_data(self, src, dst): proxy_id = dst['proxy_id'] proxy_addr = yield get_proxy(proxy_id) if proxy_addr: # proxy addr format: (ip, ctrl_port, file_port, stream_ws_port, stream_restful_port) url = 'https://%s:%d' % (str(proxy_addr[0]), int(proxy_addr[2])) data = open(src, 'rb') resp = yield treq.post(url, agent=no_verify_agent(), data=data, stream=True) if resp: file_path = yield treq.text_content(resp) file_url = '%s/%s' % (url, file_path) return file_url
def writeZeroPoint(self, zero_point): ''' Writes Zero Point to the device. Asynchronous operation ''' result = {} result['tstamp'] = datetime.datetime.utcnow().replace( microsecond=0) + datetime.timedelta(seconds=0.5) url = make_save_url(self.httpEndPoint) self.log.info("==> TESS-W [HTTP GET] {url}", url=url) params = [('cons', '{0:0.2f}'.format(zero_point))] resp = yield treq.get(url, params=params, timeout=4) text = yield treq.text_content(resp) self.log.info("<== TESS-W [HTTP GET] {url}", url=url) matchobj = GET_INFO['flash'].search(text) result['zp'] = float(matchobj.groups(1)[0]) returnValue(result)
def test_text_content_unicode_headers(self): """ Header parsing is robust against unicode header names and values. """ self.response.headers = Headers({ b'Content-Type': [u'text/plain; charset="UTF-16BE"; u=ᛃ'.encode('utf-8')], u'Coördination'.encode('iso-8859-1'): [u'koʊˌɔrdɪˈneɪʃən'.encode('utf-8')], }) d = text_content(self.response) self.protocol.dataReceived(u'ᚠᚡ'.encode('UTF-16BE')) self.protocol.connectionLost(Failure(ResponseDone())) self.assertEqual(self.successResultOf(d), u'ᚠᚡ')
def test_throughput_limit_rejection(self): user = copy.copy(self.user1) user.mt_credential.setQuota('http_throughput', 2) route = DefaultRoute(self.c1, rate=0.0) yield self.connect('127.0.0.1', self.pbPort) yield self.prepareRoutingsAndStartConnector(user, route) # Set content self.params['content'] = 'Any Content' baseurl = 'http://127.0.0.1:1401/send' # Send a bunch of MT messages # We should receive a msg id for success and error when throughput is exceeded start_time = datetime.now() throughput_exceeded_errors = 0 request_counter = 0 for x in range(5000): agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(baseurl, data=self.params) response_text = yield text_content(response) response_code = response.code request_counter += 1 if response_code == 403 and response_text == 'Error "User throughput exceeded"': throughput_exceeded_errors += 1 end_time = datetime.now() # Wait 2 seconds before stopping SmppClientConnectors yield waitFor(2) yield self.stopSmppClientConnectors() # Asserts (tolerance of -/+ 3 messages) throughput = 1 / float(user.mt_credential.getQuota('http_throughput')) dt = end_time - start_time max_unsuccessfull_requests = request_counter - (dt.seconds / throughput) unsuccessfull_requests = throughput_exceeded_errors self.assertGreaterEqual(unsuccessfull_requests, max_unsuccessfull_requests - 3) self.assertLessEqual(unsuccessfull_requests, max_unsuccessfull_requests + 3)
def _make_request(self, url, **kwargs): """Make the API call and return the response. This is separated into it's own function, so we can mock it easily for testing. """ try: res = yield tx_req(url=url, **kwargs) code = res.code content = yield text_content(res, encoding='utf-8') ret = { 'body': content, 'code': code, 'headers': {h[0]: h[1] for h in res.headers.getAllRawHeaders()} } if not 200 <= code < 300: raise HTTPError(**ret) return ret except HTTPError as err: exc = handle_error(err) exc.__cause__ = None raise exc
def test_tagging(self): """Refs #495 Will tag message inside interceptor script and assert routing based tagfilter were correctly done """ # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript("routable.addTag(10)") yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Change routing rules by shadowing (high order value) default route with a # static route having a tagfilter yield self.mtroute_flush() yield self.mtroute_add(StaticMTRoute([TagFilter(10)], self.c1, 0.0), 1000) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/send' params = { 'to': '06155423', 'content': 'test', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(url, data=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 200) self.assertEqual(1, len(self.SMSCPort.factory.lastClient.submitRecords))
def getDirectory(self, directory): path = '' if isinstance(directory, (str, unicode)): path = directory elif isinstance(directory, DiscoveredFile): path = directory.file_path + directory.file_name print path resp = yield request(method='GET', url=self._buildURL(path=path), **self._treq_options) if resp.code != 200: print resp.code defer.returnValue(None) content = yield text_content(resp) data = [] items = self._indexParser(content) if not items: defer.returnValue(None) for item in items: if item['isdir'] and item['url'].endswith('/'): item['url'] = item['url'][:-1] if not path.endswith('/'): path += '/' data.append(DiscoveredFile( resource_id=self.data['resource_id'], file_path=path, file_name=item['url'], file_isdir=item['isdir'], file_size=item['size'], file_modified=item['modified'], file_perm=None )) defer.returnValue(data)
def test_send_success(self): _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript(self.update_message_sript) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/send' params = { 'to': '06155423', 'content': 'test', 'username': self.u1.username, 'password': self.u1_password } # We should receive an error since interceptorpb is not connected agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(url, data=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 200) self.assertEqual(1, len(self.SMSCPort.factory.lastClient.submitRecords)) self.assertEqual( b'Intercepted message', self.SMSCPort.factory.lastClient. submitRecords[0].params['short_message']) self.assertEqual(_ic + 1, self.stats_http.get('interceptor_count')) self.assertEqual(_iec, self.stats_http.get('interceptor_error_count'))
def readPhotometerInfo(self): ''' Reads Info from the device. Asynchronous operation ''' result = {} result['tstamp'] = datetime.datetime.utcnow().replace( microsecond=0) + datetime.timedelta(seconds=0.5) url = make_state_url(self.httpEndPoint) self.log.info("==> TESS-W [HTTP GET] {url}", url=url) resp = yield treq.get(url, timeout=4) text = yield treq.text_content(resp) self.log.info("<== TESS-W [HTTP GET] {url}", url=url) matchobj = GET_INFO['name'].search(text) result['name'] = matchobj.groups(1)[0] matchobj = GET_INFO['mac'].search(text) result['mac'] = matchobj.groups(1)[0] matchobj = GET_INFO['zp'].search(text) result['zp'] = float(matchobj.groups(1)[0]) matchobj = GET_INFO['firmware'].search(text) result['firmware'] = matchobj.groups(1)[0] returnValue(result)
def test_send_ESME_RINVESMCLASS_from_script(self): "Will ensure if script defines only smpp error it will implicitly cause a http 520 error" _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript(self.return_ESME_RINVESMCLASS) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/send' params = { 'to': '06155423', 'content': 'test', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(url, data=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 520) self.assertEqual(lastResponse, 'Error "Interception specific error code 520"') self.assertEqual(_ic, self.stats_http.get('interceptor_count')) self.assertEqual(_iec + 1, self.stats_http.get('interceptor_error_count'))
def test_send_any_exception_from_script(self): _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript(self.raise_any_exception) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/send' params = { 'to': '06155423', 'content': 'test', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(url, data=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 400) self.assertEqual( lastResponse, 'Error "Failed running interception script, check log for details"' ) self.assertEqual(_ic, self.stats_http.get('interceptor_count')) self.assertEqual(_iec + 1, self.stats_http.get('interceptor_error_count'))
def test_send_with_tags(self): """Related to #455 Will send message through http api using tags and then assert for getting the tags into the short_message """ # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript( self.update_message_from_tags_sript) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/send' params = { 'to': '06155423', 'content': 'temporary', 'username': self.u1.username, 'password': self.u1_password, 'tags': '123,456' } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(url, data=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 200) self.assertEqual(1, len(self.SMSCPort.factory.lastClient.submitRecords)) self.assertEqual( b"['123', '456']", self.SMSCPort.factory.lastClient. submitRecords[0].params['short_message'])
def test_send_and_lock_param(self): """Related to #458 Will set and lock sm_default_msg_id inside interceptor and assert it were kept as set. """ # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript(self.lock_param_script) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/send' params = { 'to': '06155423', 'content': 'temporary', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.post(url, data=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 200) self.assertEqual(1, len(self.SMSCPort.factory.lastClient.submitRecords)) self.assertEqual( 10, self.SMSCPort.factory.lastClient.submitRecords[0]. params['sm_default_msg_id'])
def test_rate_HTTP_300_from_script(self): _ic = self.stats_http.get('interceptor_count') _iec = self.stats_http.get('interceptor_error_count') # Re-provision interceptor with correct script mt_interceptor = MTInterceptorScript(self.return_HTTP_300) yield self.mtinterceptor_add(DefaultInterceptor(mt_interceptor), 0) # Connect to InterceptorPB yield self.ipb_connect() # Send a SMS MT through http interface url = 'http://127.0.0.1:1401/rate' params = { 'to': '06155423', 'username': self.u1.username, 'password': self.u1_password } agent = Agent(reactor) client = HTTPClient(agent) response = yield client.get(url, params=params) lastErrorStatus = response.code lastResponse = yield text_content(response) # Wait some time for message delivery through smppc yield waitFor(2) # Asserts self.assertEqual(lastErrorStatus, 300) self.assertEqual(lastResponse, '"Interception specific error code 300"') self.assertEqual(_ic, self.stats_http.get('interceptor_count')) self.assertEqual(_iec + 1, self.stats_http.get('interceptor_error_count'))
def display(response): return treq.text_content(response).addCallback(print)
def print_response(response): print(response.code, response.phrase) print(response.headers) return treq.text_content(response).addCallback(print)
def parse(cls, response): content = yield treq.text_content(response) json_content = json.loads(content) content = json.dumps(json_content, ensure_ascii=False, indent=2) defer.returnValue(content.encode("utf-8"))
def http_dlr_callback(self, message): msgid = message.content.properties['message-id'] url = message.content.properties['headers']['url'] method = message.content.properties['headers']['method'] level = message.content.properties['headers']['level'] self.log.debug('Got one message (msgid:%s) to throw', msgid) # If any, clear requeuing timer self.clearRequeueTimer(msgid) # Build mandatory arguments args = { 'id': msgid, 'level': level, 'message_status': message.content.properties['headers']['message_status'], 'connector': message.content.properties['headers']['connector'] } # Level 2 extra args if level in [2, 3]: args['id_smsc'] = message.content.properties['headers']['id_smsc'] args['sub'] = message.content.properties['headers']['sub'] args['dlvrd'] = message.content.properties['headers']['dlvrd'] args['subdate'] = message.content.properties['headers']['subdate'] args['donedate'] = message.content.properties['headers']['donedate'] args['err'] = message.content.properties['headers']['err'] args['text'] = message.content.properties['headers']['text'] try: # Throw the message to http endpoint postdata = None params = None baseurl = url if method == 'GET': params = args else: postdata = args self.log.debug('Calling %s with args %s using %s method.', baseurl, args, method) agent = Agent(reactor) client = HTTPClient(agent) response = yield client.request( method, baseurl, params=params, data=postdata, timeout=self.config.timeout, agent='Jasmin gateway/1.0 %s' % self.name, headers={'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}) self.log.info('Throwed DLR [msgid:%s] to %s.', msgid, baseurl) content = yield text_content(response) if response.code >= 400: raise HttpApiError(response.code, content) self.log.debug('Destination end replied to message [msgid:%s]: %r', msgid, content) # Check for acknowledgement if content.strip() != 'ACK': raise MessageAcknowledgementError( 'Destination end did not acknowledge receipt of the DLR message.') # Everything is okay ? then: yield self.ackMessage(message) except Exception as e: self.log.error('Throwing HTTP/DLR [msgid:%s] to (%s): %r.', msgid, baseurl, e) # List of errors after which, no further retrying shall be made noRetryErrors = ['404'] # Requeue message for later retry if (str(e) not in noRetryErrors and self.getThrowingRetrials(message) <= self.config.max_retries): self.log.debug('Message try-count is %s [msgid:%s]: requeuing', self.getThrowingRetrials(message), msgid) yield self.rejectAndRequeueMessage(message) elif str(e) in noRetryErrors: self.log.warn('Message is no more processed after receiving "%s" error', str(e)) yield self.rejectMessage(message) else: self.log.warn('Message try-count is %s [msgid:%s]: purged from queue', self.getThrowingRetrials(message), msgid) yield self.rejectMessage(message)