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