本文整理汇总了Python中httplib2.Response方法的典型用法代码示例。如果您正苦于以下问题:Python httplib2.Response方法的具体用法?Python httplib2.Response怎么用?Python httplib2.Response使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类httplib2
的用法示例。
在下文中一共展示了httplib2.Response方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_data
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def set_data(self, r_code, enc=None, r_body=None, absolute_limit=True):
if enc is None:
enc = self.c_type
resp_dict = {'status': r_code, 'content-type': enc}
resp_body = {'resp_body': 'fake_resp_body'}
if absolute_limit is False:
resp_dict.update({'retry-after': 120})
resp_body.update({'overLimit': {'message': 'fake_message'}})
resp = httplib2.Response(resp_dict)
data = {
"method": "fake_method",
"url": "fake_url",
"headers": "fake_headers",
"body": "fake_body",
"resp": resp,
"resp_body": json.dumps(resp_body)
}
if r_body is not None:
data.update({"resp_body": r_body})
return data
示例2: request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def request(self, uri, method="GET", body=None, headers=None,
redirections=5, connection_type=None):
if not self.return_type:
fake_headers = httplib2.Response(headers)
return_obj = {
'uri': uri,
'method': method,
'body': body,
'headers': headers
}
return (fake_headers, return_obj)
elif isinstance(self.return_type, int):
body = body or "fake_body"
header_info = {
'content-type': 'text/plain',
'status': str(self.return_type),
'content-length': len(body)
}
resp_header = httplib2.Response(header_info)
return (resp_header, body)
else:
msg = "unsupported return type %s" % self.return_type
raise TypeError(msg)
示例3: request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def request(self, uri,
method='GET',
body=None,
headers=None,
redirections=1,
connection_type=None):
resp, content = self._iterable.pop(0)
if content == 'echo_request_headers':
content = headers
elif content == 'echo_request_headers_as_json':
content = simplejson.dumps(headers)
elif content == 'echo_request_body':
if hasattr(body, 'read'):
content = body.read()
else:
content = body
elif content == 'echo_request_uri':
content = uri
return httplib2.Response(resp), content
示例4: test_http_error
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_http_error(self, mock_hook):
http_error_code = 403
hook_instance = mock_hook.return_value
hook_instance.create_job.side_effect = HttpError(
resp=httplib2.Response({
'status': http_error_code
}),
content=b'Forbidden')
with self.assertRaises(HttpError) as context:
training_op = MLEngineStartTrainingJobOperator(
**self.TRAINING_DEFAULT_ARGS)
training_op.execute(None)
mock_hook.assert_called_once_with(
gcp_conn_id='google_cloud_default', delegate_to=None)
# Make sure only 'create_job' is invoked on hook instance
self.assertEqual(len(hook_instance.mock_calls), 1)
hook_instance.create_job.assert_called_once_with(
project_id='test-project', job=self.TRAINING_INPUT, use_existing_job_fn=ANY)
self.assertEqual(http_error_code, context.exception.resp.status)
示例5: test_successful_copy_template
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_successful_copy_template(self, mock_hook):
mock_hook.return_value.get_instance_template.side_effect = [
HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
GCE_INSTANCE_TEMPLATE_BODY_GET,
GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
]
op = ComputeEngineCopyInstanceTemplateOperator(
project_id=GCP_PROJECT_ID,
resource_id=GCE_INSTANCE_TEMPLATE_NAME,
task_id='id',
body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME}
)
result = op.execute(None)
mock_hook.assert_called_once_with(api_version='v1',
gcp_conn_id='google_cloud_default')
mock_hook.return_value.insert_instance_template.assert_called_once_with(
project_id=GCP_PROJECT_ID,
body=GCE_INSTANCE_TEMPLATE_BODY_INSERT,
request_id=None
)
self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例6: test_successful_copy_template_missing_project_id
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_successful_copy_template_missing_project_id(self, mock_hook):
mock_hook.return_value.get_instance_template.side_effect = [
HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
GCE_INSTANCE_TEMPLATE_BODY_GET,
GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
]
op = ComputeEngineCopyInstanceTemplateOperator(
resource_id=GCE_INSTANCE_TEMPLATE_NAME,
task_id='id',
body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME}
)
result = op.execute(None)
mock_hook.assert_called_once_with(api_version='v1',
gcp_conn_id='google_cloud_default')
mock_hook.return_value.insert_instance_template.assert_called_once_with(
project_id=None,
body=GCE_INSTANCE_TEMPLATE_BODY_INSERT,
request_id=None
)
self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例7: test_successful_copy_template_with_request_id
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_successful_copy_template_with_request_id(self, mock_hook):
mock_hook.return_value.get_instance_template.side_effect = [
HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
GCE_INSTANCE_TEMPLATE_BODY_GET,
GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
]
op = ComputeEngineCopyInstanceTemplateOperator(
project_id=GCP_PROJECT_ID,
resource_id=GCE_INSTANCE_TEMPLATE_NAME,
request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
task_id='id',
body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME}
)
result = op.execute(None)
mock_hook.assert_called_once_with(api_version='v1',
gcp_conn_id='google_cloud_default')
mock_hook.return_value.insert_instance_template.assert_called_once_with(
project_id=GCP_PROJECT_ID,
body=GCE_INSTANCE_TEMPLATE_BODY_INSERT,
request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
)
self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
示例8: test_missing_name
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_missing_name(self, mock_hook):
mock_hook.return_value.get_instance_template.side_effect = [
HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT),
GCE_INSTANCE_TEMPLATE_BODY_GET,
GCE_INSTANCE_TEMPLATE_BODY_GET_NEW
]
with self.assertRaises(AirflowException) as cm:
op = ComputeEngineCopyInstanceTemplateOperator(
project_id=GCP_PROJECT_ID,
resource_id=GCE_INSTANCE_TEMPLATE_NAME,
request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID,
task_id='id',
body_patch={"description": "New description"}
)
op.execute(None)
err = cm.exception
self.assertIn("should contain at least name for the new operator "
"in the 'name' field", str(err))
mock_hook.assert_not_called()
示例9: test_bad_request_subscription
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_bad_request_subscription():
with patch.object(googleplay.GooglePlayVerifier, "_authorize", return_value=None):
verifier = GooglePlayVerifier("bundle_id", "private_key_path")
auth = HttpMock(datafile("androidpublisher.json"), headers={"status": 200})
request_mock_builder = RequestMockBuilder(
{
"androidpublisher.purchases.subscriptions.get": (
httplib2.Response({"status": 400, "reason": b"Bad request"}),
b'{"reason": "Bad request"}',
)
}
)
build_mock_result = googleplay.build("androidpublisher", "v3", http=auth, requestBuilder=request_mock_builder)
with patch.object(googleplay, "build", return_value=build_mock_result):
with pytest.raises(errors.GoogleError, match="Bad request"):
verifier.verify("broken_purchase_token", "product_scu", is_subscription=True)
示例10: test_bad_request_product
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_bad_request_product():
with patch.object(googleplay.GooglePlayVerifier, "_authorize", return_value=None):
verifier = GooglePlayVerifier("bundle_id", "private_key_path")
auth = HttpMock(datafile("androidpublisher.json"), headers={"status": 200})
request_mock_builder = RequestMockBuilder(
{
"androidpublisher.purchases.products.get": (
httplib2.Response({"status": 400, "reason": b"Bad request"}),
b'{"reason": "Bad request"}',
)
}
)
build_mock_result = googleplay.build("androidpublisher", "v3", http=auth, requestBuilder=request_mock_builder)
with patch.object(googleplay, "build", return_value=build_mock_result):
with pytest.raises(errors.GoogleError, match="Bad request"):
verifier.verify("broken_purchase_token", "product_scu")
示例11: request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def request(
self, uri, method="GET", body=None, headers=None, redirections=5, connection_type=None
):
for expected_uri, expected_method, result in EXPECTED_RESULTS:
if re.match(expected_uri, uri) and method == expected_method:
return (httplib2.Response({'status': '200'}), seven.json.dumps(result).encode())
# Pass this one through since its the entire JSON schema used for dynamic object creation
if uri == DATAPROC_SCHEMA_URI:
response, content = super(HttpSnooper, self).request(
uri,
method=method,
body=body,
headers=headers,
redirections=redirections,
connection_type=connection_type,
)
return response, content
示例12: __init__
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def __init__(self, resp, content, postproc):
"""Constructor for HttpRequestMock
Args:
resp: httplib2.Response, the response to emulate coming from the request
content: string, the response body
postproc: callable, the post processing function usually supplied by
the model class. See model.JsonModel.response() as an example.
"""
self.resp = resp
self.content = content
self.postproc = postproc
if resp is None:
self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
if 'reason' in self.resp:
self.resp.reason = self.resp['reason']
示例13: request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def request(self, uri,
method='GET',
body=None,
headers=None,
redirections=1,
connection_type=None):
resp, content = self._iterable.pop(0)
if content == 'echo_request_headers':
content = headers
elif content == 'echo_request_headers_as_json':
content = json.dumps(headers)
elif content == 'echo_request_body':
if hasattr(body, 'read'):
content = body.read()
else:
content = body
elif content == 'echo_request_uri':
content = uri
if isinstance(content, six.text_type):
content = content.encode('utf-8')
return httplib2.Response(resp), content
示例14: request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def request(self, uri,
method='GET',
body=None,
headers=None,
redirections=1,
connection_type=None):
self.requests_made.append(self.HttpCall(uri, method, body, headers))
headers = None
body = None
for candidate in self._uris:
if candidate[0].match(uri):
_, headers, body = candidate
break
if not headers:
raise AssertionError("Unexpected request to %s" % uri)
return httplib2.Response(headers), body
示例15: test_directory_api_insert_role__errors
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import Response [as 别名]
def test_directory_api_insert_role__errors(
self, error_code, expected_error):
"""Test the insert_role API method for the Google Admin Directory API."""
test_directory_api = directory.DirectoryAPI(self.config, mock.Mock())
test_directory_api._client.roles.side_effect = errors.HttpError(
httplib2.Response({
'reason': 'NOT USED', 'status': error_code}),
'NOT USED.'.encode(encoding='UTF-8'))
with self.assertRaises(expected_error):
test_directory_api.insert_role()