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