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