Skip to content
Myr

api.py

This file

Path

requests/api.py

147 lines · 5.3 KiB · Python

Last changed by

Merge pull request #2728 from lukasgraf/docs-fix-timeout-linksCory Benfieldbefore adoptionfdc76f1

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.1112"""1314from . import sessions151617def request(method, url, **kwargs):18    """Constructs and sends a :class:`Request <Request>`.1920    :param method: method for the new :class:`Request` object.21    :param url: URL for the new :class:`Request` object.22    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.23    :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.24    :param json: (optional) json data to send in the body of the :class:`Request`.25    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.26    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.27    :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': ('filename', fileobj)}``) for multipart encoding upload.28    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.29    :param timeout: (optional) How long to wait for the server to send data30        before giving up, as a float, or a :ref:`(connect timeout, read31        timeout) <timeouts>` tuple.32    :type timeout: float or tuple33    :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.34    :type allow_redirects: bool35    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.36    :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.37    :param stream: (optional) if ``False``, the response content will be immediately downloaded.38    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.39    :return: :class:`Response <Response>` object40    :rtype: requests.Response4142    Usage::4344      >>> import requests45      >>> req = requests.request('GET', 'http://httpbin.org/get')46      <Response [200]>47    """4849    session = sessions.Session()50    response = session.request(method=method, url=url, **kwargs)51    # By explicitly closing the session, we avoid leaving sockets open which52    # can trigger a ResourceWarning in some cases, and look like a memory leak53    # in others.54    session.close()55    return response565758def get(url, params=None, **kwargs):59    """Sends a GET request.6061    :param url: URL for the new :class:`Request` object.62    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.63    :param \*\*kwargs: Optional arguments that ``request`` takes.64    :return: :class:`Response <Response>` object65    :rtype: requests.Response66    """6768    kwargs.setdefault('allow_redirects', True)69    return request('get', url, params=params, **kwargs)707172def options(url, **kwargs):73    """Sends a OPTIONS request.7475    :param url: URL for the new :class:`Request` object.76    :param \*\*kwargs: Optional arguments that ``request`` takes.77    :return: :class:`Response <Response>` object78    :rtype: requests.Response79    """8081    kwargs.setdefault('allow_redirects', True)82    return request('options', url, **kwargs)838485def head(url, **kwargs):86    """Sends a HEAD request.8788    :param url: URL for the new :class:`Request` object.89    :param \*\*kwargs: Optional arguments that ``request`` takes.90    :return: :class:`Response <Response>` object91    :rtype: requests.Response92    """9394    kwargs.setdefault('allow_redirects', False)95    return request('head', url, **kwargs)969798def post(url, data=None, json=None, **kwargs):99    """Sends a POST request.100101    :param url: URL for the new :class:`Request` object.102    :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.103    :param json: (optional) json data to send in the body of the :class:`Request`.104    :param \*\*kwargs: Optional arguments that ``request`` takes.105    :return: :class:`Response <Response>` object106    :rtype: requests.Response107    """108109    return request('post', url, data=data, json=json, **kwargs)110111112def put(url, data=None, **kwargs):113    """Sends a PUT request.114115    :param url: URL for the new :class:`Request` object.116    :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.117    :param \*\*kwargs: Optional arguments that ``request`` takes.118    :return: :class:`Response <Response>` object119    :rtype: requests.Response120    """121122    return request('put', url, data=data, **kwargs)123124125def patch(url, data=None, **kwargs):126    """Sends a PATCH request.127128    :param url: URL for the new :class:`Request` object.129    :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.130    :param \*\*kwargs: Optional arguments that ``request`` takes.131    :return: :class:`Response <Response>` object132    :rtype: requests.Response133    """134135    return request('patch', url,  data=data, **kwargs)136137138def delete(url, **kwargs):139    """Sends a DELETE request.140141    :param url: URL for the new :class:`Request` object.142    :param \*\*kwargs: Optional arguments that ``request`` takes.143    :return: :class:`Response <Response>` object144    :rtype: requests.Response145    """146147    return request('delete', url, **kwargs)