annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/requests/api.py @ 69:33d812a61356

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