本文整理匯總了Python中requests.sessions.Session.close方法的典型用法代碼示例。如果您正苦於以下問題:Python Session.close方法的具體用法?Python Session.close怎麽用?Python Session.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類requests.sessions.Session
的用法示例。
在下文中一共展示了Session.close方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: PortalConnection
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import close [as 別名]
#.........這裏部分代碼省略.........
'PUT',
url_path,
body_deserialization=body_deserialization,
)
def send_delete_request(self, url_path):
"""
Send a DELETE request to HubSpot
:param basestring url_path: The URL path to the endpoint
:return: Decoded version of the ``JSON`` that HubSpot put in \
the body of the response.
"""
return self._send_request('DELETE', url_path)
def _send_request(
self,
method,
url_path,
query_string_args=None,
body_deserialization=None,
):
url = self._API_URL + url_path
query_string_args = query_string_args or {}
query_string_args = dict(query_string_args, auditId=self._change_source)
request_headers = \
{'content-type': 'application/json'} if body_deserialization else {}
if body_deserialization:
request_body_serialization = json_serialize(body_deserialization)
else:
request_body_serialization = None
response = self._session.request(
method,
url,
params=query_string_args,
auth=self._authentication_handler,
data=request_body_serialization,
headers=request_headers,
)
response_body_deserialization = \
self._deserialize_response_body(response)
return response_body_deserialization
@classmethod
def _deserialize_response_body(cls, response):
cls._require_successful_response(response)
cls._require_json_response(response)
if response.status_code == HTTP_STATUS_OK:
response_body_deserialization = response.json()
elif response.status_code in _HTTP_STATUS_CODES_WITH_EMPTY_BODIES:
response_body_deserialization = None
else:
exception_message = \
'Unsupported response status {}'.format(response.status_code)
raise HubspotUnsupportedResponseError(exception_message)
return response_body_deserialization
@staticmethod
def _require_successful_response(response):
if 400 <= response.status_code < 500:
response_data = response.json()
error_data = _HUBSPOT_ERROR_RESPONSE_SCHEMA(response_data)
if response.status_code == HTTP_STATUS_UNAUTHORIZED:
exception_class = HubspotAuthenticationError
else:
exception_class = HubspotClientError
raise exception_class(
error_data['message'],
error_data['requestId'],
)
elif 500 <= response.status_code < 600:
raise HubspotServerError(response.reason, response.status_code)
@staticmethod
def _require_json_response(response):
content_type_header_value = response.headers.get('Content-Type')
if not content_type_header_value:
exception_message = 'Response does not specify a Content-Type'
raise HubspotUnsupportedResponseError(exception_message)
content_type = content_type_header_value.split(';')[0].lower()
if content_type != 'application/json':
exception_message = \
'Unsupported response content type {}'.format(content_type)
raise HubspotUnsupportedResponseError(exception_message)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self._session.close()
示例2: __init__
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import close [as 別名]
#.........這裏部分代碼省略.........
:return: Decoded version of the ``JSON`` the remote put in \
the body of the response.
"""
return self._send_request(
'PUT',
url,
body_deserialization=body_deserialization,
)
def send_delete_request(self, url):
"""
Send a DELETE request
:param str url: The URL or URL path to the endpoint
:return: Decoded version of the ``JSON`` the remote put in \
the body of the response.
"""
return self._send_request('DELETE', url)
def _send_request(
self,
method,
url,
query_string_args=None,
body_deserialization=None,
):
if url.startswith(self._api_url):
url = url
else:
url = self._api_url + url
query_string_args = query_string_args or {}
request_headers = \
{'content-type': 'application/json'} if body_deserialization else {}
if body_deserialization:
request_body_serialization = json_serialize(body_deserialization)
else:
request_body_serialization = None
response = self._session.request(
method,
url,
params=query_string_args,
auth=self._authentication_handler,
data=request_body_serialization,
headers=request_headers,
timeout=self._timeout,
)
self._require_successful_response(response)
self._require_deserializable_response_body(response)
return response
@staticmethod
def _require_successful_response(response):
if 400 <= response.status_code < 500:
if response.status_code == HTTPStatus.UNAUTHORIZED:
exception_class = AuthenticationError
elif response.status_code == HTTPStatus.FORBIDDEN:
exception_class = AccessDeniedError
elif response.status_code == HTTPStatus.NOT_FOUND:
exception_class = NotFoundError
else:
exception_class = ClientError
raise exception_class()
elif 500 <= response.status_code < 600:
raise ServerError(response.reason, response.status_code)
@classmethod
def _require_deserializable_response_body(cls, response):
if response.status_code in (HTTPStatus.OK, HTTPStatus.NO_CONTENT):
if response.content:
cls._require_json_response(response)
else:
exception_message = \
'Unsupported response status {}'.format(response.status_code)
raise UnsupportedResponseError(exception_message)
@staticmethod
def _require_json_response(response):
content_type_header_value = response.headers.get('Content-Type')
if not content_type_header_value:
exception_message = 'Response does not specify a Content-Type'
raise UnsupportedResponseError(exception_message)
content_type = content_type_header_value.split(';')[0].lower()
if content_type != 'application/json':
exception_message = \
'Unsupported response content type {}'.format(content_type)
raise UnsupportedResponseError(exception_message)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self._session.close()