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