request.py 5.6 KB

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