2014-03-10 05:18:05 +00:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
except ImportError:
|
|
|
|
from urllib import urlencode
|
|
|
|
|
|
|
|
from .filepost import encode_multipart_formdata
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['RequestMethods']
|
|
|
|
|
|
|
|
|
|
|
|
class RequestMethods(object):
|
|
|
|
"""
|
|
|
|
Convenience mixin for classes who implement a :meth:`urlopen` method, such
|
|
|
|
as :class:`~urllib3.connectionpool.HTTPConnectionPool` and
|
|
|
|
:class:`~urllib3.poolmanager.PoolManager`.
|
|
|
|
|
|
|
|
Provides behavior for making common types of HTTP request methods and
|
|
|
|
decides which type of request field encoding to use.
|
|
|
|
|
|
|
|
Specifically,
|
|
|
|
|
2014-11-20 02:00:00 +00:00
|
|
|
:meth:`.request_encode_url` is for sending requests whose fields are
|
|
|
|
encoded in the URL (such as GET, HEAD, DELETE).
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
:meth:`.request_encode_body` is for sending requests whose fields are
|
|
|
|
encoded in the *body* of the request using multipart or www-form-urlencoded
|
|
|
|
(such as for POST, PUT, PATCH).
|
|
|
|
|
|
|
|
:meth:`.request` is for making any kind of request, it will look up the
|
|
|
|
appropriate encoding format and use one of the above two methods to make
|
|
|
|
the request.
|
|
|
|
|
|
|
|
Initializer parameters:
|
|
|
|
|
|
|
|
:param headers:
|
|
|
|
Headers to include with all requests, unless other headers are given
|
|
|
|
explicitly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_encode_url_methods = set(['DELETE', 'GET', 'HEAD', 'OPTIONS'])
|
|
|
|
|
|
|
|
def __init__(self, headers=None):
|
|
|
|
self.headers = headers or {}
|
|
|
|
|
|
|
|
def urlopen(self, method, url, body=None, headers=None,
|
|
|
|
encode_multipart=True, multipart_boundary=None,
|
2014-11-20 02:00:00 +00:00
|
|
|
**kw): # Abstract
|
2014-03-10 05:18:05 +00:00
|
|
|
raise NotImplemented("Classes extending RequestMethods must implement "
|
|
|
|
"their own ``urlopen`` method.")
|
|
|
|
|
|
|
|
def request(self, method, url, fields=None, headers=None, **urlopen_kw):
|
|
|
|
"""
|
|
|
|
Make a request using :meth:`urlopen` with the appropriate encoding of
|
|
|
|
``fields`` based on the ``method`` used.
|
|
|
|
|
|
|
|
This is a convenience method that requires the least amount of manual
|
2014-11-20 02:00:00 +00:00
|
|
|
effort. It can be used in most situations, while still having the
|
|
|
|
option to drop down to more specific methods when necessary, such as
|
2014-03-10 05:18:05 +00:00
|
|
|
:meth:`request_encode_url`, :meth:`request_encode_body`,
|
|
|
|
or even the lowest level :meth:`urlopen`.
|
|
|
|
"""
|
|
|
|
method = method.upper()
|
|
|
|
|
|
|
|
if method in self._encode_url_methods:
|
|
|
|
return self.request_encode_url(method, url, fields=fields,
|
2014-11-20 02:00:00 +00:00
|
|
|
headers=headers,
|
|
|
|
**urlopen_kw)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
return self.request_encode_body(method, url, fields=fields,
|
2014-11-20 02:00:00 +00:00
|
|
|
headers=headers,
|
|
|
|
**urlopen_kw)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def request_encode_url(self, method, url, fields=None, **urlopen_kw):
|
|
|
|
"""
|
|
|
|
Make a request using :meth:`urlopen` with the ``fields`` encoded in
|
|
|
|
the url. This is useful for request methods like GET, HEAD, DELETE, etc.
|
|
|
|
"""
|
|
|
|
if fields:
|
|
|
|
url += '?' + urlencode(fields)
|
|
|
|
return self.urlopen(method, url, **urlopen_kw)
|
|
|
|
|
|
|
|
def request_encode_body(self, method, url, fields=None, headers=None,
|
|
|
|
encode_multipart=True, multipart_boundary=None,
|
|
|
|
**urlopen_kw):
|
|
|
|
"""
|
|
|
|
Make a request using :meth:`urlopen` with the ``fields`` encoded in
|
|
|
|
the body. This is useful for request methods like POST, PUT, PATCH, etc.
|
|
|
|
|
|
|
|
When ``encode_multipart=True`` (default), then
|
2014-11-20 02:00:00 +00:00
|
|
|
:meth:`urllib3.filepost.encode_multipart_formdata` is used to encode
|
|
|
|
the payload with the appropriate content type. Otherwise
|
2014-03-10 05:18:05 +00:00
|
|
|
:meth:`urllib.urlencode` is used with the
|
|
|
|
'application/x-www-form-urlencoded' content type.
|
|
|
|
|
|
|
|
Multipart encoding must be used when posting files, and it's reasonably
|
2014-11-20 02:00:00 +00:00
|
|
|
safe to use it in other times too. However, it may break request
|
|
|
|
signing, such as with OAuth.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
Supports an optional ``fields`` parameter of key/value strings AND
|
|
|
|
key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
|
2014-11-20 02:00:00 +00:00
|
|
|
the MIME type is optional. For example::
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
fields = {
|
|
|
|
'foo': 'bar',
|
|
|
|
'fakefile': ('foofile.txt', 'contents of foofile'),
|
|
|
|
'realfile': ('barfile.txt', open('realfile').read()),
|
|
|
|
'typedfile': ('bazfile.bin', open('bazfile').read(),
|
|
|
|
'image/jpeg'),
|
|
|
|
'nonamefile': 'contents of nonamefile field',
|
|
|
|
}
|
|
|
|
|
|
|
|
When uploading a file, providing a filename (the first parameter of the
|
|
|
|
tuple) is optional but recommended to best mimick behavior of browsers.
|
|
|
|
|
2014-11-20 02:00:00 +00:00
|
|
|
Note that if ``headers`` are supplied, the 'Content-Type' header will
|
|
|
|
be overwritten because it depends on the dynamic random boundary string
|
2014-03-10 05:18:05 +00:00
|
|
|
which is used to compose the body of the request. The random boundary
|
|
|
|
string can be explicitly set with the ``multipart_boundary`` parameter.
|
|
|
|
"""
|
|
|
|
if encode_multipart:
|
2014-11-20 02:00:00 +00:00
|
|
|
body, content_type = encode_multipart_formdata(
|
|
|
|
fields or {}, boundary=multipart_boundary)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
body, content_type = (urlencode(fields or {}),
|
2014-11-20 02:00:00 +00:00
|
|
|
'application/x-www-form-urlencoded')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if headers is None:
|
|
|
|
headers = self.headers
|
|
|
|
|
|
|
|
headers_ = {'Content-Type': content_type}
|
|
|
|
headers_.update(headers)
|
|
|
|
|
|
|
|
return self.urlopen(method, url, body=body, headers=headers_,
|
|
|
|
**urlopen_kw)
|