Skip to content
Myr

api.py

This file

Path

src/requests/api.py

157 lines · 6.3 KiB · Python

Last changed by

Move to src directory (#6506)Nate Prewittbefore adoptiond63e94f

History of this file

Take it

1"""2requests.api3~~~~~~~~~~~~45This module implements the Requests API.67:copyright: (c) 2012 by Kenneth Reitz.8:license: Apache2, see LICENSE for more details.9"""1011from . import sessions121314def request(method, url, **kwargs):15    """Constructs and sends a :class:`Request <Request>`.1617    :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.18    :param url: URL for the new :class:`Request` object.19    :param params: (optional) Dictionary, list of tuples or bytes to send20        in the query string for the :class:`Request`.21    :param data: (optional) Dictionary, list of tuples, bytes, or file-like22        object to send in the body of the :class:`Request`.23    :param json: (optional) A JSON serializable Python object 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 many seconds 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. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.37    :type allow_redirects: bool38    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.39    :param verify: (optional) Either a boolean, in which case it controls whether we verify40            the server's TLS certificate, or a string, in which case it must be a path41            to a CA bundle to use. Defaults to ``True``.42    :param stream: (optional) if ``False``, the response content will be immediately downloaded.43    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.44    :return: :class:`Response <Response>` object45    :rtype: requests.Response4647    Usage::4849      >>> import requests50      >>> req = requests.request('GET', 'https://httpbin.org/get')51      >>> req52      <Response [200]>53    """5455    # By using the 'with' statement we are sure the session is closed, thus we56    # avoid leaving sockets open which can trigger a ResourceWarning in some57    # cases, and look like a memory leak in others.58    with sessions.Session() as session:59        return session.request(method=method, url=url, **kwargs)606162def get(url, params=None, **kwargs):63    r"""Sends a GET request.6465    :param url: URL for the new :class:`Request` object.66    :param params: (optional) Dictionary, list of tuples or bytes to send67        in the query string for the :class:`Request`.68    :param \*\*kwargs: Optional arguments that ``request`` takes.69    :return: :class:`Response <Response>` object70    :rtype: requests.Response71    """7273    return request("get", url, params=params, **kwargs)747576def options(url, **kwargs):77    r"""Sends an OPTIONS request.7879    :param url: URL for the new :class:`Request` object.80    :param \*\*kwargs: Optional arguments that ``request`` takes.81    :return: :class:`Response <Response>` object82    :rtype: requests.Response83    """8485    return request("options", url, **kwargs)868788def head(url, **kwargs):89    r"""Sends a HEAD request.9091    :param url: URL for the new :class:`Request` object.92    :param \*\*kwargs: Optional arguments that ``request`` takes. If93        `allow_redirects` is not provided, it will be set to `False` (as94        opposed to the default :meth:`request` behavior).95    :return: :class:`Response <Response>` object96    :rtype: requests.Response97    """9899    kwargs.setdefault("allow_redirects", False)100    return request("head", url, **kwargs)101102103def post(url, data=None, json=None, **kwargs):104    r"""Sends a POST request.105106    :param url: URL for the new :class:`Request` object.107    :param data: (optional) Dictionary, list of tuples, bytes, or file-like108        object to send in the body of the :class:`Request`.109    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.110    :param \*\*kwargs: Optional arguments that ``request`` takes.111    :return: :class:`Response <Response>` object112    :rtype: requests.Response113    """114115    return request("post", url, data=data, json=json, **kwargs)116117118def put(url, data=None, **kwargs):119    r"""Sends a PUT request.120121    :param url: URL for the new :class:`Request` object.122    :param data: (optional) Dictionary, list of tuples, bytes, or file-like123        object to send in the body of the :class:`Request`.124    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.125    :param \*\*kwargs: Optional arguments that ``request`` takes.126    :return: :class:`Response <Response>` object127    :rtype: requests.Response128    """129130    return request("put", url, data=data, **kwargs)131132133def patch(url, data=None, **kwargs):134    r"""Sends a PATCH request.135136    :param url: URL for the new :class:`Request` object.137    :param data: (optional) Dictionary, list of tuples, bytes, or file-like138        object to send in the body of the :class:`Request`.139    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.140    :param \*\*kwargs: Optional arguments that ``request`` takes.141    :return: :class:`Response <Response>` object142    :rtype: requests.Response143    """144145    return request("patch", url, data=data, **kwargs)146147148def delete(url, **kwargs):149    r"""Sends a DELETE request.150151    :param url: URL for the new :class:`Request` object.152    :param \*\*kwargs: Optional arguments that ``request`` takes.153    :return: :class:`Response <Response>` object154    :rtype: requests.Response155    """156157    return request("delete", url, **kwargs)