Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/urllib3/request.py: 25%
39 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from __future__ import absolute_import
3from .filepost import encode_multipart_formdata
4from .packages.six.moves.urllib.parse import urlencode
6__all__ = ["RequestMethods"]
9class RequestMethods(object):
10 """
11 Convenience mixin for classes who implement a :meth:`urlopen` method, such
12 as :class:`urllib3.HTTPConnectionPool` and
13 :class:`urllib3.PoolManager`.
15 Provides behavior for making common types of HTTP request methods and
16 decides which type of request field encoding to use.
18 Specifically,
20 :meth:`.request_encode_url` is for sending requests whose fields are
21 encoded in the URL (such as GET, HEAD, DELETE).
23 :meth:`.request_encode_body` is for sending requests whose fields are
24 encoded in the *body* of the request using multipart or www-form-urlencoded
25 (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.
31 Initializer parameters:
33 :param headers:
34 Headers to include with all requests, unless other headers are given
35 explicitly.
36 """
38 _encode_url_methods = {"DELETE", "GET", "HEAD", "OPTIONS"}
40 def __init__(self, headers=None):
41 self.headers = headers or {}
43 def urlopen(
44 self,
45 method,
46 url,
47 body=None,
48 headers=None,
49 encode_multipart=True,
50 multipart_boundary=None,
51 **kw
52 ): # Abstract
53 raise NotImplementedError(
54 "Classes extending RequestMethods must implement "
55 "their own ``urlopen`` method."
56 )
58 def request(self, method, url, fields=None, headers=None, **urlopen_kw):
59 """
60 Make a request using :meth:`urlopen` with the appropriate encoding of
61 ``fields`` based on the ``method`` used.
63 This is a convenience method that requires the least amount of manual
64 effort. It can be used in most situations, while still having the
65 option to drop down to more specific methods when necessary, such as
66 :meth:`request_encode_url`, :meth:`request_encode_body`,
67 or even the lowest level :meth:`urlopen`.
68 """
69 method = method.upper()
71 urlopen_kw["request_url"] = url
73 if method in self._encode_url_methods:
74 return self.request_encode_url(
75 method, url, fields=fields, headers=headers, **urlopen_kw
76 )
77 else:
78 return self.request_encode_body(
79 method, url, fields=fields, headers=headers, **urlopen_kw
80 )
82 def request_encode_url(self, method, url, fields=None, headers=None, **urlopen_kw):
83 """
84 Make a request using :meth:`urlopen` with the ``fields`` encoded in
85 the url. This is useful for request methods like GET, HEAD, DELETE, etc.
86 """
87 if headers is None:
88 headers = self.headers
90 extra_kw = {"headers": headers}
91 extra_kw.update(urlopen_kw)
93 if fields:
94 url += "?" + urlencode(fields)
96 return self.urlopen(method, url, **extra_kw)
98 def request_encode_body(
99 self,
100 method,
101 url,
102 fields=None,
103 headers=None,
104 encode_multipart=True,
105 multipart_boundary=None,
106 **urlopen_kw
107 ):
108 """
109 Make a request using :meth:`urlopen` with the ``fields`` encoded in
110 the body. This is useful for request methods like POST, PUT, PATCH, etc.
112 When ``encode_multipart=True`` (default), then
113 :func:`urllib3.encode_multipart_formdata` is used to encode
114 the payload with the appropriate content type. Otherwise
115 :func:`urllib.parse.urlencode` is used with the
116 'application/x-www-form-urlencoded' content type.
118 Multipart encoding must be used when posting files, and it's reasonably
119 safe to use it in other times too. However, it may break request
120 signing, such as with OAuth.
122 Supports an optional ``fields`` parameter of key/value strings AND
123 key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
124 the MIME type is optional. For example::
126 fields = {
127 'foo': 'bar',
128 'fakefile': ('foofile.txt', 'contents of foofile'),
129 'realfile': ('barfile.txt', open('realfile').read()),
130 'typedfile': ('bazfile.bin', open('bazfile').read(),
131 'image/jpeg'),
132 'nonamefile': 'contents of nonamefile field',
133 }
135 When uploading a file, providing a filename (the first parameter of the
136 tuple) is optional but recommended to best mimic behavior of browsers.
138 Note that if ``headers`` are supplied, the 'Content-Type' header will
139 be overwritten because it depends on the dynamic random boundary string
140 which is used to compose the body of the request. The random boundary
141 string can be explicitly set with the ``multipart_boundary`` parameter.
142 """
143 if headers is None:
144 headers = self.headers
146 extra_kw = {"headers": {}}
148 if fields:
149 if "body" in urlopen_kw:
150 raise TypeError(
151 "request got values for both 'fields' and 'body', can only specify one."
152 )
154 if encode_multipart:
155 body, content_type = encode_multipart_formdata(
156 fields, boundary=multipart_boundary
157 )
158 else:
159 body, content_type = (
160 urlencode(fields),
161 "application/x-www-form-urlencoded",
162 )
164 extra_kw["body"] = body
165 extra_kw["headers"] = {"Content-Type": content_type}
167 extra_kw["headers"].update(headers)
168 extra_kw.update(urlopen_kw)
170 return self.urlopen(method, url, **extra_kw)