Skip to content
Myr

api.py

This file

Path

requests/api.py

120 lines · 4.2 KiB · Python

Last changed by

Revert "[kennethreitz/requests#1208] adding a max_retries argument"Kenneth Reitzbefore adoption23d8522

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>`.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 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) Float describing the timeout of the request.30    :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.31    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.32    :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.33    :param stream: (optional) if ``False``, the response content will be immediately downloaded.34    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.3536    Usage::3738      >>> import requests39      >>> req = requests.request('GET', 'http://httpbin.org/get')40      <Response [200]>41    """4243    session = sessions.Session()44    return session.request(method=method, url=url, **kwargs)454647def get(url, **kwargs):48    """Sends a GET request. Returns :class:`Response` object.4950    :param url: URL for the new :class:`Request` object.51    :param \*\*kwargs: Optional arguments that ``request`` takes.52    """5354    kwargs.setdefault('allow_redirects', True)55    return request('get', url, **kwargs)565758def options(url, **kwargs):59    """Sends a OPTIONS request. Returns :class:`Response` object.6061    :param url: URL for the new :class:`Request` object.62    :param \*\*kwargs: Optional arguments that ``request`` takes.63    """6465    kwargs.setdefault('allow_redirects', True)66    return request('options', url, **kwargs)676869def head(url, **kwargs):70    """Sends a HEAD request. Returns :class:`Response` object.7172    :param url: URL for the new :class:`Request` object.73    :param \*\*kwargs: Optional arguments that ``request`` takes.74    """7576    kwargs.setdefault('allow_redirects', False)77    return request('head', url, **kwargs)787980def post(url, data=None, **kwargs):81    """Sends a POST request. Returns :class:`Response` object.8283    :param url: URL for the new :class:`Request` object.84    :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.85    :param \*\*kwargs: Optional arguments that ``request`` takes.86    """8788    return request('post', url, data=data, **kwargs)899091def put(url, data=None, **kwargs):92    """Sends a PUT request. Returns :class:`Response` object.9394    :param url: URL for the new :class:`Request` object.95    :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.96    :param \*\*kwargs: Optional arguments that ``request`` takes.97    """9899    return request('put', url, data=data, **kwargs)100101102def patch(url, data=None, **kwargs):103    """Sends a PATCH 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('patch', url,  data=data, **kwargs)111112113def delete(url, **kwargs):114    """Sends a DELETE request. Returns :class:`Response` object.115116    :param url: URL for the new :class:`Request` object.117    :param \*\*kwargs: Optional arguments that ``request`` takes.118    """119120    return request('delete', url, **kwargs)