annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/requests/api.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 """
jpayne@68 2 requests.api
jpayne@68 3 ~~~~~~~~~~~~
jpayne@68 4
jpayne@68 5 This module implements the Requests API.
jpayne@68 6
jpayne@68 7 :copyright: (c) 2012 by Kenneth Reitz.
jpayne@68 8 :license: Apache2, see LICENSE for more details.
jpayne@68 9 """
jpayne@68 10
jpayne@68 11 from . import sessions
jpayne@68 12
jpayne@68 13
jpayne@68 14 def request(method, url, **kwargs):
jpayne@68 15 """Constructs and sends a :class:`Request <Request>`.
jpayne@68 16
jpayne@68 17 :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
jpayne@68 18 :param url: URL for the new :class:`Request` object.
jpayne@68 19 :param params: (optional) Dictionary, list of tuples or bytes to send
jpayne@68 20 in the query string for the :class:`Request`.
jpayne@68 21 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
jpayne@68 22 object to send in the body of the :class:`Request`.
jpayne@68 23 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
jpayne@68 24 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
jpayne@68 25 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
jpayne@68 26 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
jpayne@68 27 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
jpayne@68 28 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content_type'`` is a string
jpayne@68 29 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
jpayne@68 30 to add for the file.
jpayne@68 31 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
jpayne@68 32 :param timeout: (optional) How many seconds to wait for the server to send data
jpayne@68 33 before giving up, as a float, or a :ref:`(connect timeout, read
jpayne@68 34 timeout) <timeouts>` tuple.
jpayne@68 35 :type timeout: float or tuple
jpayne@68 36 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
jpayne@68 37 :type allow_redirects: bool
jpayne@68 38 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
jpayne@68 39 :param verify: (optional) Either a boolean, in which case it controls whether we verify
jpayne@68 40 the server's TLS certificate, or a string, in which case it must be a path
jpayne@68 41 to a CA bundle to use. Defaults to ``True``.
jpayne@68 42 :param stream: (optional) if ``False``, the response content will be immediately downloaded.
jpayne@68 43 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
jpayne@68 44 :return: :class:`Response <Response>` object
jpayne@68 45 :rtype: requests.Response
jpayne@68 46
jpayne@68 47 Usage::
jpayne@68 48
jpayne@68 49 >>> import requests
jpayne@68 50 >>> req = requests.request('GET', 'https://httpbin.org/get')
jpayne@68 51 >>> req
jpayne@68 52 <Response [200]>
jpayne@68 53 """
jpayne@68 54
jpayne@68 55 # By using the 'with' statement we are sure the session is closed, thus we
jpayne@68 56 # avoid leaving sockets open which can trigger a ResourceWarning in some
jpayne@68 57 # cases, and look like a memory leak in others.
jpayne@68 58 with sessions.Session() as session:
jpayne@68 59 return session.request(method=method, url=url, **kwargs)
jpayne@68 60
jpayne@68 61
jpayne@68 62 def get(url, params=None, **kwargs):
jpayne@68 63 r"""Sends a GET request.
jpayne@68 64
jpayne@68 65 :param url: URL for the new :class:`Request` object.
jpayne@68 66 :param params: (optional) Dictionary, list of tuples or bytes to send
jpayne@68 67 in the query string for the :class:`Request`.
jpayne@68 68 :param \*\*kwargs: Optional arguments that ``request`` takes.
jpayne@68 69 :return: :class:`Response <Response>` object
jpayne@68 70 :rtype: requests.Response
jpayne@68 71 """
jpayne@68 72
jpayne@68 73 return request("get", url, params=params, **kwargs)
jpayne@68 74
jpayne@68 75
jpayne@68 76 def options(url, **kwargs):
jpayne@68 77 r"""Sends an OPTIONS request.
jpayne@68 78
jpayne@68 79 :param url: URL for the new :class:`Request` object.
jpayne@68 80 :param \*\*kwargs: Optional arguments that ``request`` takes.
jpayne@68 81 :return: :class:`Response <Response>` object
jpayne@68 82 :rtype: requests.Response
jpayne@68 83 """
jpayne@68 84
jpayne@68 85 return request("options", url, **kwargs)
jpayne@68 86
jpayne@68 87
jpayne@68 88 def head(url, **kwargs):
jpayne@68 89 r"""Sends a HEAD request.
jpayne@68 90
jpayne@68 91 :param url: URL for the new :class:`Request` object.
jpayne@68 92 :param \*\*kwargs: Optional arguments that ``request`` takes. If
jpayne@68 93 `allow_redirects` is not provided, it will be set to `False` (as
jpayne@68 94 opposed to the default :meth:`request` behavior).
jpayne@68 95 :return: :class:`Response <Response>` object
jpayne@68 96 :rtype: requests.Response
jpayne@68 97 """
jpayne@68 98
jpayne@68 99 kwargs.setdefault("allow_redirects", False)
jpayne@68 100 return request("head", url, **kwargs)
jpayne@68 101
jpayne@68 102
jpayne@68 103 def post(url, data=None, json=None, **kwargs):
jpayne@68 104 r"""Sends a POST request.
jpayne@68 105
jpayne@68 106 :param url: URL for the new :class:`Request` object.
jpayne@68 107 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
jpayne@68 108 object to send in the body of the :class:`Request`.
jpayne@68 109 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
jpayne@68 110 :param \*\*kwargs: Optional arguments that ``request`` takes.
jpayne@68 111 :return: :class:`Response <Response>` object
jpayne@68 112 :rtype: requests.Response
jpayne@68 113 """
jpayne@68 114
jpayne@68 115 return request("post", url, data=data, json=json, **kwargs)
jpayne@68 116
jpayne@68 117
jpayne@68 118 def put(url, data=None, **kwargs):
jpayne@68 119 r"""Sends a PUT request.
jpayne@68 120
jpayne@68 121 :param url: URL for the new :class:`Request` object.
jpayne@68 122 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
jpayne@68 123 object to send in the body of the :class:`Request`.
jpayne@68 124 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
jpayne@68 125 :param \*\*kwargs: Optional arguments that ``request`` takes.
jpayne@68 126 :return: :class:`Response <Response>` object
jpayne@68 127 :rtype: requests.Response
jpayne@68 128 """
jpayne@68 129
jpayne@68 130 return request("put", url, data=data, **kwargs)
jpayne@68 131
jpayne@68 132
jpayne@68 133 def patch(url, data=None, **kwargs):
jpayne@68 134 r"""Sends a PATCH request.
jpayne@68 135
jpayne@68 136 :param url: URL for the new :class:`Request` object.
jpayne@68 137 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
jpayne@68 138 object to send in the body of the :class:`Request`.
jpayne@68 139 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
jpayne@68 140 :param \*\*kwargs: Optional arguments that ``request`` takes.
jpayne@68 141 :return: :class:`Response <Response>` object
jpayne@68 142 :rtype: requests.Response
jpayne@68 143 """
jpayne@68 144
jpayne@68 145 return request("patch", url, data=data, **kwargs)
jpayne@68 146
jpayne@68 147
jpayne@68 148 def delete(url, **kwargs):
jpayne@68 149 r"""Sends a DELETE request.
jpayne@68 150
jpayne@68 151 :param url: URL for the new :class:`Request` object.
jpayne@68 152 :param \*\*kwargs: Optional arguments that ``request`` takes.
jpayne@68 153 :return: :class:`Response <Response>` object
jpayne@68 154 :rtype: requests.Response
jpayne@68 155 """
jpayne@68 156
jpayne@68 157 return request("delete", url, **kwargs)