本文整理汇总了Python中vumi.utils.http_request函数的典型用法代码示例。如果您正苦于以下问题:Python http_request函数的具体用法?Python http_request怎么用?Python http_request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了http_request函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
def search(self, query, limit=10, namespace=0):
"""
Perform a query and returns a list of dictionaries with results
matching the query.
Parameters
----------
query : str
The search term.
limit : int, optional
How many results to get back, defaults to 10
namespace : int, optional
The namespace of the OpenSearch Suggestions extention, defaults
to 0
"""
query_params = {
'search': query.encode('utf-8'),
'action': 'opensearch',
'limit': str(limit),
'namespace': str(namespace),
'format': 'xml',
}
url = '%s?%s' % (self.URL, urlencode(query_params))
response = yield http_request(url, '', {
'User-Agent': 'Vumi HTTP Request',
}, method='GET')
returnValue(self.parse_xml(response))
示例2: make_request
def make_request(self, session_type, session_id, reply=None,
continue_session=True, expect_msg=True,
defer_response=False, **kw):
url_suffix = "session/%s/%s" % (session_id, session_type)
method = self.SESSION_HTTP_METHOD.get(session_type, "POST")
request_data = self.DEFAULT_SESSION_DATA[session_type].copy()
request_data.update(kw)
deferred_req = http_request(self.worker_url + url_suffix,
json.dumps(request_data), method=method)
if not expect_msg:
msg = None
else:
[msg] = yield self.broker.wait_messages("vumi",
"test_infobip.inbound",
1)
self.broker.clear_messages("vumi", "test_infobip.inbound")
msg = TransportUserMessage(**msg.payload)
if reply is not None:
reply_msg = msg.reply(reply, continue_session=continue_session)
self.broker.publish_message("vumi", "test_infobip.outbound",
reply_msg)
if defer_response:
response = deferred_req
else:
response = yield deferred_req
returnValue((msg, response))
示例3: test_inbound_resume_continue
def test_inbound_resume_continue(self):
url = "%s%s?%s" % (
self.transport_url,
self.config['web_path'],
urlencode({
'ussdSessionId': 123,
'msisdn': 555,
'provider': 'web',
'request': 1,
})
)
d = http_request(url, '', method='GET')
msg, = yield self.wait_for_dispatched_messages(1)
payload = msg.payload
self.assertEqual(payload['transport_name'], self.transport_name)
self.assertEqual(payload['transport_type'], "ussd")
self.assertEqual(payload['transport_metadata'],
{"session_id": "123"})
self.assertEqual(payload['session_event'],
TransportUserMessage.SESSION_RESUME)
self.assertEqual(payload['from_addr'], '555')
self.assertEqual(payload['to_addr'], '')
self.assertEqual(payload['content'], '1')
tum = TransportUserMessage(**payload)
rep = tum.reply("OK")
self.dispatch(rep)
response = yield d
correct_response = '<request>\n\t<headertext>OK</headertext>\n\t' \
'<options>\n\t\t<option command="1" order="1" ' \
'callback="http://localhost/foo" display="False" >' \
'</option>\n\t</options>\n</request>'
self.assertEqual(response, correct_response)
示例4: test_inbound
def test_inbound(self):
url = '%s%s?%s' % (
self.transport_url,
self.config['web_path'],
urlencode([
('to_msisdn', 555),
('to_msisdn', 556),
('from_msisdn', 123),
('message', 'hello'),
])
)
response = yield http_request(url, '', method='GET')
[msg1, msg2] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg1['transport_name'], self.tx_helper.transport_name)
self.assertEqual(msg1['to_addr'], "555")
self.assertEqual(msg2['to_addr'], "556")
self.assertEqual(msg1['from_addr'], "123")
self.assertEqual(msg1['content'], "hello")
self.assertEqual(json.loads(response), [
{
'id': msg1['message_id'],
'message': msg1['content'],
'from_msisdn': msg1['from_addr'],
'to_msisdn': msg1['to_addr'],
},
{
'id': msg2['message_id'],
'message': msg2['content'],
'from_msisdn': msg2['from_addr'],
'to_msisdn': msg2['to_addr'],
},
])
示例5: test_inbound_new_continue
def test_inbound_new_continue(self):
url = "%s%s?%s" % (
self.transport_url,
self.config['web_path'],
urlencode({
'ussdSessionId': 123,
'msisdn': 555,
'provider': 'web',
'request': '*120*666#',
}))
d = http_request(url, '', method='GET')
msg, = yield self.tx_helper.wait_for_dispatched_inbound(1)
self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
self.assertEqual(msg['transport_type'], "ussd")
self.assertEqual(msg['transport_metadata'], {
"session_id": "123"
})
self.assertEqual(msg['session_event'],
TransportUserMessage.SESSION_NEW)
self.assertEqual(msg['from_addr'], '555')
self.assertEqual(msg['to_addr'], '*120*666#')
self.assertEqual(msg['content'], '*120*666#')
self.tx_helper.make_dispatch_reply(msg, "OK")
response = yield d
correct_response = '<request>\n\t<headertext>OK</headertext>\n\t' \
'<options>\n\t\t<option command="1" order="1" ' \
'callback="http://127.0.0.1/foo" display="False" >' \
'</option>\n\t</options>\n</request>'
self.assertEqual(response, correct_response)
示例6: make_request
def make_request(self, session_type, session_id, reply=None,
continue_session=True, expect_msg=True,
defer_response=False, **kw):
url_suffix = "session/%s/%s" % (session_id, session_type)
method = self.SESSION_HTTP_METHOD.get(session_type, "POST")
request_data = self.DEFAULT_SESSION_DATA[session_type].copy()
request_data.update(kw)
deferred_req = http_request(self.transport_url + url_suffix,
json.dumps(request_data), method=method)
if not expect_msg:
msg = None
else:
[msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
self.tx_helper.clear_all_dispatched()
if reply is not None:
yield self.tx_helper.make_dispatch_reply(
msg, reply, continue_session=continue_session)
if defer_response:
response = deferred_req
# We need to make sure we wait for the response so we don't leave
# the reactor dirty if the test runner wins the race with the HTTP
# client.
self.add_cleanup(lambda: deferred_req)
else:
response = yield deferred_req
returnValue((msg, response))
示例7: test_inbound_resume_close
def test_inbound_resume_close(self):
args = "/?ussdSessionId=123&msisdn=555&provider=web&request=1"
url = "%s%s?%s" % (
self.transport_url,
self.config['web_path'],
urlencode({
'ussdSessionId': 123,
'msisdn': 555,
'provider': 'web',
'request': 1,
})
)
d = http_request(url, '', method='GET')
msg, = yield self.wait_for_dispatched_messages(1)
payload = msg.payload
self.assertEqual(payload['transport_name'], self.transport_name)
self.assertEqual(payload['transport_type'], "ussd")
self.assertEqual(payload['transport_metadata'],
{"session_id": "123"})
self.assertEqual(payload['session_event'],
TransportUserMessage.SESSION_RESUME)
self.assertEqual(payload['from_addr'], '555')
self.assertEqual(payload['to_addr'], '')
self.assertEqual(payload['content'], '1')
tum = TransportUserMessage(**payload)
rep = tum.reply("OK", False)
self.dispatch(rep)
response = yield d
correct_response = '<request>\n\t<headertext>OK' + \
'</headertext>\n</request>'
self.assertEqual(response, correct_response)
示例8: test_receipt_processing
def test_receipt_processing(self):
"""it should be able to process an incoming XML receipt via HTTP"""
identifier = '001efc31'
message_id = '123456'
# prime redis to match the incoming identifier to an
# internal message id
yield self.transport.set_message_id_for_identifier(
identifier, message_id)
xml_data = """
<?xml version="1.0"?>
<!DOCTYPE receipts>
<receipts>
<receipt>
<msgid>26567958</msgid>
<reference>%s</reference>
<msisdn>+27123456789</msisdn>
<status>D</status>
<timestamp>20080831T15:59:24</timestamp>
<billed>NO</billed>
</receipt>
</receipts>
""".strip() % identifier
yield http_request(
self.transport.get_transport_url('receipt.xml'), xml_data)
self.assertEqual([], self.tx_helper.get_dispatched_failures())
self.assertEqual([], self.tx_helper.get_dispatched_inbound())
[event] = yield self.tx_helper.wait_for_dispatched_events(1)
self.assertEqual(event['delivery_status'], 'delivered')
self.assertEqual(event['message_type'], 'event')
self.assertEqual(event['event_type'], 'delivery_report')
self.assertEqual(event['user_message_id'], message_id)
示例9: handle_outbound_message
def handle_outbound_message(self, message):
text = message['content']
if text is None:
text = ''
flags = '0'
if message['session_event'] == message.SESSION_CLOSE:
flags = '1'
session_id = message['transport_metadata']['session_id']
response = yield http_request(self.integrat_url, hxg.build({
'Flags': flags,
'SessionID': session_id,
'Type': 'USSReply',
'USSText': text,
'Password': self.integrat_password,
'UserID': self.integrat_username,
}), headers={
'Content-Type': ['text/xml; charset=utf-8']
}, agent_class=self.agent_factory)
error = hxg.parse_response(response)
if not error:
yield self.publish_ack(user_message_id=message['message_id'],
sent_message_id=message['message_id'])
else:
yield self.publish_nack(
user_message_id=message['message_id'],
sent_message_id=message['message_id'],
reason=', '.join([': '.join(ef.items()[0])
for ef in error['error_fields']]))
示例10: test_inbound_resume_close
def test_inbound_resume_close(self):
url = "%s%s?%s" % (
self.transport_url,
self.config['web_path'],
urlencode({
'ussdSessionId': 123,
'msisdn': 555,
'provider': 'web',
'request': 1,
})
)
d = http_request(url, '', method='GET')
msg, = yield self.tx_helper.wait_for_dispatched_inbound(1)
self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
self.assertEqual(msg['transport_type'], "ussd")
self.assertEqual(msg['transport_metadata'], {"session_id": "123"})
self.assertEqual(
msg['session_event'], TransportUserMessage.SESSION_RESUME)
self.assertEqual(msg['from_addr'], '555')
self.assertEqual(msg['to_addr'], '')
self.assertEqual(msg['content'], '1')
self.tx_helper.make_dispatch_reply(msg, "OK", continue_session=False)
response = yield d
correct_response = '<request>\n\t<headertext>OK' + \
'</headertext>\n</request>'
self.assertEqual(response, correct_response)
示例11: test_good_optional_parameter
def test_good_optional_parameter(self):
url = self.mkurl('hello', group='#channel')
response = yield http_request(url, '', method='GET')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['group'], '#channel')
self.assertEqual(json.loads(response),
{'message_id': msg['message_id']})
示例12: test_non_json_content
def test_non_json_content(self):
response = yield http_request(self.worker_url + "session/1/start",
"not json at all", method="POST")
correct_response = {
'responseExitCode': 400,
'responseMessage': 'Invalid JSON',
}
self.assertEqual(json.loads(response), correct_response)
示例13: _make_call
def _make_call(self, params):
params.setdefault("format", "json")
url = "%s?%s" % (self.URL, urlencode(params))
headers = {"User-Agent": "Vumi HTTP Request"}
if self.GZIP:
headers["Accept-Encoding"] = "gzip"
response = yield http_request(url, "", headers, method="GET")
returnValue(json.loads(response))
示例14: test_inbound_with_failed_auth
def test_inbound_with_failed_auth(self):
headers = {
'Authorization': basic_auth_string("user-1", "bad-pass")
}
d = http_request(self.transport_url + "foo", '',
headers=headers, method='GET')
response = yield d
self.assertEqual(response, 'Unauthorized')
示例15: test_ignored_parameters
def test_ignored_parameters(self):
url = self.mkurl('hello', channelID='a', keyword='b', CHANNELID='c',
serviceID='d', SERVICEID='e', unsub='f')
response = yield http_request(url, '', method='GET')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['content'], "hello")
self.assertEqual(json.loads(response),
{'message_id': msg['message_id']})