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

« back to all changes in this revision

Viewing changes to requests/api.py

  • Committer: Package Import Robot
  • Author(s): Daniele Tricoli
  • Date: 2011-11-25 00:02:28 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111125000228-fwf8kwttxpimoepx
Tags: 0.8.2-1
* New upstream release
* debian/watch
  - Removed "debian uupdate" options
* debian/{copyright,README.source}
  - Updated to reflect upstream changes: switched from poster to
    urllib3
  - Added a stanza about the embedded modified copy of the
    standard module Cookie

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
requests.api
5
5
~~~~~~~~~~~~
6
6
 
7
 
This module impliments the Requests API.
 
7
This module implements the Requests API.
8
8
 
9
9
:copyright: (c) 2011 by Kenneth Reitz.
10
10
:license: ISC, see LICENSE for more details.
11
11
 
12
12
"""
13
13
 
14
 
import config
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
19
 
 
20
 
 
21
 
__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
 
14
from .sessions import session
 
15
 
22
16
 
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):
26
 
 
 
18
    params=None,
 
19
    data=None,
 
20
    headers=None,
 
21
    cookies=None,
 
22
    files=None,
 
23
    auth=None,
 
24
    timeout=None,
 
25
    allow_redirects=False,
 
26
    proxies=None,
 
27
    hooks=None,
 
28
    return_response=True,
 
29
    prefetch=False,
 
30
    config=None):
27
31
    """Constructs and sends a :class:`Request <Request>`.
28
32
    Returns :class:`Response <Response>` object.
29
33
 
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.
42
47
    """
43
48
 
44
 
    method = str(method).upper()
45
 
 
46
 
    if cookies is None:
47
 
        cookies = {}
48
 
 
49
 
    cookies = cookiejar_from_dict(cookies)
50
 
 
51
 
    # Expand header values
52
 
    if headers:
53
 
        for k, v in headers.items() or {}:
54
 
            headers[k] = header_expand(v)
55
 
 
56
 
    args = dict(
57
 
        method = method,
58
 
        url = url,
59
 
        data = data,
60
 
        params = params,
61
 
        headers = headers,
62
 
        cookiejar = cookies,
63
 
        files = files,
64
 
        auth = auth,
65
 
        hooks = hooks,
66
 
        timeout = timeout or config.settings.timeout,
67
 
        allow_redirects = allow_redirects,
68
 
        proxies = proxies or config.settings.proxies,
 
49
    s = session()
 
50
    return s.request(
 
51
        method=method,
 
52
        url=url,
 
53
        params=params,
 
54
        data=data,
 
55
        headers=headers,
 
56
        cookies=cookies,
 
57
        files=files,
 
58
        auth=auth,
 
59
        timeout=timeout,
 
60
        allow_redirects=allow_redirects,
 
61
        proxies=proxies,
 
62
        hooks=hooks,
 
63
        return_response=return_response,
 
64
        config=config,
 
65
        prefetch=prefetch
69
66
    )
70
67
 
71
 
    # Arguments manipulation hook.
72
 
    args = dispatch_hook('args', hooks, args)
73
 
 
74
 
    r = Request(**args)
75
 
 
76
 
    # Pre-request hook.
77
 
    r = dispatch_hook('pre_request', hooks, r)
78
 
 
79
 
    # Don't send if asked nicely.
80
 
    if not return_response:
81
 
        return r
82
 
 
83
 
    # Send the HTTP Request.
84
 
    r.send()
85
 
 
86
 
    # Post-request hook.
87
 
    r = dispatch_hook('post_request', hooks, r)
88
 
 
89
 
    # Response manipulation hook.
90
 
    r.response = dispatch_hook('response', hooks, r.response)
91
 
 
92
 
    return r.response
93
68
 
94
69
 
95
70
def get(url, **kwargs):
96
 
 
97
71
    """Sends a GET request. Returns :class:`Response` object.
98
72
 
99
73
    :param url: URL for the new :class:`Request` object.
100
74
    :param **kwargs: Optional arguments that ``request`` takes.
101
75
    """
102
76
 
103
 
 
104
 
    kwargs.setdefault('allow_redirects', True)
105
 
    return request('GET', url, **kwargs)
 
77
    kwargs.setdefault('allow_redirects', True)
 
78
    return request('get', url, **kwargs)
 
79
 
 
80
 
 
81
def options(url, **kwargs):
 
82
    """Sends a OPTIONS request. Returns :class:`Response` object.
 
83
 
 
84
    :param url: URL for the new :class:`Request` object.
 
85
    :param **kwargs: Optional arguments that ``request`` takes.
 
86
    """
 
87
 
 
88
    kwargs.setdefault('allow_redirects', True)
 
89
    return request('options', url, **kwargs)
106
90
 
107
91
 
108
92
def head(url, **kwargs):
113
97
    """
114
98
 
115
99
    kwargs.setdefault('allow_redirects', True)
116
 
    return request('HEAD', url, **kwargs)
117
 
 
118
 
 
119
 
def post(url, data='', **kwargs):
 
100
    return request('head', url, **kwargs)
 
101
 
 
102
 
 
103
def post(url, data=None, **kwargs):
120
104
    """Sends a POST request. Returns :class:`Response` object.
121
105
 
122
106
    :param url: URL for the new :class:`Request` object.
127
111
    return request('post', url, data=data, **kwargs)
128
112
 
129
113
 
130
 
def put(url, data='', **kwargs):
 
114
def put(url, data=None, **kwargs):
131
115
    """Sends a PUT request. Returns :class:`Response` object.
132
116
 
133
117
    :param url: URL for the new :class:`Request` object.
138
122
    return request('put', url, data=data, **kwargs)
139
123
 
140
124
 
141
 
def patch(url, data='', **kwargs):
 
125
def patch(url, data=None, **kwargs):
142
126
    """Sends a PATCH request. Returns :class:`Response` object.
143
127
 
144
128
    :param url: URL for the new :class:`Request` object.
146
130
    :param **kwargs: Optional arguments that ``request`` takes.
147
131
    """
148
132
 
149
 
    return request('patch', url, **kwargs)
 
133
    return request('patch', url,  data=data, **kwargs)
150
134
 
151
135
 
152
136
def delete(url, **kwargs):