| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
requests.api |
| 5 |
~~~~~~~~~~~~ |
| 6 |
|
| 7 |
This module implements the Requests API. |
| 8 |
|
| 9 |
:copyright: (c) 2012 by Kenneth Reitz. |
| 10 |
:license: Apache2, see LICENSE for more details. |
| 11 |
|
| 12 |
""" |
| 13 |
|
| 14 |
from . import sessions |
| 15 |
|
| 16 |
|
| 17 |
def request(method, url, **kwargs): |
| 18 |
"""Constructs and sends a :class:`Request <Request>`. |
| 19 |
Returns :class:`Response <Response>` object. |
| 20 |
|
| 21 |
:param method: method for the new :class:`Request` object. |
| 22 |
:param url: URL for the new :class:`Request` object. |
| 23 |
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. |
| 24 |
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. |
| 25 |
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. |
| 26 |
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. |
| 27 |
:param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload. |
| 28 |
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. |
| 29 |
:param timeout: (optional) Float describing the timeout of the request. |
| 30 |
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed. |
| 31 |
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. |
| 32 |
:param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided. |
| 33 |
:param stream: (optional) if ``False``, the response content will be immediately downloaded. |
| 34 |
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. |
| 35 |
|
| 36 |
Usage:: |
| 37 |
|
| 38 |
>>> import requests |
| 39 |
>>> req = requests.request('GET', 'http://httpbin.org/get') |
| 40 |
<Response [200]> |
| 41 |
""" |
| 42 |
|
| 43 |
session = sessions.Session() |
| 44 |
return session.request(method=method, url=url, **kwargs) |
| 45 |
|
| 46 |
|
| 47 |
def get(url, **kwargs): |
| 48 |
"""Sends a GET request. Returns :class:`Response` object. |
| 49 |
|
| 50 |
:param url: URL for the new :class:`Request` object. |
| 51 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 52 |
""" |
| 53 |
|
| 54 |
kwargs.setdefault('allow_redirects', True) |
| 55 |
return request('get', url, **kwargs) |
| 56 |
|
| 57 |
|
| 58 |
def options(url, **kwargs): |
| 59 |
"""Sends a OPTIONS request. Returns :class:`Response` object. |
| 60 |
|
| 61 |
:param url: URL for the new :class:`Request` object. |
| 62 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 63 |
""" |
| 64 |
|
| 65 |
kwargs.setdefault('allow_redirects', True) |
| 66 |
return request('options', url, **kwargs) |
| 67 |
|
| 68 |
|
| 69 |
def head(url, **kwargs): |
| 70 |
"""Sends a HEAD request. Returns :class:`Response` object. |
| 71 |
|
| 72 |
:param url: URL for the new :class:`Request` object. |
| 73 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 74 |
""" |
| 75 |
|
| 76 |
kwargs.setdefault('allow_redirects', False) |
| 77 |
return request('head', url, **kwargs) |
| 78 |
|
| 79 |
|
| 80 |
def post(url, data=None, **kwargs): |
| 81 |
"""Sends a POST request. Returns :class:`Response` object. |
| 82 |
|
| 83 |
:param url: URL for the new :class:`Request` object. |
| 84 |
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. |
| 85 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 86 |
""" |
| 87 |
|
| 88 |
return request('post', url, data=data, **kwargs) |
| 89 |
|
| 90 |
|
| 91 |
def put(url, data=None, **kwargs): |
| 92 |
"""Sends a PUT request. Returns :class:`Response` object. |
| 93 |
|
| 94 |
:param url: URL for the new :class:`Request` object. |
| 95 |
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. |
| 96 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 97 |
""" |
| 98 |
|
| 99 |
return request('put', url, data=data, **kwargs) |
| 100 |
|
| 101 |
|
| 102 |
def patch(url, data=None, **kwargs): |
| 103 |
"""Sends a PATCH request. Returns :class:`Response` object. |
| 104 |
|
| 105 |
:param url: URL for the new :class:`Request` object. |
| 106 |
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. |
| 107 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 108 |
""" |
| 109 |
|
| 110 |
return request('patch', url, data=data, **kwargs) |
| 111 |
|
| 112 |
|
| 113 |
def delete(url, **kwargs): |
| 114 |
"""Sends a DELETE request. Returns :class:`Response` object. |
| 115 |
|
| 116 |
:param url: URL for the new :class:`Request` object. |
| 117 |
:param \*\*kwargs: Optional arguments that ``request`` takes. |
| 118 |
""" |
| 119 |
|
| 120 |
return request('delete', url, **kwargs) |