api.py
This file
Path
requests/api.py
131 lines · 4.8 KiB · Python
Last changed by
Merge pull request #2326 from sigmavirus24/close-functional-sessionsKenneth Reitzbefore adoptione4ddca0
History of this fileTake it
1# -*- coding: utf-8 -*-23"""4requests.api5~~~~~~~~~~~~67This module implements the Requests API.89:copyright: (c) 2012 by Kenneth Reitz.10:license: Apache2, see LICENSE for more details.1112"""1314from . import sessions151617def request(method, url, **kwargs):18 """Constructs and sends a :class:`Request <Request>`.19 Returns :class:`Response <Response>` object.2021 :param method: method for the new :class:`Request` object.22 :param url: URL for the new :class:`Request` object.23 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.24 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.25 :param json: (optional) json data to send in the body of the :class:`Request`.26 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.27 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.28 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': ('filename', fileobj)}``) for multipart encoding upload.29 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.30 :param timeout: (optional) How long to wait for the server to send data31 before giving up, as a float, or a (`connect timeout, read timeout32 <user/advanced.html#timeouts>`_) tuple.33 :type timeout: float or tuple34 :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.35 :type allow_redirects: bool36 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.37 :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.38 :param stream: (optional) if ``False``, the response content will be immediately downloaded.39 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.4041 Usage::4243 >>> import requests44 >>> req = requests.request('GET', 'http://httpbin.org/get')45 <Response [200]>46 """4748 session = sessions.Session()49 response = session.request(method=method, url=url, **kwargs)50 # By explicitly closing the session, we avoid leaving sockets open which51 # can trigger a ResourceWarning in some cases, and look like a memory leak52 # in others.53 session.close()54 return response555657def get(url, **kwargs):58 """Sends a GET request. Returns :class:`Response` object.5960 :param url: URL for the new :class:`Request` object.61 :param \*\*kwargs: Optional arguments that ``request`` takes.62 """6364 kwargs.setdefault('allow_redirects', True)65 return request('get', url, **kwargs)666768def options(url, **kwargs):69 """Sends a OPTIONS request. Returns :class:`Response` object.7071 :param url: URL for the new :class:`Request` object.72 :param \*\*kwargs: Optional arguments that ``request`` takes.73 """7475 kwargs.setdefault('allow_redirects', True)76 return request('options', url, **kwargs)777879def head(url, **kwargs):80 """Sends a HEAD request. Returns :class:`Response` object.8182 :param url: URL for the new :class:`Request` object.83 :param \*\*kwargs: Optional arguments that ``request`` takes.84 """8586 kwargs.setdefault('allow_redirects', False)87 return request('head', url, **kwargs)888990def post(url, data=None, json=None, **kwargs):91 """Sends a POST request. Returns :class:`Response` object.9293 :param url: URL for the new :class:`Request` object.94 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.95 :param json: (optional) json data to send in the body of the :class:`Request`.96 :param \*\*kwargs: Optional arguments that ``request`` takes.97 """9899 return request('post', url, data=data, json=json, **kwargs)100101102def put(url, data=None, **kwargs):103 """Sends a PUT request. Returns :class:`Response` object.104105 :param url: URL for the new :class:`Request` object.106 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.107 :param \*\*kwargs: Optional arguments that ``request`` takes.108 """109110 return request('put', url, data=data, **kwargs)111112113def patch(url, data=None, **kwargs):114 """Sends a PATCH request. Returns :class:`Response` object.115116 :param url: URL for the new :class:`Request` object.117 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.118 :param \*\*kwargs: Optional arguments that ``request`` takes.119 """120121 return request('patch', url, data=data, **kwargs)122123124def delete(url, **kwargs):125 """Sends a DELETE request. Returns :class:`Response` object.126127 :param url: URL for the new :class:`Request` object.128 :param \*\*kwargs: Optional arguments that ``request`` takes.129 """130131 return request('delete', url, **kwargs)