Mercurial > repos > guerler > hhblits
comparison lib/python3.8/site-packages/pip/_vendor/urllib3/request.py @ 0:9e54283cc701 draft
"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author | guerler |
---|---|
date | Mon, 27 Jul 2020 03:47:31 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9e54283cc701 |
---|---|
1 from __future__ import absolute_import | |
2 | |
3 from .filepost import encode_multipart_formdata | |
4 from .packages.six.moves.urllib.parse import urlencode | |
5 | |
6 | |
7 __all__ = ["RequestMethods"] | |
8 | |
9 | |
10 class RequestMethods(object): | |
11 """ | |
12 Convenience mixin for classes who implement a :meth:`urlopen` method, such | |
13 as :class:`~urllib3.connectionpool.HTTPConnectionPool` and | |
14 :class:`~urllib3.poolmanager.PoolManager`. | |
15 | |
16 Provides behavior for making common types of HTTP request methods and | |
17 decides which type of request field encoding to use. | |
18 | |
19 Specifically, | |
20 | |
21 :meth:`.request_encode_url` is for sending requests whose fields are | |
22 encoded in the URL (such as GET, HEAD, DELETE). | |
23 | |
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 | |
28 :meth:`.request` is for making any kind of request, it will look up the | |
29 appropriate encoding format and use one of the above two methods to make | |
30 the request. | |
31 | |
32 Initializer parameters: | |
33 | |
34 :param headers: | |
35 Headers to include with all requests, unless other headers are given | |
36 explicitly. | |
37 """ | |
38 | |
39 _encode_url_methods = {"DELETE", "GET", "HEAD", "OPTIONS"} | |
40 | |
41 def __init__(self, headers=None): | |
42 self.headers = headers or {} | |
43 | |
44 def urlopen( | |
45 self, | |
46 method, | |
47 url, | |
48 body=None, | |
49 headers=None, | |
50 encode_multipart=True, | |
51 multipart_boundary=None, | |
52 **kw | |
53 ): # Abstract | |
54 raise NotImplementedError( | |
55 "Classes extending RequestMethods must implement " | |
56 "their own ``urlopen`` method." | |
57 ) | |
58 | |
59 def request(self, method, url, fields=None, headers=None, **urlopen_kw): | |
60 """ | |
61 Make a request using :meth:`urlopen` with the appropriate encoding of | |
62 ``fields`` based on the ``method`` used. | |
63 | |
64 This is a convenience method that requires the least amount of manual | |
65 effort. It can be used in most situations, while still having the | |
66 option to drop down to more specific methods when necessary, such as | |
67 :meth:`request_encode_url`, :meth:`request_encode_body`, | |
68 or even the lowest level :meth:`urlopen`. | |
69 """ | |
70 method = method.upper() | |
71 | |
72 urlopen_kw["request_url"] = url | |
73 | |
74 if method in self._encode_url_methods: | |
75 return self.request_encode_url( | |
76 method, url, fields=fields, headers=headers, **urlopen_kw | |
77 ) | |
78 else: | |
79 return self.request_encode_body( | |
80 method, url, fields=fields, headers=headers, **urlopen_kw | |
81 ) | |
82 | |
83 def request_encode_url(self, method, url, fields=None, headers=None, **urlopen_kw): | |
84 """ | |
85 Make a request using :meth:`urlopen` with the ``fields`` encoded in | |
86 the url. This is useful for request methods like GET, HEAD, DELETE, etc. | |
87 """ | |
88 if headers is None: | |
89 headers = self.headers | |
90 | |
91 extra_kw = {"headers": headers} | |
92 extra_kw.update(urlopen_kw) | |
93 | |
94 if fields: | |
95 url += "?" + urlencode(fields) | |
96 | |
97 return self.urlopen(method, url, **extra_kw) | |
98 | |
99 def request_encode_body( | |
100 self, | |
101 method, | |
102 url, | |
103 fields=None, | |
104 headers=None, | |
105 encode_multipart=True, | |
106 multipart_boundary=None, | |
107 **urlopen_kw | |
108 ): | |
109 """ | |
110 Make a request using :meth:`urlopen` with the ``fields`` encoded in | |
111 the body. This is useful for request methods like POST, PUT, PATCH, etc. | |
112 | |
113 When ``encode_multipart=True`` (default), then | |
114 :meth:`urllib3.filepost.encode_multipart_formdata` is used to encode | |
115 the payload with the appropriate content type. Otherwise | |
116 :meth:`urllib.urlencode` is used with the | |
117 'application/x-www-form-urlencoded' content type. | |
118 | |
119 Multipart encoding must be used when posting files, and it's reasonably | |
120 safe to use it in other times too. However, it may break request | |
121 signing, such as with OAuth. | |
122 | |
123 Supports an optional ``fields`` parameter of key/value strings AND | |
124 key/filetuple. A filetuple is a (filename, data, MIME type) tuple where | |
125 the MIME type is optional. For example:: | |
126 | |
127 fields = { | |
128 'foo': 'bar', | |
129 'fakefile': ('foofile.txt', 'contents of foofile'), | |
130 'realfile': ('barfile.txt', open('realfile').read()), | |
131 'typedfile': ('bazfile.bin', open('bazfile').read(), | |
132 'image/jpeg'), | |
133 'nonamefile': 'contents of nonamefile field', | |
134 } | |
135 | |
136 When uploading a file, providing a filename (the first parameter of the | |
137 tuple) is optional but recommended to best mimic behavior of browsers. | |
138 | |
139 Note that if ``headers`` are supplied, the 'Content-Type' header will | |
140 be overwritten because it depends on the dynamic random boundary string | |
141 which is used to compose the body of the request. The random boundary | |
142 string can be explicitly set with the ``multipart_boundary`` parameter. | |
143 """ | |
144 if headers is None: | |
145 headers = self.headers | |
146 | |
147 extra_kw = {"headers": {}} | |
148 | |
149 if fields: | |
150 if "body" in urlopen_kw: | |
151 raise TypeError( | |
152 "request got values for both 'fields' and 'body', can only specify one." | |
153 ) | |
154 | |
155 if encode_multipart: | |
156 body, content_type = encode_multipart_formdata( | |
157 fields, boundary=multipart_boundary | |
158 ) | |
159 else: | |
160 body, content_type = ( | |
161 urlencode(fields), | |
162 "application/x-www-form-urlencoded", | |
163 ) | |
164 | |
165 extra_kw["body"] = body | |
166 extra_kw["headers"] = {"Content-Type": content_type} | |
167 | |
168 extra_kw["headers"].update(headers) | |
169 extra_kw.update(urlopen_kw) | |
170 | |
171 return self.urlopen(method, url, **extra_kw) |