OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 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 from . import sessions |
| 14 |
| 15 |
| 16 def request(method, url, **kwargs): |
| 17 """Constructs and sends a :class:`Request <Request>`. |
| 18 |
| 19 :param method: method for the new :class:`Request` object. |
| 20 :param url: URL for the new :class:`Request` object. |
| 21 :param params: (optional) Dictionary or bytes to be sent in the query string
for the :class:`Request`. |
| 22 :param data: (optional) Dictionary, bytes, or file-like object to send in th
e body of the :class:`Request`. |
| 23 :param json: (optional) json data to send in the body of the :class:`Request
`. |
| 24 :param headers: (optional) Dictionary of HTTP Headers to send with the :clas
s:`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 ``('f
ilename', fileobj, 'content_type')`` |
| 28 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``,
where ``'content-type'`` is a string |
| 29 defining the content type of the given file and ``custom_headers`` a dic
t-like object containing additional headers |
| 30 to add for the file. |
| 31 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. |
| 32 :param timeout: (optional) How long to wait for the server to send data |
| 33 before giving up, as a float, or a :ref:`(connect timeout, read |
| 34 timeout) <timeouts>` tuple. |
| 35 :type timeout: float or tuple |
| 36 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/
PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. |
| 37 :type allow_redirects: bool |
| 38 :param proxies: (optional) Dictionary mapping protocol to the URL of the pro
xy. |
| 39 :param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE
path can also be provided. Defaults to ``True``. |
| 40 :param stream: (optional) if ``False``, the response content will be immedia
tely downloaded. |
| 41 :param cert: (optional) if String, path to ssl client cert file (.pem). If T
uple, ('cert', 'key') pair. |
| 42 :return: :class:`Response <Response>` object |
| 43 :rtype: requests.Response |
| 44 |
| 45 Usage:: |
| 46 |
| 47 >>> import requests |
| 48 >>> req = requests.request('GET', 'http://httpbin.org/get') |
| 49 <Response [200]> |
| 50 """ |
| 51 |
| 52 # By using the 'with' statement we are sure the session is closed, thus we |
| 53 # avoid leaving sockets open which can trigger a ResourceWarning in some |
| 54 # cases, and look like a memory leak in others. |
| 55 with sessions.Session() as session: |
| 56 return session.request(method=method, url=url, **kwargs) |
| 57 |
| 58 |
| 59 def get(url, params=None, **kwargs): |
| 60 """Sends a GET request. |
| 61 |
| 62 :param url: URL for the new :class:`Request` object. |
| 63 :param params: (optional) Dictionary or bytes to be sent in the query string
for the :class:`Request`. |
| 64 :param \*\*kwargs: Optional arguments that ``request`` takes. |
| 65 :return: :class:`Response <Response>` object |
| 66 :rtype: requests.Response |
| 67 """ |
| 68 |
| 69 kwargs.setdefault('allow_redirects', True) |
| 70 return request('get', url, params=params, **kwargs) |
| 71 |
| 72 |
| 73 def options(url, **kwargs): |
| 74 """Sends a OPTIONS request. |
| 75 |
| 76 :param url: URL for the new :class:`Request` object. |
| 77 :param \*\*kwargs: Optional arguments that ``request`` takes. |
| 78 :return: :class:`Response <Response>` object |
| 79 :rtype: requests.Response |
| 80 """ |
| 81 |
| 82 kwargs.setdefault('allow_redirects', True) |
| 83 return request('options', url, **kwargs) |
| 84 |
| 85 |
| 86 def head(url, **kwargs): |
| 87 """Sends a HEAD request. |
| 88 |
| 89 :param url: URL for the new :class:`Request` object. |
| 90 :param \*\*kwargs: Optional arguments that ``request`` takes. |
| 91 :return: :class:`Response <Response>` object |
| 92 :rtype: requests.Response |
| 93 """ |
| 94 |
| 95 kwargs.setdefault('allow_redirects', False) |
| 96 return request('head', url, **kwargs) |
| 97 |
| 98 |
| 99 def post(url, data=None, json=None, **kwargs): |
| 100 """Sends a POST request. |
| 101 |
| 102 :param url: URL for the new :class:`Request` object. |
| 103 :param data: (optional) Dictionary, bytes, or file-like object to send in th
e body of the :class:`Request`. |
| 104 :param json: (optional) json data to send in the body of the :class:`Request
`. |
| 105 :param \*\*kwargs: Optional arguments that ``request`` takes. |
| 106 :return: :class:`Response <Response>` object |
| 107 :rtype: requests.Response |
| 108 """ |
| 109 |
| 110 return request('post', url, data=data, json=json, **kwargs) |
| 111 |
| 112 |
| 113 def put(url, data=None, **kwargs): |
| 114 """Sends a PUT request. |
| 115 |
| 116 :param url: URL for the new :class:`Request` object. |
| 117 :param data: (optional) Dictionary, bytes, or file-like object to send in th
e body of the :class:`Request`. |
| 118 :param json: (optional) json data to send in the body of the :class:`Request
`. |
| 119 :param \*\*kwargs: Optional arguments that ``request`` takes. |
| 120 :return: :class:`Response <Response>` object |
| 121 :rtype: requests.Response |
| 122 """ |
| 123 |
| 124 return request('put', url, data=data, **kwargs) |
| 125 |
| 126 |
| 127 def patch(url, data=None, **kwargs): |
| 128 """Sends a PATCH request. |
| 129 |
| 130 :param url: URL for the new :class:`Request` object. |
| 131 :param data: (optional) Dictionary, bytes, or file-like object to send in th
e 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 """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) |
OLD | NEW |