7
This module impliments the Requests API.
7
This module implements the Requests API.
9
9
:copyright: (c) 2011 by Kenneth Reitz.
10
10
:license: ISC, see LICENSE for more details.
15
from .models import Request, Response, AuthObject
16
from .status_codes import codes
17
from .hooks import dispatch_hook
18
from .utils import cookiejar_from_dict, header_expand
21
__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
14
from .sessions import session
23
17
def request(method, url,
24
params=None, data=None, headers=None, cookies=None, files=None, auth=None,
25
timeout=None, allow_redirects=False, proxies=None, hooks=None, return_response=True):
25
allow_redirects=False,
27
31
"""Constructs and sends a :class:`Request <Request>`.
28
32
Returns :class:`Response <Response>` object.
33
37
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
34
38
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
35
39
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
36
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
37
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
40
:param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
41
:param auth: (optional) Auth typle to enable Basic/Digest/Custom HTTP Auth.
38
42
:param timeout: (optional) Float describing the timeout of the request.
39
43
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
40
44
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
41
45
:param return_response: (optional) If False, an un-sent Request object will returned.
46
:param config: (optional) A configuration dictionary.
44
method = str(method).upper()
49
cookies = cookiejar_from_dict(cookies)
51
# Expand header values
53
for k, v in headers.items() or {}:
54
headers[k] = header_expand(v)
66
timeout = timeout or config.settings.timeout,
67
allow_redirects = allow_redirects,
68
proxies = proxies or config.settings.proxies,
60
allow_redirects=allow_redirects,
63
return_response=return_response,
71
# Arguments manipulation hook.
72
args = dispatch_hook('args', hooks, args)
77
r = dispatch_hook('pre_request', hooks, r)
79
# Don't send if asked nicely.
80
if not return_response:
83
# Send the HTTP Request.
87
r = dispatch_hook('post_request', hooks, r)
89
# Response manipulation hook.
90
r.response = dispatch_hook('response', hooks, r.response)
95
70
def get(url, **kwargs):
97
71
"""Sends a GET request. Returns :class:`Response` object.
99
73
:param url: URL for the new :class:`Request` object.
100
74
:param **kwargs: Optional arguments that ``request`` takes.
104
kwargs.setdefault('allow_redirects', True)
105
return request('GET', url, **kwargs)
77
kwargs.setdefault('allow_redirects', True)
78
return request('get', url, **kwargs)
81
def options(url, **kwargs):
82
"""Sends a OPTIONS request. Returns :class:`Response` object.
84
:param url: URL for the new :class:`Request` object.
85
:param **kwargs: Optional arguments that ``request`` takes.
88
kwargs.setdefault('allow_redirects', True)
89
return request('options', url, **kwargs)
108
92
def head(url, **kwargs):
115
99
kwargs.setdefault('allow_redirects', True)
116
return request('HEAD', url, **kwargs)
119
def post(url, data='', **kwargs):
100
return request('head', url, **kwargs)
103
def post(url, data=None, **kwargs):
120
104
"""Sends a POST request. Returns :class:`Response` object.
122
106
:param url: URL for the new :class:`Request` object.
127
111
return request('post', url, data=data, **kwargs)
130
def put(url, data='', **kwargs):
114
def put(url, data=None, **kwargs):
131
115
"""Sends a PUT request. Returns :class:`Response` object.
133
117
:param url: URL for the new :class:`Request` object.
138
122
return request('put', url, data=data, **kwargs)
141
def patch(url, data='', **kwargs):
125
def patch(url, data=None, **kwargs):
142
126
"""Sends a PATCH request. Returns :class:`Response` object.
144
128
:param url: URL for the new :class:`Request` object.