codekingpro/portable-devtools
114k
1"""2requests.api3~~~~~~~~~~~~4 5This module implements the Requests API.6 7:copyright: (c) 2012 by Kenneth Reitz.8:license: Apache2, see LICENSE for more details.9"""10 11from . import sessions12 13 14def request(method, url, **kwargs):15 """Constructs and sends a :class:`Request <Request>`.16 17 :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.18 :param url: URL for the new :class:`Request` object.19 :param params: (optional) Dictionary, list of tuples or bytes to send20 in the query string for the :class:`Request`.21 :param data: (optional) Dictionary, list of tuples, bytes, or file-like22 object to send in the body of the :class:`Request`.23 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.24 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.25 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.26 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.27 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``28 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string29 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers30 to add for the file.31 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.32 :param timeout: (optional) How many seconds to wait for the server to send data33 before giving up, as a float, or a :ref:`(connect timeout, read34 timeout) <timeouts>` tuple.35 :type timeout: float or tuple36 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.37 :type allow_redirects: bool38 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.39 :param verify: (optional) Either a boolean, in which case it controls whether we verify40 the server's TLS certificate, or a string, in which case it must be a path41 to a CA bundle to use. Defaults to ``True``.42 :param stream: (optional) if ``False``, the response content will be immediately downloaded.43 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.44 :return: :class:`Response <Response>` object45 :rtype: requests.Response46 47 Usage::48 49 >>> import requests50 >>> req = requests.request('GET', 'https://httpbin.org/get')51 >>> req52 <Response [200]>53 """54 55 # By using the 'with' statement we are sure the session is closed, thus we56 # avoid leaving sockets open which can trigger a ResourceWarning in some57 # cases, and look like a memory leak in others.58 with sessions.Session() as session:59 return session.request(method=method, url=url, **kwargs)60 61 62def get(url, params=None, **kwargs):63 r"""Sends a GET request.64 65 :param url: URL for the new :class:`Request` object.66 :param params: (optional) Dictionary, list of tuples or bytes to send67 in the query string for the :class:`Request`.68 :param \*\*kwargs: Optional arguments that ``request`` takes.69 :return: :class:`Response <Response>` object70 :rtype: requests.Response71 """72 73 return request("get", url, params=params, **kwargs)74 75 76def options(url, **kwargs):77 r"""Sends an OPTIONS request.78 79 :param url: URL for the new :class:`Request` object.80 :param \*\*kwargs: Optional arguments that ``request`` takes.81 :return: :class:`Response <Response>` object82 :rtype: requests.Response83 """84 85 return request("options", url, **kwargs)86 87 88def head(url, **kwargs):89 r"""Sends a HEAD request.90 91 :param url: URL for the new :class:`Request` object.92 :param \*\*kwargs: Optional arguments that ``request`` takes. If93 `allow_redirects` is not provided, it will be set to `False` (as94 opposed to the default :meth:`request` behavior).95 :return: :class:`Response <Response>` object96 :rtype: requests.Response97 """98 99 kwargs.setdefault("allow_redirects", False)100 return request("head", url, **kwargs)101 102 103def post(url, data=None, json=None, **kwargs):104 r"""Sends a POST request.105 106 :param url: URL for the new :class:`Request` object.107 :param data: (optional) Dictionary, list of tuples, bytes, or file-like108 object to send in the body of the :class:`Request`.109 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.110 :param \*\*kwargs: Optional arguments that ``request`` takes.111 :return: :class:`Response <Response>` object112 :rtype: requests.Response113 """114 115 return request("post", url, data=data, json=json, **kwargs)116 117 118def put(url, data=None, **kwargs):119 r"""Sends a PUT request.120 121 :param url: URL for the new :class:`Request` object.122 :param data: (optional) Dictionary, list of tuples, bytes, or file-like123 object to send in the body of the :class:`Request`.124 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.125 :param \*\*kwargs: Optional arguments that ``request`` takes.126 :return: :class:`Response <Response>` object127 :rtype: requests.Response128 """129 130 return request("put", url, data=data, **kwargs)131 132 133def patch(url, data=None, **kwargs):134 r"""Sends a PATCH request.135 136 :param url: URL for the new :class:`Request` object.137 :param data: (optional) Dictionary, list of tuples, bytes, or file-like138 object to send in the body of the :class:`Request`.139 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.140 :param \*\*kwargs: Optional arguments that ``request`` takes.141 :return: :class:`Response <Response>` object142 :rtype: requests.Response143 """144 145 return request("patch", url, data=data, **kwargs)146 147 148def delete(url, **kwargs):149 r"""Sends a DELETE request.150 151 :param url: URL for the new :class:`Request` object.152 :param \*\*kwargs: Optional arguments that ``request`` takes.153 :return: :class:`Response <Response>` object154 :rtype: requests.Response155 """156 157 return request("delete", url, **kwargs)158 