本文整理汇总了Python中vumi.utils.http_request_full函数的典型用法代码示例。如果您正苦于以下问题:Python http_request_full函数的具体用法?Python http_request_full怎么用?Python http_request_full使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了http_request_full函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_http_basic_auth
def test_http_basic_auth(self):
http_auth_config = self.config.copy()
http_auth_config.update({
'identities': {
'username': 'password',
}
})
transport = yield self.tx_helper.get_transport(http_auth_config)
url = '%s%s?%s' % (
transport.get_transport_url(),
self.config['web_path'],
urlencode({
'to_msisdn': '123',
'from_msisdn': '456',
'message': 'hello',
}))
response = yield http_request_full(url, '', method='GET')
self.assertEqual(response.code, http.UNAUTHORIZED)
self.assertEqual([], self.tx_helper.get_dispatched_inbound())
response = yield http_request_full(url, '', headers={
'Authorization': ['Basic %s' % b64encode('username:password')]
}, method='GET')
self.assertEqual(response.code, http.OK)
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['content'], 'hello')
self.assertEqual(msg['transport_metadata'], {
'http_user': 'username',
})
示例2: test_health_response
def test_health_response(self):
health_url = 'http://%s:%s%s' % (
self.addr.host, self.addr.port, self.config['health_path'])
response = yield http_request_full(health_url, method='GET')
self.assertEqual(response.delivered_body, '0')
yield self.app_helper.make_dispatch_inbound(
'in 1', message_id='1', conv=self.conversation)
queue = DeferredQueue()
stream_url = '%s/%s/messages.json' % (self.url, self.conversation.key)
stream_receiver = self.client.stream(
TransportUserMessage, queue.put, queue.put, stream_url,
Headers(self.auth_headers))
yield queue.get()
response = yield http_request_full(health_url, method='GET')
self.assertEqual(response.delivered_body, '1')
stream_receiver.disconnect()
response = yield http_request_full(health_url, method='GET')
self.assertEqual(response.delivered_body, '0')
self.assertEqual(self.app.client_manager.clients, {
'sphex.stream.message.%s' % (self.conversation.key,): []
})
示例3: test_http_request_full_headers
def test_http_request_full_headers(self):
def check_ua(request):
self.assertEqual('blah', request.getHeader('user-agent'))
return "Yay"
self.set_render(check_ua)
request = yield http_request_full(self.url, '',
{'User-Agent': ['blah']})
self.assertEqual(request.delivered_body, "Yay")
self.assertEqual(request.code, http.OK)
request = yield http_request_full(self.url, '', {'User-Agent': 'blah'})
self.assertEqual(request.delivered_body, "Yay")
self.assertEqual(request.code, http.OK)
示例4: test_missing_parameters
def test_missing_parameters(self):
url = self.mkurl_raw(ServiceNumber='12345', SMSBODY='hello',
USN='user', PWD='pass', Operator='foo')
response = yield http_request_full(url, '', method='GET')
self.assertEqual(400, response.code)
self.assertEqual(json.loads(response.delivered_body),
{'missing_parameter': ['PhoneNumber']})
示例5: test_health_response
def test_health_response(self):
yield self.start_app_worker()
health_url = 'http://%s:%s%s' % (
self.addr.host, self.addr.port, self.config['health_path'])
response = yield http_request_full(health_url, method='GET')
self.assertEqual(response.delivered_body, 'OK')
示例6: test_send_to_within_content_length_limit
def test_send_to_within_content_length_limit(self):
yield self.start_app_worker({
'content_length_limit': 182,
})
msg = {
'content': 'foo',
'to_addr': '+1234',
}
url = '%s/%s/messages.json' % (self.url, self.conversation)
response = yield http_request_full(url, json.dumps(msg),
self.auth_headers, method='PUT')
self.assertEqual(
response.headers.getRawHeaders('content-type'),
['application/json; charset=utf-8'])
put_msg = json.loads(response.delivered_body)
self.assertEqual(response.code, http.OK)
[sent_msg] = self.app_helper.get_dispatched_outbound()
self.assertEqual(sent_msg['to_addr'], put_msg['to_addr'])
self.assertEqual(sent_msg['message_id'], put_msg['message_id'])
self.assertEqual(sent_msg['session_event'], None)
self.assertEqual(sent_msg['to_addr'], '+1234')
self.assertEqual(sent_msg['from_addr'], None)
示例7: test_send_to
def test_send_to(self):
yield self.start_app_worker()
msg = {
'to_addr': '+2345',
'content': 'foo',
'helper_metadata': {'voice': {'foo': 'bar'}},
'session_event': 'new',
'message_id': 'evil_id',
}
url = '%s/%s/messages.json' % (self.url, self.conversation)
response = yield http_request_full(url, json.dumps(msg),
self.auth_headers, method='PUT')
self.assertEqual(response.code, http.OK)
self.assertEqual(
response.headers.getRawHeaders('content-type'),
['application/json; charset=utf-8'])
put_msg = json.loads(response.delivered_body)
[sent_msg] = self.app_helper.get_dispatched_outbound()
self.assertEqual(sent_msg['to_addr'], sent_msg['to_addr'])
# We do not respect the message_id that's been given.
self.assertNotEqual(sent_msg['message_id'], msg['message_id'])
self.assertEqual(sent_msg['message_id'], put_msg['message_id'])
self.assertEqual(sent_msg['to_addr'], msg['to_addr'])
self.assertEqual(sent_msg['from_addr'], None)
self.assertEqual(sent_msg['session_event'], 'new')
self.assertEqual(
sent_msg['helper_metadata'], {'voice': {'foo': 'bar'}})
示例8: handle_outbound_message
def handle_outbound_message(self, message):
headers = {
'Content-Type': 'application/json; charset=utf-8',
}
headers.update(self.get_auth_headers())
params = {
'to_addr': message['to_addr'],
'content': message['content'],
'message_id': message['message_id'],
'in_reply_to': message['in_reply_to'],
'session_event': message['session_event']
}
if 'helper_metadata' in message:
params['helper_metadata'] = message['helper_metadata']
resp = yield http_request_full(
self.get_url('messages.json'),
data=json.dumps(params).encode('utf-8'),
headers=headers,
method='PUT')
if resp.code != http.OK:
log.warning('Unexpected status code: %s, body: %s' % (
resp.code, resp.delivered_body))
yield self.publish_nack(message['message_id'],
reason='Unexpected status code: %s' % (
resp.code,))
return
remote_message = json.loads(resp.delivered_body)
yield self.map_message_id(
remote_message['message_id'], message['message_id'])
yield self.publish_ack(user_message_id=message['message_id'],
sent_message_id=remote_message['message_id'])
示例9: test_unicode_rendering
def test_unicode_rendering(self):
resp_d = http_request_full(self.url, headers=self.sample_req_headers)
[msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
self.tx_helper.make_dispatch_reply(msg, self.sample_unicode_menu_resp)
resp = yield resp_d
self.assertTrue("Hell\xc3\xb8" in resp.delivered_body)
self.assertTrue("\xc3\xb8pti\xc3\xb8n 1" in resp.delivered_body)
示例10: test_cleanup_as_seen_in_production
def test_cleanup_as_seen_in_production(self):
"""what's a technical spec between friends?"""
yield self.session_manager.create_session('13697502734175597',
to_addr='*167*7#', from_addr='254XXXXXXXXX')
query_string = ("msisdn=254XXXXXXXXX&clean=cleann&error=523"
"&SessionID=13697502734175597&MSC=254XXXXXXXXX"
"&=&=en&=9031510005344&=&=&=postpaid"
"&=20130528171235405&=200220130528171113956582"
"&userid=%s&password=%s" % (self.airtel_username,
self.airtel_password))
response = yield http_request_full(
'%s?%s' % (self.transport_url, query_string),
data='', method='GET')
self.assertEqual(response.code, http.OK)
self.assertEqual(response.delivered_body, '')
[msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
self.assertEqual(msg['session_event'],
TransportUserMessage.SESSION_CLOSE)
self.assertEqual(msg['to_addr'], '*167*7#')
self.assertEqual(msg['from_addr'], '254XXXXXXXXX')
self.assertEqual(msg['transport_metadata'], {
'airtel': {
'clean': 'cleann',
'error': '523',
}
})
示例11: request_new_access_token
def request_new_access_token(self):
config = self.get_static_config()
response = yield http_request_full(
self.make_url(
"token",
{"grant_type": "client_credential", "appid": config.wechat_appid, "secret": config.wechat_secret},
),
method="GET",
)
if not http_ok(response):
raise WeChatApiException(
("Received HTTP status code %r when " "requesting access token.") % (response.code,)
)
data = json.loads(response.delivered_body)
if "errcode" in data:
raise WeChatApiException(
"Error when requesting access token. " "Errcode: %(errcode)s, Errmsg: %(errmsg)s." % data
)
# make sure we're always ahead of the WeChat expiry
access_token = data["access_token"]
expiry = int(data["expires_in"]) * 0.90
yield self.redis.setex(self.ACCESS_TOKEN_KEY, int(expiry), access_token)
returnValue(access_token)
示例12: push
def push(self, url, vumi_message):
config = self.get_static_config()
data = vumi_message.to_json().encode('utf-8')
try:
auth, url = extract_auth_from_url(url.encode('utf-8'))
headers = {
'Content-Type': 'application/json; charset=utf-8',
}
if auth is not None:
username, password = auth
if username is None:
username = ''
if password is None:
password = ''
headers.update({
'Authorization': 'Basic %s' % (
base64.b64encode('%s:%s' % (username, password)),)
})
resp = yield http_request_full(
url, data=data, headers=headers, timeout=config.timeout)
if not (200 <= resp.code < 300):
# We didn't get a 2xx response.
log.warning('Got unexpected response code %s from %s' % (
resp.code, url))
except SchemeNotSupported:
log.warning('Unsupported scheme for URL: %s' % (url,))
except HttpTimeoutError:
log.warning("Timeout pushing message to %s" % (url,))
except DNSLookupError:
log.warning("DNS lookup error pushing message to %s" % (url,))
except ConnectionRefusedError:
log.warning("Connection refused pushing message to %s" % (url,))
示例13: handle_outbound_message
def handle_outbound_message(self, message):
log.msg("Outbound message to be processed %s" % repr(message))
params = {
'ybsacctno': self.config['ybsacctno'],
'password': self.config['password'],
'origin': message['from_addr'],
'sms_content': message['content'],
'destinations': self.phone_format_to_yo(message['to_addr']),
}
log.msg('Hitting %s with %s' % (self.config['url'], urlencode(params)))
try:
response = yield http_request_full(
"%s?%s" % (self.config['url'], urlencode(params)),
"",
{'User-Agent': ['Vumi Yo Transport'],
'Content-Type': ['application/json;charset=UTF-8'], },
'GET')
except ConnectionRefusedError:
log.msg("Connection failed sending message: %s" % message)
#raise TemporaryFailure('connection refused')
except Exception as ex:
log.msg("Unexpected error %s" % repr(ex))
if response.code != 200:
log.msg("Http Error %s: %s"
% (response.code, response.delivered_body))
yield self.publish_delivery_report(
user_message_id=message['message_id'],
delivery_status='failed',
failure_level='http',
failure_code=response.code,
failure_reason=response.delivered_body
)
return
try:
response_attr = parse_qs(unquote(response.delivered_body))
[ybs_status] = response_attr['ybs_autocreate_status']
ybs_msg = response_attr['ybs_autocreate_message'][0] if 'ybs_autocreate_message' in response_attr else None
if (ybs_status == 'ERROR'):
log.msg("Yo Error %s: %s" % (response.code,
response.delivered_body))
yield self.publish_delivery_report(
user_message_id=message['message_id'],
delivery_status='failed',
failure_level='service',
failure_code=ybs_status,
failure_reason=ybs_msg
)
return
log.msg("Sms received and accepted by Yo %s" %
response.delivered_body)
yield self.publish_delivery_report(
user_message_id=message['message_id'],
delivery_status='delivered',
to_addr=message['to_addr']
)
except Exception as ex:
log.msg("Unexpected error %s" % repr(ex))
示例14: handle_outbound_message
def handle_outbound_message(self, message):
log.msg("Outbound message to be processed %s" % repr(message))
try:
cmparser = CMXMLParser()
params = cmparser.build({
'customer_id': self.config['customer_id'],
'login': self.config['login'],
'password': self.config['password'],
'from_addr': self.config['default_origin'],
'to_addr': message['to_addr'],
'content': message['content']})
log.msg('Hitting %s with %r' % (self.config['outbound_url'], params))
response = yield http_request_full(
self.config['outbound_url'],
params,
{'User-Agent': ['Vumi CM YO Transport'],
'Content-Type': ['application/json;charset=UTF-8'], },
method='POST')
log.msg("Response: (%s) %r" % (response.code, response.delivered_body))
content = response.delivered_body.strip()
if response.code != 200 or response.delivered_body:
error = self.KNOWN_ERROR_RESPONSE_CODES.get(content,
'Unknown response code: %s' % (content,))
yield self.publish_nack(message['message_id'], error)
return
yield self.publish_ack(user_message_id=message['message_id'],
sent_message_id=message['message_id'])
except Exception as ex:
log.msg("Unexpected error %s" % repr(ex))
示例15: test_invalid_credentials
def test_invalid_credentials(self):
url = self.mkurl_raw(ServiceNumber='12345', SMSBODY='hello',
USN='something', PWD='wrong', Operator='foo', PhoneNumber='1234')
response = yield http_request_full(url, '', method='GET')
self.assertEqual(400, response.code)
self.assertEqual(json.loads(response.delivered_body),
{'credentials': 'invalid'})