api.py
This file
Path
requests/api.py
148 lines · 5.7 KiB · Python
Last changed by
Merge pull request #3429 from nateprewitt/docstring_cleanupKenneth Reitzbefore adoptionfb01456
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.11"""1213from . import sessions141516def request(method, url, **kwargs):17 """Constructs and sends a :class:`Request <Request>`.1819 :param method: method for the new :class:`Request` object.20 :param url: URL for the new :class:`Request` object.21 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.22 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.23 :param json: (optional) json data to send in the body of the :class:`Request`.24 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.25 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.26 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.27 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``28 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string29 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers30 to add for the file.31 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.32 :param timeout: (optional) How long to wait for the server to send data33 before giving up, as a float, or a :ref:`(connect timeout, read34 timeout) <timeouts>` tuple.35 :type timeout: float or tuple36 :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.37 :type allow_redirects: bool38 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.39 :param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to ``True``.40 :param stream: (optional) if ``False``, the response content will be immediately downloaded.41 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.42 :return: :class:`Response <Response>` object43 :rtype: requests.Response4445 Usage::4647 >>> import requests48 >>> req = requests.request('GET', 'http://httpbin.org/get')49 <Response [200]>50 """5152 # By using the 'with' statement we are sure the session is closed, thus we53 # avoid leaving sockets open which can trigger a ResourceWarning in some54 # cases, and look like a memory leak in others.55 with sessions.Session() as session:56 return session.request(method=method, url=url, **kwargs)575859def get(url, params=None, **kwargs):60 """Sends a GET request.6162 :param url: URL for the new :class:`Request` object.63 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.64 :param \*\*kwargs: Optional arguments that ``request`` takes.65 :return: :class:`Response <Response>` object66 :rtype: requests.Response67 """6869 kwargs.setdefault('allow_redirects', True)70 return request('get', url, params=params, **kwargs)717273def options(url, **kwargs):74 """Sends a OPTIONS request.7576 :param url: URL for the new :class:`Request` object.77 :param \*\*kwargs: Optional arguments that ``request`` takes.78 :return: :class:`Response <Response>` object79 :rtype: requests.Response80 """8182 kwargs.setdefault('allow_redirects', True)83 return request('options', url, **kwargs)848586def head(url, **kwargs):87 """Sends a HEAD request.8889 :param url: URL for the new :class:`Request` object.90 :param \*\*kwargs: Optional arguments that ``request`` takes.91 :return: :class:`Response <Response>` object92 :rtype: requests.Response93 """9495 kwargs.setdefault('allow_redirects', False)96 return request('head', url, **kwargs)979899def post(url, data=None, json=None, **kwargs):100 """Sends a POST request.101102 :param url: URL for the new :class:`Request` object.103 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.104 :param json: (optional) json data to send in the body of the :class:`Request`.105 :param \*\*kwargs: Optional arguments that ``request`` takes.106 :return: :class:`Response <Response>` object107 :rtype: requests.Response108 """109110 return request('post', url, data=data, json=json, **kwargs)111112113def put(url, data=None, **kwargs):114 """Sends a PUT request.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 :return: :class:`Response <Response>` object120 :rtype: requests.Response121 """122123 return request('put', url, data=data, **kwargs)124125126def patch(url, data=None, **kwargs):127 """Sends a PATCH request.128129 :param url: URL for the new :class:`Request` object.130 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.131 :param \*\*kwargs: Optional arguments that ``request`` takes.132 :return: :class:`Response <Response>` object133 :rtype: requests.Response134 """135136 return request('patch', url, data=data, **kwargs)137138139def delete(url, **kwargs):140 """Sends a DELETE request.141142 :param url: URL for the new :class:`Request` object.143 :param \*\*kwargs: Optional arguments that ``request`` takes.144 :return: :class:`Response <Response>` object145 :rtype: requests.Response146 """147148 return request('delete', url, **kwargs)