_request_methods.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from __future__ import annotations
  2. import json as _json
  3. import typing
  4. from urllib.parse import urlencode
  5. from ._base_connection import _TYPE_BODY
  6. from ._collections import HTTPHeaderDict
  7. from .filepost import _TYPE_FIELDS, encode_multipart_formdata
  8. from .response import BaseHTTPResponse
  9. __all__ = ["RequestMethods"]
  10. _TYPE_ENCODE_URL_FIELDS = typing.Union[
  11. typing.Sequence[tuple[str, typing.Union[str, bytes]]],
  12. typing.Mapping[str, typing.Union[str, bytes]],
  13. ]
  14. class RequestMethods:
  15. """
  16. Convenience mixin for classes who implement a :meth:`urlopen` method, such
  17. as :class:`urllib3.HTTPConnectionPool` and
  18. :class:`urllib3.PoolManager`.
  19. Provides behavior for making common types of HTTP request methods and
  20. decides which type of request field encoding to use.
  21. Specifically,
  22. :meth:`.request_encode_url` is for sending requests whose fields are
  23. encoded in the URL (such as GET, HEAD, DELETE).
  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. :meth:`.request` is for making any kind of request, it will look up the
  28. appropriate encoding format and use one of the above two methods to make
  29. the request.
  30. Initializer parameters:
  31. :param headers:
  32. Headers to include with all requests, unless other headers are given
  33. explicitly.
  34. """
  35. _encode_url_methods = {"DELETE", "GET", "HEAD", "OPTIONS"}
  36. def __init__(self, headers: typing.Mapping[str, str] | None = None) -> None:
  37. self.headers = headers or {}
  38. def urlopen(
  39. self,
  40. method: str,
  41. url: str,
  42. body: _TYPE_BODY | None = None,
  43. headers: typing.Mapping[str, str] | None = None,
  44. encode_multipart: bool = True,
  45. multipart_boundary: str | None = None,
  46. **kw: typing.Any,
  47. ) -> BaseHTTPResponse: # Abstract
  48. raise NotImplementedError(
  49. "Classes extending RequestMethods must implement "
  50. "their own ``urlopen`` method."
  51. )
  52. def request(
  53. self,
  54. method: str,
  55. url: str,
  56. body: _TYPE_BODY | None = None,
  57. fields: _TYPE_FIELDS | None = None,
  58. headers: typing.Mapping[str, str] | None = None,
  59. json: typing.Any | None = None,
  60. **urlopen_kw: typing.Any,
  61. ) -> BaseHTTPResponse:
  62. """
  63. Make a request using :meth:`urlopen` with the appropriate encoding of
  64. ``fields`` based on the ``method`` used.
  65. This is a convenience method that requires the least amount of manual
  66. effort. It can be used in most situations, while still having the
  67. option to drop down to more specific methods when necessary, such as
  68. :meth:`request_encode_url`, :meth:`request_encode_body`,
  69. or even the lowest level :meth:`urlopen`.
  70. :param method:
  71. HTTP request method (such as GET, POST, PUT, etc.)
  72. :param url:
  73. The URL to perform the request on.
  74. :param body:
  75. Data to send in the request body, either :class:`str`, :class:`bytes`,
  76. an iterable of :class:`str`/:class:`bytes`, or a file-like object.
  77. :param fields:
  78. Data to encode and send in the URL or request body, depending on ``method``.
  79. :param headers:
  80. Dictionary of custom headers to send, such as User-Agent,
  81. If-None-Match, etc. If None, pool headers are used. If provided,
  82. these headers completely replace any pool-specific headers.
  83. :param json:
  84. Data to encode and send as JSON with UTF-encoded in the request body.
  85. The ``"Content-Type"`` header will be set to ``"application/json"``
  86. unless specified otherwise.
  87. """
  88. method = method.upper()
  89. if json is not None and body is not None:
  90. raise TypeError(
  91. "request got values for both 'body' and 'json' parameters which are mutually exclusive"
  92. )
  93. if json is not None:
  94. if headers is None:
  95. headers = self.headers
  96. if not ("content-type" in map(str.lower, headers.keys())):
  97. headers = HTTPHeaderDict(headers)
  98. headers["Content-Type"] = "application/json"
  99. body = _json.dumps(json, separators=(",", ":"), ensure_ascii=False).encode(
  100. "utf-8"
  101. )
  102. if body is not None:
  103. urlopen_kw["body"] = body
  104. if method in self._encode_url_methods:
  105. return self.request_encode_url(
  106. method,
  107. url,
  108. fields=fields, # type: ignore[arg-type]
  109. headers=headers,
  110. **urlopen_kw,
  111. )
  112. else:
  113. return self.request_encode_body(
  114. method, url, fields=fields, headers=headers, **urlopen_kw
  115. )
  116. def request_encode_url(
  117. self,
  118. method: str,
  119. url: str,
  120. fields: _TYPE_ENCODE_URL_FIELDS | None = None,
  121. headers: typing.Mapping[str, str] | None = None,
  122. **urlopen_kw: str,
  123. ) -> BaseHTTPResponse:
  124. """
  125. Make a request using :meth:`urlopen` with the ``fields`` encoded in
  126. the url. This is useful for request methods like GET, HEAD, DELETE, etc.
  127. :param method:
  128. HTTP request method (such as GET, POST, PUT, etc.)
  129. :param url:
  130. The URL to perform the request on.
  131. :param fields:
  132. Data to encode and send in the URL.
  133. :param headers:
  134. Dictionary of custom headers to send, such as User-Agent,
  135. If-None-Match, etc. If None, pool headers are used. If provided,
  136. these headers completely replace any pool-specific headers.
  137. """
  138. if headers is None:
  139. headers = self.headers
  140. extra_kw: dict[str, typing.Any] = {"headers": headers}
  141. extra_kw.update(urlopen_kw)
  142. if fields:
  143. url += "?" + urlencode(fields)
  144. return self.urlopen(method, url, **extra_kw)
  145. def request_encode_body(
  146. self,
  147. method: str,
  148. url: str,
  149. fields: _TYPE_FIELDS | None = None,
  150. headers: typing.Mapping[str, str] | None = None,
  151. encode_multipart: bool = True,
  152. multipart_boundary: str | None = None,
  153. **urlopen_kw: str,
  154. ) -> BaseHTTPResponse:
  155. """
  156. Make a request using :meth:`urlopen` with the ``fields`` encoded in
  157. the body. This is useful for request methods like POST, PUT, PATCH, etc.
  158. When ``encode_multipart=True`` (default), then
  159. :func:`urllib3.encode_multipart_formdata` is used to encode
  160. the payload with the appropriate content type. Otherwise
  161. :func:`urllib.parse.urlencode` is used with the
  162. 'application/x-www-form-urlencoded' content type.
  163. Multipart encoding must be used when posting files, and it's reasonably
  164. safe to use it in other times too. However, it may break request
  165. signing, such as with OAuth.
  166. Supports an optional ``fields`` parameter of key/value strings AND
  167. key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
  168. the MIME type is optional. For example::
  169. fields = {
  170. 'foo': 'bar',
  171. 'fakefile': ('foofile.txt', 'contents of foofile'),
  172. 'realfile': ('barfile.txt', open('realfile').read()),
  173. 'typedfile': ('bazfile.bin', open('bazfile').read(),
  174. 'image/jpeg'),
  175. 'nonamefile': 'contents of nonamefile field',
  176. }
  177. When uploading a file, providing a filename (the first parameter of the
  178. tuple) is optional but recommended to best mimic behavior of browsers.
  179. Note that if ``headers`` are supplied, the 'Content-Type' header will
  180. be overwritten because it depends on the dynamic random boundary string
  181. which is used to compose the body of the request. The random boundary
  182. string can be explicitly set with the ``multipart_boundary`` parameter.
  183. :param method:
  184. HTTP request method (such as GET, POST, PUT, etc.)
  185. :param url:
  186. The URL to perform the request on.
  187. :param fields:
  188. Data to encode and send in the request body.
  189. :param headers:
  190. Dictionary of custom headers to send, such as User-Agent,
  191. If-None-Match, etc. If None, pool headers are used. If provided,
  192. these headers completely replace any pool-specific headers.
  193. :param encode_multipart:
  194. If True, encode the ``fields`` using the multipart/form-data MIME
  195. format.
  196. :param multipart_boundary:
  197. If not specified, then a random boundary will be generated using
  198. :func:`urllib3.filepost.choose_boundary`.
  199. """
  200. if headers is None:
  201. headers = self.headers
  202. extra_kw: dict[str, typing.Any] = {"headers": HTTPHeaderDict(headers)}
  203. body: bytes | str
  204. if fields:
  205. if "body" in urlopen_kw:
  206. raise TypeError(
  207. "request got values for both 'fields' and 'body', can only specify one."
  208. )
  209. if encode_multipart:
  210. body, content_type = encode_multipart_formdata(
  211. fields, boundary=multipart_boundary
  212. )
  213. else:
  214. body, content_type = (
  215. urlencode(fields), # type: ignore[arg-type]
  216. "application/x-www-form-urlencoded",
  217. )
  218. extra_kw["body"] = body
  219. extra_kw["headers"].setdefault("Content-Type", content_type)
  220. extra_kw.update(urlopen_kw)
  221. return self.urlopen(method, url, **extra_kw)