~ubuntu-branches/ubuntu/precise/requests/precise

« back to all changes in this revision

Viewing changes to requests/api.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniele Tricoli
  • Date: 2011-10-19 20:49:39 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20111019204939-cd40i6asm7mlm0wu
Tags: 0.6.4-1
* New upstream release
* debian/control
  - Dropped python-eventlet from Depends field because it's not used
    anymore
  - Moved python-gevent from Depends field to Recommends field so
    python-requests can be installed also in ia64 and sparc

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
19
19
 
20
 
from urlparse import urlparse
21
20
 
22
21
__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
23
22
 
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):
27
26
 
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.
30
29
 
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.
42
42
    """
43
43
 
 
44
    method = str(method).upper()
 
45
 
44
46
    if cookies is None:
45
47
        cookies = {}
46
48
 
47
49
    cookies = cookiejar_from_dict(cookies)
48
50
 
 
51
    # Expand header values
 
52
    if headers:
 
53
        for k, v in headers.items() or {}:
 
54
            headers[k] = header_expand(v)
 
55
 
49
56
    args = dict(
50
57
        method = method,
51
58
        url = url,
55
62
        cookiejar = cookies,
56
63
        files = files,
57
64
        auth = auth,
 
65
        hooks = hooks,
58
66
        timeout = timeout or config.settings.timeout,
59
67
        allow_redirects = allow_redirects,
60
68
        proxies = proxies or config.settings.proxies,
68
76
    # Pre-request hook.
69
77
    r = dispatch_hook('pre_request', hooks, r)
70
78
 
 
79
    # Don't send if asked nicely.
 
80
    if not return_response:
 
81
        return r
 
82
 
71
83
    # Send the HTTP Request.
72
84
    r.send()
73
85
 
85
97
    """Sends a GET request. Returns :class:`Response` object.
86
98
 
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.
94
101
    """
95
102
 
 
103
 
 
104
    kwargs.setdefault('allow_redirects', True)
96
105
    return request('GET', url, **kwargs)
97
106
 
98
107
 
99
108
def head(url, **kwargs):
100
 
 
101
109
    """Sends a HEAD request. Returns :class:`Response` object.
102
110
 
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.
110
113
    """
111
114
 
 
115
    kwargs.setdefault('allow_redirects', True)
112
116
    return request('HEAD', url, **kwargs)
113
117
 
114
118
 
115
119
def post(url, data='', **kwargs):
116
 
 
117
120
    """Sends a POST request. Returns :class:`Response` object.
118
121
 
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.
129
125
    """
130
126
 
131
 
    return request('POST', url, data=data, **kwargs)
 
127
    return request('post', url, data=data, **kwargs)
132
128
 
133
129
 
134
130
def put(url, data='', **kwargs):
136
132
 
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.
147
136
    """
148
137
 
149
 
    return request('PUT', url, data=data, **kwargs)
 
138
    return request('put', url, data=data, **kwargs)
150
139
 
151
140
 
152
141
def patch(url, data='', **kwargs):
154
143
 
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.
165
147
    """
166
148
 
167
 
    return request('PATCH', url, **kwargs)
 
149
    return request('patch', url, **kwargs)
168
150
 
169
151
 
170
152
def delete(url, **kwargs):
171
 
 
172
153
    """Sends a DELETE request. Returns :class:`Response` object.
173
154
 
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.
182
157
    """
183
158
 
184
 
    return request('DELETE', url, **kwargs)
 
159
    return request('delete', url, **kwargs)