Skip to content
Myr

api.py

This file

Path

requests/api.py

158 lines · 6.1 KiB · Python

Last changed by

Merge pull request #4739 from moy/doc-list-of-tuplesNate Prewittbefore adoption5aeec8b

History of this file

Take 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, list of tuples or bytes to send22        in the body of the :class:`Request`.23    :param data: (optional) Dictionary, list of tuples, bytes, or file-like24        object to send in the body of the :class:`Request`.25    :param json: (optional) A JSON serializable Python object 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': file-tuple}``) for multipart encoding upload.29        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``30        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string31        defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers32        to add for the file.33    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.34    :param timeout: (optional) How many seconds to wait for the server to send data35        before giving up, as a float, or a :ref:`(connect timeout, read36        timeout) <timeouts>` tuple.37    :type timeout: float or tuple38    :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.39    :type allow_redirects: bool40    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.41    :param verify: (optional) Either a boolean, in which case it controls whether we verify42            the server's TLS certificate, or a string, in which case it must be a path43            to a CA bundle to use. Defaults to ``True``.44    :param stream: (optional) if ``False``, the response content will be immediately downloaded.45    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.46    :return: :class:`Response <Response>` object47    :rtype: requests.Response4849    Usage::5051      >>> import requests52      >>> req = requests.request('GET', 'http://httpbin.org/get')53      <Response [200]>54    """5556    # By using the 'with' statement we are sure the session is closed, thus we57    # avoid leaving sockets open which can trigger a ResourceWarning in some58    # cases, and look like a memory leak in others.59    with sessions.Session() as session:60        return session.request(method=method, url=url, **kwargs)616263def get(url, params=None, **kwargs):64    r"""Sends a GET request.6566    :param url: URL for the new :class:`Request` object.67    :param params: (optional) Dictionary, list of tuples or bytes to send68        in the body of the :class:`Request`.69    :param \*\*kwargs: Optional arguments that ``request`` takes.70    :return: :class:`Response <Response>` object71    :rtype: requests.Response72    """7374    kwargs.setdefault('allow_redirects', True)75    return request('get', url, params=params, **kwargs)767778def options(url, **kwargs):79    r"""Sends an OPTIONS request.8081    :param url: URL for the new :class:`Request` object.82    :param \*\*kwargs: Optional arguments that ``request`` takes.83    :return: :class:`Response <Response>` object84    :rtype: requests.Response85    """8687    kwargs.setdefault('allow_redirects', True)88    return request('options', url, **kwargs)899091def head(url, **kwargs):92    r"""Sends a HEAD request.9394    :param url: URL for the new :class:`Request` object.95    :param \*\*kwargs: Optional arguments that ``request`` takes.96    :return: :class:`Response <Response>` object97    :rtype: requests.Response98    """99100    kwargs.setdefault('allow_redirects', False)101    return request('head', url, **kwargs)102103104def post(url, data=None, json=None, **kwargs):105    r"""Sends a POST request.106107    :param url: URL for the new :class:`Request` object.108    :param data: (optional) Dictionary, list of tuples, bytes, or file-like109        object to send in the body of the :class:`Request`.110    :param json: (optional) json data to send in the body of the :class:`Request`.111    :param \*\*kwargs: Optional arguments that ``request`` takes.112    :return: :class:`Response <Response>` object113    :rtype: requests.Response114    """115116    return request('post', url, data=data, json=json, **kwargs)117118119def put(url, data=None, **kwargs):120    r"""Sends a PUT request.121122    :param url: URL for the new :class:`Request` object.123    :param data: (optional) Dictionary, list of tuples, bytes, or file-like124        object to send in the body of the :class:`Request`.125    :param json: (optional) json data to send in the body of the :class:`Request`.126    :param \*\*kwargs: Optional arguments that ``request`` takes.127    :return: :class:`Response <Response>` object128    :rtype: requests.Response129    """130131    return request('put', url, data=data, **kwargs)132133134def patch(url, data=None, **kwargs):135    r"""Sends a PATCH request.136137    :param url: URL for the new :class:`Request` object.138    :param data: (optional) Dictionary, list of tuples, bytes, or file-like139        object to send in the body of the :class:`Request`.140    :param json: (optional) json data to send in the body of the :class:`Request`.141    :param \*\*kwargs: Optional arguments that ``request`` takes.142    :return: :class:`Response <Response>` object143    :rtype: requests.Response144    """145146    return request('patch', url, data=data, **kwargs)147148149def delete(url, **kwargs):150    r"""Sends a DELETE request.151152    :param url: URL for the new :class:`Request` object.153    :param \*\*kwargs: Optional arguments that ``request`` takes.154    :return: :class:`Response <Response>` object155    :rtype: requests.Response156    """157158    return request('delete', url, **kwargs)