jpayne@68: """ jpayne@68: requests.api jpayne@68: ~~~~~~~~~~~~ jpayne@68: jpayne@68: This module implements the Requests API. jpayne@68: jpayne@68: :copyright: (c) 2012 by Kenneth Reitz. jpayne@68: :license: Apache2, see LICENSE for more details. jpayne@68: """ jpayne@68: jpayne@68: from . import sessions jpayne@68: jpayne@68: jpayne@68: def request(method, url, **kwargs): jpayne@68: """Constructs and sends a :class:`Request `. jpayne@68: jpayne@68: :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``. jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param params: (optional) Dictionary, list of tuples or bytes to send jpayne@68: in the query string for the :class:`Request`. jpayne@68: :param data: (optional) Dictionary, list of tuples, bytes, or file-like jpayne@68: object to send in the body of the :class:`Request`. jpayne@68: :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. jpayne@68: :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. jpayne@68: :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. jpayne@68: :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. jpayne@68: ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` jpayne@68: or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content_type'`` is a string jpayne@68: defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers jpayne@68: to add for the file. jpayne@68: :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. jpayne@68: :param timeout: (optional) How many seconds to wait for the server to send data jpayne@68: before giving up, as a float, or a :ref:`(connect timeout, read jpayne@68: timeout) ` tuple. jpayne@68: :type timeout: float or tuple jpayne@68: :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. jpayne@68: :type allow_redirects: bool jpayne@68: :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. jpayne@68: :param verify: (optional) Either a boolean, in which case it controls whether we verify jpayne@68: the server's TLS certificate, or a string, in which case it must be a path jpayne@68: to a CA bundle to use. Defaults to ``True``. jpayne@68: :param stream: (optional) if ``False``, the response content will be immediately downloaded. jpayne@68: :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: jpayne@68: Usage:: jpayne@68: jpayne@68: >>> import requests jpayne@68: >>> req = requests.request('GET', 'https://httpbin.org/get') jpayne@68: >>> req jpayne@68: jpayne@68: """ jpayne@68: jpayne@68: # By using the 'with' statement we are sure the session is closed, thus we jpayne@68: # avoid leaving sockets open which can trigger a ResourceWarning in some jpayne@68: # cases, and look like a memory leak in others. jpayne@68: with sessions.Session() as session: jpayne@68: return session.request(method=method, url=url, **kwargs) jpayne@68: jpayne@68: jpayne@68: def get(url, params=None, **kwargs): jpayne@68: r"""Sends a GET request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param params: (optional) Dictionary, list of tuples or bytes to send jpayne@68: in the query string for the :class:`Request`. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: return request("get", url, params=params, **kwargs) jpayne@68: jpayne@68: jpayne@68: def options(url, **kwargs): jpayne@68: r"""Sends an OPTIONS request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: return request("options", url, **kwargs) jpayne@68: jpayne@68: jpayne@68: def head(url, **kwargs): jpayne@68: r"""Sends a HEAD request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. If jpayne@68: `allow_redirects` is not provided, it will be set to `False` (as jpayne@68: opposed to the default :meth:`request` behavior). jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: kwargs.setdefault("allow_redirects", False) jpayne@68: return request("head", url, **kwargs) jpayne@68: jpayne@68: jpayne@68: def post(url, data=None, json=None, **kwargs): jpayne@68: r"""Sends a POST request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param data: (optional) Dictionary, list of tuples, bytes, or file-like jpayne@68: object to send in the body of the :class:`Request`. jpayne@68: :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: return request("post", url, data=data, json=json, **kwargs) jpayne@68: jpayne@68: jpayne@68: def put(url, data=None, **kwargs): jpayne@68: r"""Sends a PUT request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param data: (optional) Dictionary, list of tuples, bytes, or file-like jpayne@68: object to send in the body of the :class:`Request`. jpayne@68: :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: return request("put", url, data=data, **kwargs) jpayne@68: jpayne@68: jpayne@68: def patch(url, data=None, **kwargs): jpayne@68: r"""Sends a PATCH request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param data: (optional) Dictionary, list of tuples, bytes, or file-like jpayne@68: object to send in the body of the :class:`Request`. jpayne@68: :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: return request("patch", url, data=data, **kwargs) jpayne@68: jpayne@68: jpayne@68: def delete(url, **kwargs): jpayne@68: r"""Sends a DELETE request. jpayne@68: jpayne@68: :param url: URL for the new :class:`Request` object. jpayne@68: :param \*\*kwargs: Optional arguments that ``request`` takes. jpayne@68: :return: :class:`Response ` object jpayne@68: :rtype: requests.Response jpayne@68: """ jpayne@68: jpayne@68: return request("delete", url, **kwargs)