comparison venv/lib/python2.7/site-packages/requests/packages/urllib3/request.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 try:
2 from urllib.parse import urlencode
3 except ImportError:
4 from urllib import urlencode
5
6 from .filepost import encode_multipart_formdata
7
8
9 __all__ = ['RequestMethods']
10
11
12 class RequestMethods(object):
13 """
14 Convenience mixin for classes who implement a :meth:`urlopen` method, such
15 as :class:`~urllib3.connectionpool.HTTPConnectionPool` and
16 :class:`~urllib3.poolmanager.PoolManager`.
17
18 Provides behavior for making common types of HTTP request methods and
19 decides which type of request field encoding to use.
20
21 Specifically,
22
23 :meth:`.request_encode_url` is for sending requests whose fields are
24 encoded in the URL (such as GET, HEAD, DELETE).
25
26 :meth:`.request_encode_body` is for sending requests whose fields are
27 encoded in the *body* of the request using multipart or www-form-urlencoded
28 (such as for POST, PUT, PATCH).
29
30 :meth:`.request` is for making any kind of request, it will look up the
31 appropriate encoding format and use one of the above two methods to make
32 the request.
33
34 Initializer parameters:
35
36 :param headers:
37 Headers to include with all requests, unless other headers are given
38 explicitly.
39 """
40
41 _encode_url_methods = set(['DELETE', 'GET', 'HEAD', 'OPTIONS'])
42
43 def __init__(self, headers=None):
44 self.headers = headers or {}
45
46 def urlopen(self, method, url, body=None, headers=None,
47 encode_multipart=True, multipart_boundary=None,
48 **kw): # Abstract
49 raise NotImplemented("Classes extending RequestMethods must implement "
50 "their own ``urlopen`` method.")
51
52 def request(self, method, url, fields=None, headers=None, **urlopen_kw):
53 """
54 Make a request using :meth:`urlopen` with the appropriate encoding of
55 ``fields`` based on the ``method`` used.
56
57 This is a convenience method that requires the least amount of manual
58 effort. It can be used in most situations, while still having the
59 option to drop down to more specific methods when necessary, such as
60 :meth:`request_encode_url`, :meth:`request_encode_body`,
61 or even the lowest level :meth:`urlopen`.
62 """
63 method = method.upper()
64
65 if method in self._encode_url_methods:
66 return self.request_encode_url(method, url, fields=fields,
67 headers=headers,
68 **urlopen_kw)
69 else:
70 return self.request_encode_body(method, url, fields=fields,
71 headers=headers,
72 **urlopen_kw)
73
74 def request_encode_url(self, method, url, fields=None, headers=None,
75 **urlopen_kw):
76 """
77 Make a request using :meth:`urlopen` with the ``fields`` encoded in
78 the url. This is useful for request methods like GET, HEAD, DELETE, etc.
79 """
80 if headers is None:
81 headers = self.headers
82
83 extra_kw = {'headers': headers}
84 extra_kw.update(urlopen_kw)
85
86 if fields:
87 url += '?' + urlencode(fields)
88
89 return self.urlopen(method, url, **extra_kw)
90
91 def request_encode_body(self, method, url, fields=None, headers=None,
92 encode_multipart=True, multipart_boundary=None,
93 **urlopen_kw):
94 """
95 Make a request using :meth:`urlopen` with the ``fields`` encoded in
96 the body. This is useful for request methods like POST, PUT, PATCH, etc.
97
98 When ``encode_multipart=True`` (default), then
99 :meth:`urllib3.filepost.encode_multipart_formdata` is used to encode
100 the payload with the appropriate content type. Otherwise
101 :meth:`urllib.urlencode` is used with the
102 'application/x-www-form-urlencoded' content type.
103
104 Multipart encoding must be used when posting files, and it's reasonably
105 safe to use it in other times too. However, it may break request
106 signing, such as with OAuth.
107
108 Supports an optional ``fields`` parameter of key/value strings AND
109 key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
110 the MIME type is optional. For example::
111
112 fields = {
113 'foo': 'bar',
114 'fakefile': ('foofile.txt', 'contents of foofile'),
115 'realfile': ('barfile.txt', open('realfile').read()),
116 'typedfile': ('bazfile.bin', open('bazfile').read(),
117 'image/jpeg'),
118 'nonamefile': 'contents of nonamefile field',
119 }
120
121 When uploading a file, providing a filename (the first parameter of the
122 tuple) is optional but recommended to best mimick behavior of browsers.
123
124 Note that if ``headers`` are supplied, the 'Content-Type' header will
125 be overwritten because it depends on the dynamic random boundary string
126 which is used to compose the body of the request. The random boundary
127 string can be explicitly set with the ``multipart_boundary`` parameter.
128 """
129 if headers is None:
130 headers = self.headers
131
132 extra_kw = {'headers': {}}
133
134 if fields:
135 if 'body' in urlopen_kw:
136 raise TypeError('request got values for both \'fields\' and \'body\', can only specify one.')
137
138 if encode_multipart:
139 body, content_type = encode_multipart_formdata(fields, boundary=multipart_boundary)
140 else:
141 body, content_type = urlencode(fields), 'application/x-www-form-urlencoded'
142
143 extra_kw['body'] = body
144 extra_kw['headers'] = {'Content-Type': content_type}
145
146 extra_kw['headers'].update(headers)
147 extra_kw.update(urlopen_kw)
148
149 return self.urlopen(method, url, **extra_kw)