request.py 5.8 KB

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