15
15
from .models import Request, Response, AuthObject
16
16
from .status_codes import codes
17
17
from .hooks import dispatch_hook
18
from .utils import cookiejar_from_dict
18
from .utils import cookiejar_from_dict, header_expand
20
from urlparse import urlparse
22
21
__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
24
23
def request(method, url,
25
24
params=None, data=None, headers=None, cookies=None, files=None, auth=None,
26
timeout=None, allow_redirects=False, proxies=None, hooks=None):
25
timeout=None, allow_redirects=False, proxies=None, hooks=None, return_response=True):
28
"""Constructs and sends a :class:`Request <models.Request>`.
29
Returns :class:`Response <models.Response>` object.
27
"""Constructs and sends a :class:`Request <Request>`.
28
Returns :class:`Response <Response>` object.
31
30
:param method: method for the new :class:`Request` object.
32
31
:param url: URL for the new :class:`Request` object.
39
38
:param timeout: (optional) Float describing the timeout of the request.
40
39
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
41
40
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
41
:param return_response: (optional) If False, an un-sent Request object will returned.
44
method = str(method).upper()
44
46
if cookies is None:
47
49
cookies = cookiejar_from_dict(cookies)
51
# Expand header values
53
for k, v in headers.items() or {}:
54
headers[k] = header_expand(v)
85
97
"""Sends a GET request. Returns :class:`Response` object.
87
99
:param url: URL for the new :class:`Request` object.
88
:param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.
89
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
90
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
91
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
92
:param timeout: (optional) Float describing the timeout of the request.
93
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
100
:param **kwargs: Optional arguments that ``request`` takes.
104
kwargs.setdefault('allow_redirects', True)
96
105
return request('GET', url, **kwargs)
99
108
def head(url, **kwargs):
101
109
"""Sends a HEAD request. Returns :class:`Response` object.
103
111
:param url: URL for the new :class:`Request` object.
104
:param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.
105
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
106
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
107
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
108
:param timeout: (optional) Float describing the timeout of the request.
109
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
112
:param **kwargs: Optional arguments that ``request`` takes.
115
kwargs.setdefault('allow_redirects', True)
112
116
return request('HEAD', url, **kwargs)
115
119
def post(url, data='', **kwargs):
117
120
"""Sends a POST request. Returns :class:`Response` object.
119
122
:param url: URL for the new :class:`Request` object.
120
123
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
121
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
122
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
123
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
124
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
125
:param timeout: (optional) Float describing the timeout of the request.
126
:param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed.
127
:param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.
128
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
124
:param **kwargs: Optional arguments that ``request`` takes.
131
return request('POST', url, data=data, **kwargs)
127
return request('post', url, data=data, **kwargs)
134
130
def put(url, data='', **kwargs):
137
133
:param url: URL for the new :class:`Request` object.
138
134
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
139
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
140
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
141
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
142
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
143
:param timeout: (optional) Float describing the timeout of the request.
144
:param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed.
145
:param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.
146
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
135
:param **kwargs: Optional arguments that ``request`` takes.
149
return request('PUT', url, data=data, **kwargs)
138
return request('put', url, data=data, **kwargs)
152
141
def patch(url, data='', **kwargs):
155
144
:param url: URL for the new :class:`Request` object.
156
145
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
157
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
158
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
159
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
160
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
161
:param timeout: (optional) Float describing the timeout of the request.
162
:param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed.
163
:param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.
164
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
146
:param **kwargs: Optional arguments that ``request`` takes.
167
return request('PATCH', url, **kwargs)
149
return request('patch', url, **kwargs)
170
152
def delete(url, **kwargs):
172
153
"""Sends a DELETE request. Returns :class:`Response` object.
174
155
:param url: URL for the new :class:`Request` object.
175
:param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.
176
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
177
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
178
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
179
:param timeout: (optional) Float describing the timeout of the request.
180
:param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed.
181
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
156
:param **kwargs: Optional arguments that ``request`` takes.
184
return request('DELETE', url, **kwargs)
159
return request('delete', url, **kwargs)