docusign_esign.client.api_response
Docusign eSignature REST API
The Docusign eSignature REST API provides you with a powerful, convenient, and simple Web services API for interacting with Docusign. # noqa: E501
OpenAPI spec version: v2.1 Contact: devcenter@docusign.com Generated by: https://github.com/swagger-api/swagger-codegen.git
1# coding: utf-8 2 3""" 4 Docusign eSignature REST API 5 6 The Docusign eSignature REST API provides you with a powerful, convenient, and simple Web services API for interacting with Docusign. # noqa: E501 7 8 OpenAPI spec version: v2.1 9 Contact: devcenter@docusign.com 10 Generated by: https://github.com/swagger-api/swagger-codegen.git 11""" 12 13 14from __future__ import absolute_import 15 16import io 17import json 18import logging 19import re 20import ssl 21 22import certifi 23# python 2 and python 3 compatibility library 24import six 25from six.moves.urllib.parse import urlencode 26 27from .api_exception import ApiException 28from .configuration import Configuration 29 30try: 31 import urllib3 32except ImportError: 33 raise ImportError('Swagger python client requires urllib3.') 34 35 36logger = logging.getLogger(__name__) 37 38 39class RESTResponse(io.IOBase): 40 41 def __init__(self, resp): 42 self.urllib3_response = resp 43 self.status = resp.status 44 self.reason = resp.reason 45 self.data = resp.data 46 47 def getheaders(self): 48 """Returns a dictionary of the response headers.""" 49 return self.urllib3_response.getheaders() 50 51 def getheader(self, name, default=None): 52 """Returns a given response header.""" 53 return self.urllib3_response.getheader(name, default) 54 55 56class RESTClientObject(object): 57 58 def __init__(self, pools_size=4, maxsize=None, configuration=None): 59 # urllib3.PoolManager will pass all kw parameters to connectionpool 60 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501 61 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501 62 # maxsize is the number of requests to host that are allowed in parallel # noqa: E501 63 # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501 64 65 if configuration is None: 66 configuration = Configuration() 67 68 # cert_reqs 69 if configuration.verify_ssl: 70 cert_reqs = ssl.CERT_REQUIRED 71 else: 72 cert_reqs = ssl.CERT_NONE 73 74 # ca_certs 75 if configuration.ssl_ca_cert: 76 ca_certs = configuration.ssl_ca_cert 77 else: 78 # if not set certificate file, use Mozilla's root certificates. 79 ca_certs = certifi.where() 80 81 addition_pool_args = {} 82 if configuration.assert_hostname is not None: 83 addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501 84 85 if maxsize is None: 86 if configuration.connection_pool_maxsize is not None: 87 maxsize = configuration.connection_pool_maxsize 88 else: 89 maxsize = 4 90 91 # https pool manager 92 if configuration.proxy: 93 self.pool_manager = urllib3.ProxyManager( 94 num_pools=pools_size, 95 maxsize=maxsize, 96 cert_reqs=cert_reqs, 97 ca_certs=ca_certs, 98 cert_file=configuration.cert_file, 99 key_file=configuration.key_file, 100 proxy_url=configuration.proxy, 101 **addition_pool_args 102 ) 103 else: 104 self.pool_manager = urllib3.PoolManager( 105 num_pools=pools_size, 106 maxsize=maxsize, 107 cert_reqs=cert_reqs, 108 ca_certs=ca_certs, 109 cert_file=configuration.cert_file, 110 key_file=configuration.key_file, 111 **addition_pool_args 112 ) 113 114 def request(self, method, url, query_params=None, headers=None, 115 body=None, post_params=None, _preload_content=True, 116 _request_timeout=None): 117 """Perform requests. 118 119 :param method: http request method 120 :param url: http request url 121 :param query_params: query parameters in the url 122 :param headers: http request headers 123 :param body: request json body, for `application/json` 124 :param post_params: request post parameters, 125 `application/x-www-form-urlencoded` 126 and `multipart/form-data` 127 :param _preload_content: if False, the urllib3.HTTPResponse object will 128 be returned without reading/decoding response 129 data. Default is True. 130 :param _request_timeout: timeout setting for this request. If one 131 number provided, it will be total request 132 timeout. It can also be a pair (tuple) of 133 (connection, read) timeouts. 134 """ 135 method = method.upper() 136 assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 137 'PATCH', 'OPTIONS'] 138 139 if post_params and body: 140 raise ValueError( 141 "body parameter cannot be used with post_params parameter." 142 ) 143 144 post_params = post_params or {} 145 headers = headers or {} 146 147 timeout = None 148 if _request_timeout: 149 if isinstance(_request_timeout, (int, ) if six.PY3 else (int, long)): # noqa: E501,F821 150 timeout = urllib3.Timeout(total=_request_timeout) 151 elif (isinstance(_request_timeout, tuple) and 152 len(_request_timeout) == 2): 153 timeout = urllib3.Timeout( 154 connect=_request_timeout[0], read=_request_timeout[1]) 155 156 if 'Content-Type' not in headers: 157 headers['Content-Type'] = 'application/json' 158 159 try: 160 # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` 161 if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: 162 if query_params: 163 url += '?' + urlencode(query_params) 164 if re.search('json', headers['Content-Type'], re.IGNORECASE): 165 request_body = '{}' 166 if body is not None: 167 request_body = json.dumps(body) 168 r = self.pool_manager.request( 169 method, url, 170 body=request_body, 171 preload_content=_preload_content, 172 timeout=timeout, 173 headers=headers) 174 elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501 175 r = self.pool_manager.request( 176 method, url, 177 fields=post_params, 178 encode_multipart=False, 179 preload_content=_preload_content, 180 timeout=timeout, 181 headers=headers) 182 elif headers['Content-Type'] == 'multipart/form-data': 183 # must del headers['Content-Type'], or the correct 184 # Content-Type which generated by urllib3 will be 185 # overwritten. 186 del headers['Content-Type'] 187 188 body = None 189 fields = post_params 190 191 # Custom handling for 'multipart/form-data' when there's a single file 192 # The standard approach is not used in this case to avoid overwriting 193 # the 'Content-Type' and 'Content-Disposition' headers generated by urllib3. 194 if len(post_params) == 1: 195 [k, v] = post_params[0] 196 [file_name, file_data, mime_type] = v 197 198 if isinstance(file_data, bytes): 199 # If the file data is bytes, customize headers and set body 200 headers['Content-Type'] = mime_type 201 headers['Content-Disposition'] = 'form-data; name="' + k + '"; filename="' + file_name + '"' 202 body = file_data 203 fields = None # Clear fields when there is only one file 204 205 r = self.pool_manager.request(method, url, 206 fields=fields, 207 body=body, 208 encode_multipart=True, 209 preload_content=_preload_content, 210 timeout=timeout, 211 headers=headers) 212 # Pass a `string` parameter directly in the body to support 213 # other content types than Json when `body` argument is 214 # provided in serialized form 215 elif isinstance(body, str): 216 request_body = body 217 r = self.pool_manager.request( 218 method, url, 219 body=request_body, 220 preload_content=_preload_content, 221 timeout=timeout, 222 headers=headers) 223 else: 224 # Cannot generate the request from given parameters 225 msg = """Cannot prepare a request message for provided 226 arguments. Please check that your arguments match 227 declared content type.""" 228 raise ApiException(status=0, reason=msg) 229 # For `GET`, `HEAD` 230 else: 231 r = self.pool_manager.request(method, url, 232 fields=query_params, 233 preload_content=_preload_content, 234 timeout=timeout, 235 headers=headers) 236 except urllib3.exceptions.SSLError as e: 237 msg = "{0}\n{1}".format(type(e).__name__, str(e)) 238 raise ApiException(status=0, reason=msg) 239 240 # log response body 241 logger.debug("response body: %s", r.data) 242 243 if _preload_content: 244 r = RESTResponse(r) 245 246 if not 200 <= r.status <= 299: 247 raise ApiException(http_resp=r) 248 249 return r 250 251 def GET(self, url, headers=None, query_params=None, _preload_content=True, 252 _request_timeout=None): 253 return self.request("GET", url, 254 headers=headers, 255 _preload_content=_preload_content, 256 _request_timeout=_request_timeout, 257 query_params=query_params) 258 259 def HEAD(self, url, headers=None, query_params=None, _preload_content=True, 260 _request_timeout=None): 261 return self.request("HEAD", url, 262 headers=headers, 263 _preload_content=_preload_content, 264 _request_timeout=_request_timeout, 265 query_params=query_params) 266 267 def OPTIONS(self, url, headers=None, query_params=None, post_params=None, 268 body=None, _preload_content=True, _request_timeout=None): 269 return self.request("OPTIONS", url, 270 headers=headers, 271 query_params=query_params, 272 post_params=post_params, 273 _preload_content=_preload_content, 274 _request_timeout=_request_timeout, 275 body=body) 276 277 def DELETE(self, url, headers=None, query_params=None, body=None, 278 _preload_content=True, _request_timeout=None): 279 return self.request("DELETE", url, 280 headers=headers, 281 query_params=query_params, 282 _preload_content=_preload_content, 283 _request_timeout=_request_timeout, 284 body=body) 285 286 def POST(self, url, headers=None, query_params=None, post_params=None, 287 body=None, _preload_content=True, _request_timeout=None): 288 return self.request("POST", url, 289 headers=headers, 290 query_params=query_params, 291 post_params=post_params, 292 _preload_content=_preload_content, 293 _request_timeout=_request_timeout, 294 body=body) 295 296 def PUT(self, url, headers=None, query_params=None, post_params=None, 297 body=None, _preload_content=True, _request_timeout=None): 298 return self.request("PUT", url, 299 headers=headers, 300 query_params=query_params, 301 post_params=post_params, 302 _preload_content=_preload_content, 303 _request_timeout=_request_timeout, 304 body=body) 305 306 def PATCH(self, url, headers=None, query_params=None, post_params=None, 307 body=None, _preload_content=True, _request_timeout=None): 308 return self.request("PATCH", url, 309 headers=headers, 310 query_params=query_params, 311 post_params=post_params, 312 _preload_content=_preload_content, 313 _request_timeout=_request_timeout, 314 body=body)
40class RESTResponse(io.IOBase): 41 42 def __init__(self, resp): 43 self.urllib3_response = resp 44 self.status = resp.status 45 self.reason = resp.reason 46 self.data = resp.data 47 48 def getheaders(self): 49 """Returns a dictionary of the response headers.""" 50 return self.urllib3_response.getheaders() 51 52 def getheader(self, name, default=None): 53 """Returns a given response header.""" 54 return self.urllib3_response.getheader(name, default)
The abstract base class for all I/O classes, acting on streams of bytes. There is no public constructor.
This class provides dummy implementations for many methods that derived classes can override selectively; the default implementations represent a file that cannot be read, written or seeked.
Even though IOBase does not declare read, readinto, or write because their signatures will vary, implementations and clients should consider those methods part of the interface. Also, implementations may raise UnsupportedOperation when operations they do not support are called.
The basic type used for binary data read from or written to a file is bytes. Other bytes-like objects are accepted as method arguments too. In some cases (such as readinto), a writable object is required. Text I/O classes work with str data.
Note that calling any method (except additional calls to close(), which are ignored) on a closed stream should raise a ValueError.
IOBase (and its subclasses) support the iterator protocol, meaning that an IOBase object can be iterated over yielding the lines in a stream.
IOBase also supports the :keyword:with
statement. In this example,
fp is closed after the suite of the with statement is complete:
with open('spam.txt', 'r') as fp: fp.write('Spam and eggs!')
48 def getheaders(self): 49 """Returns a dictionary of the response headers.""" 50 return self.urllib3_response.getheaders()
Returns a dictionary of the response headers.
52 def getheader(self, name, default=None): 53 """Returns a given response header.""" 54 return self.urllib3_response.getheader(name, default)
Returns a given response header.
Inherited Members
- _io._IOBase
- seek
- tell
- truncate
- flush
- close
- seekable
- readable
- writable
- fileno
- isatty
- readline
- readlines
- writelines
- closed
57class RESTClientObject(object): 58 59 def __init__(self, pools_size=4, maxsize=None, configuration=None): 60 # urllib3.PoolManager will pass all kw parameters to connectionpool 61 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501 62 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501 63 # maxsize is the number of requests to host that are allowed in parallel # noqa: E501 64 # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501 65 66 if configuration is None: 67 configuration = Configuration() 68 69 # cert_reqs 70 if configuration.verify_ssl: 71 cert_reqs = ssl.CERT_REQUIRED 72 else: 73 cert_reqs = ssl.CERT_NONE 74 75 # ca_certs 76 if configuration.ssl_ca_cert: 77 ca_certs = configuration.ssl_ca_cert 78 else: 79 # if not set certificate file, use Mozilla's root certificates. 80 ca_certs = certifi.where() 81 82 addition_pool_args = {} 83 if configuration.assert_hostname is not None: 84 addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501 85 86 if maxsize is None: 87 if configuration.connection_pool_maxsize is not None: 88 maxsize = configuration.connection_pool_maxsize 89 else: 90 maxsize = 4 91 92 # https pool manager 93 if configuration.proxy: 94 self.pool_manager = urllib3.ProxyManager( 95 num_pools=pools_size, 96 maxsize=maxsize, 97 cert_reqs=cert_reqs, 98 ca_certs=ca_certs, 99 cert_file=configuration.cert_file, 100 key_file=configuration.key_file, 101 proxy_url=configuration.proxy, 102 **addition_pool_args 103 ) 104 else: 105 self.pool_manager = urllib3.PoolManager( 106 num_pools=pools_size, 107 maxsize=maxsize, 108 cert_reqs=cert_reqs, 109 ca_certs=ca_certs, 110 cert_file=configuration.cert_file, 111 key_file=configuration.key_file, 112 **addition_pool_args 113 ) 114 115 def request(self, method, url, query_params=None, headers=None, 116 body=None, post_params=None, _preload_content=True, 117 _request_timeout=None): 118 """Perform requests. 119 120 :param method: http request method 121 :param url: http request url 122 :param query_params: query parameters in the url 123 :param headers: http request headers 124 :param body: request json body, for `application/json` 125 :param post_params: request post parameters, 126 `application/x-www-form-urlencoded` 127 and `multipart/form-data` 128 :param _preload_content: if False, the urllib3.HTTPResponse object will 129 be returned without reading/decoding response 130 data. Default is True. 131 :param _request_timeout: timeout setting for this request. If one 132 number provided, it will be total request 133 timeout. It can also be a pair (tuple) of 134 (connection, read) timeouts. 135 """ 136 method = method.upper() 137 assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 138 'PATCH', 'OPTIONS'] 139 140 if post_params and body: 141 raise ValueError( 142 "body parameter cannot be used with post_params parameter." 143 ) 144 145 post_params = post_params or {} 146 headers = headers or {} 147 148 timeout = None 149 if _request_timeout: 150 if isinstance(_request_timeout, (int, ) if six.PY3 else (int, long)): # noqa: E501,F821 151 timeout = urllib3.Timeout(total=_request_timeout) 152 elif (isinstance(_request_timeout, tuple) and 153 len(_request_timeout) == 2): 154 timeout = urllib3.Timeout( 155 connect=_request_timeout[0], read=_request_timeout[1]) 156 157 if 'Content-Type' not in headers: 158 headers['Content-Type'] = 'application/json' 159 160 try: 161 # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` 162 if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: 163 if query_params: 164 url += '?' + urlencode(query_params) 165 if re.search('json', headers['Content-Type'], re.IGNORECASE): 166 request_body = '{}' 167 if body is not None: 168 request_body = json.dumps(body) 169 r = self.pool_manager.request( 170 method, url, 171 body=request_body, 172 preload_content=_preload_content, 173 timeout=timeout, 174 headers=headers) 175 elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501 176 r = self.pool_manager.request( 177 method, url, 178 fields=post_params, 179 encode_multipart=False, 180 preload_content=_preload_content, 181 timeout=timeout, 182 headers=headers) 183 elif headers['Content-Type'] == 'multipart/form-data': 184 # must del headers['Content-Type'], or the correct 185 # Content-Type which generated by urllib3 will be 186 # overwritten. 187 del headers['Content-Type'] 188 189 body = None 190 fields = post_params 191 192 # Custom handling for 'multipart/form-data' when there's a single file 193 # The standard approach is not used in this case to avoid overwriting 194 # the 'Content-Type' and 'Content-Disposition' headers generated by urllib3. 195 if len(post_params) == 1: 196 [k, v] = post_params[0] 197 [file_name, file_data, mime_type] = v 198 199 if isinstance(file_data, bytes): 200 # If the file data is bytes, customize headers and set body 201 headers['Content-Type'] = mime_type 202 headers['Content-Disposition'] = 'form-data; name="' + k + '"; filename="' + file_name + '"' 203 body = file_data 204 fields = None # Clear fields when there is only one file 205 206 r = self.pool_manager.request(method, url, 207 fields=fields, 208 body=body, 209 encode_multipart=True, 210 preload_content=_preload_content, 211 timeout=timeout, 212 headers=headers) 213 # Pass a `string` parameter directly in the body to support 214 # other content types than Json when `body` argument is 215 # provided in serialized form 216 elif isinstance(body, str): 217 request_body = body 218 r = self.pool_manager.request( 219 method, url, 220 body=request_body, 221 preload_content=_preload_content, 222 timeout=timeout, 223 headers=headers) 224 else: 225 # Cannot generate the request from given parameters 226 msg = """Cannot prepare a request message for provided 227 arguments. Please check that your arguments match 228 declared content type.""" 229 raise ApiException(status=0, reason=msg) 230 # For `GET`, `HEAD` 231 else: 232 r = self.pool_manager.request(method, url, 233 fields=query_params, 234 preload_content=_preload_content, 235 timeout=timeout, 236 headers=headers) 237 except urllib3.exceptions.SSLError as e: 238 msg = "{0}\n{1}".format(type(e).__name__, str(e)) 239 raise ApiException(status=0, reason=msg) 240 241 # log response body 242 logger.debug("response body: %s", r.data) 243 244 if _preload_content: 245 r = RESTResponse(r) 246 247 if not 200 <= r.status <= 299: 248 raise ApiException(http_resp=r) 249 250 return r 251 252 def GET(self, url, headers=None, query_params=None, _preload_content=True, 253 _request_timeout=None): 254 return self.request("GET", url, 255 headers=headers, 256 _preload_content=_preload_content, 257 _request_timeout=_request_timeout, 258 query_params=query_params) 259 260 def HEAD(self, url, headers=None, query_params=None, _preload_content=True, 261 _request_timeout=None): 262 return self.request("HEAD", url, 263 headers=headers, 264 _preload_content=_preload_content, 265 _request_timeout=_request_timeout, 266 query_params=query_params) 267 268 def OPTIONS(self, url, headers=None, query_params=None, post_params=None, 269 body=None, _preload_content=True, _request_timeout=None): 270 return self.request("OPTIONS", url, 271 headers=headers, 272 query_params=query_params, 273 post_params=post_params, 274 _preload_content=_preload_content, 275 _request_timeout=_request_timeout, 276 body=body) 277 278 def DELETE(self, url, headers=None, query_params=None, body=None, 279 _preload_content=True, _request_timeout=None): 280 return self.request("DELETE", url, 281 headers=headers, 282 query_params=query_params, 283 _preload_content=_preload_content, 284 _request_timeout=_request_timeout, 285 body=body) 286 287 def POST(self, url, headers=None, query_params=None, post_params=None, 288 body=None, _preload_content=True, _request_timeout=None): 289 return self.request("POST", url, 290 headers=headers, 291 query_params=query_params, 292 post_params=post_params, 293 _preload_content=_preload_content, 294 _request_timeout=_request_timeout, 295 body=body) 296 297 def PUT(self, url, headers=None, query_params=None, post_params=None, 298 body=None, _preload_content=True, _request_timeout=None): 299 return self.request("PUT", url, 300 headers=headers, 301 query_params=query_params, 302 post_params=post_params, 303 _preload_content=_preload_content, 304 _request_timeout=_request_timeout, 305 body=body) 306 307 def PATCH(self, url, headers=None, query_params=None, post_params=None, 308 body=None, _preload_content=True, _request_timeout=None): 309 return self.request("PATCH", url, 310 headers=headers, 311 query_params=query_params, 312 post_params=post_params, 313 _preload_content=_preload_content, 314 _request_timeout=_request_timeout, 315 body=body)
59 def __init__(self, pools_size=4, maxsize=None, configuration=None): 60 # urllib3.PoolManager will pass all kw parameters to connectionpool 61 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501 62 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501 63 # maxsize is the number of requests to host that are allowed in parallel # noqa: E501 64 # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501 65 66 if configuration is None: 67 configuration = Configuration() 68 69 # cert_reqs 70 if configuration.verify_ssl: 71 cert_reqs = ssl.CERT_REQUIRED 72 else: 73 cert_reqs = ssl.CERT_NONE 74 75 # ca_certs 76 if configuration.ssl_ca_cert: 77 ca_certs = configuration.ssl_ca_cert 78 else: 79 # if not set certificate file, use Mozilla's root certificates. 80 ca_certs = certifi.where() 81 82 addition_pool_args = {} 83 if configuration.assert_hostname is not None: 84 addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501 85 86 if maxsize is None: 87 if configuration.connection_pool_maxsize is not None: 88 maxsize = configuration.connection_pool_maxsize 89 else: 90 maxsize = 4 91 92 # https pool manager 93 if configuration.proxy: 94 self.pool_manager = urllib3.ProxyManager( 95 num_pools=pools_size, 96 maxsize=maxsize, 97 cert_reqs=cert_reqs, 98 ca_certs=ca_certs, 99 cert_file=configuration.cert_file, 100 key_file=configuration.key_file, 101 proxy_url=configuration.proxy, 102 **addition_pool_args 103 ) 104 else: 105 self.pool_manager = urllib3.PoolManager( 106 num_pools=pools_size, 107 maxsize=maxsize, 108 cert_reqs=cert_reqs, 109 ca_certs=ca_certs, 110 cert_file=configuration.cert_file, 111 key_file=configuration.key_file, 112 **addition_pool_args 113 )
115 def request(self, method, url, query_params=None, headers=None, 116 body=None, post_params=None, _preload_content=True, 117 _request_timeout=None): 118 """Perform requests. 119 120 :param method: http request method 121 :param url: http request url 122 :param query_params: query parameters in the url 123 :param headers: http request headers 124 :param body: request json body, for `application/json` 125 :param post_params: request post parameters, 126 `application/x-www-form-urlencoded` 127 and `multipart/form-data` 128 :param _preload_content: if False, the urllib3.HTTPResponse object will 129 be returned without reading/decoding response 130 data. Default is True. 131 :param _request_timeout: timeout setting for this request. If one 132 number provided, it will be total request 133 timeout. It can also be a pair (tuple) of 134 (connection, read) timeouts. 135 """ 136 method = method.upper() 137 assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 138 'PATCH', 'OPTIONS'] 139 140 if post_params and body: 141 raise ValueError( 142 "body parameter cannot be used with post_params parameter." 143 ) 144 145 post_params = post_params or {} 146 headers = headers or {} 147 148 timeout = None 149 if _request_timeout: 150 if isinstance(_request_timeout, (int, ) if six.PY3 else (int, long)): # noqa: E501,F821 151 timeout = urllib3.Timeout(total=_request_timeout) 152 elif (isinstance(_request_timeout, tuple) and 153 len(_request_timeout) == 2): 154 timeout = urllib3.Timeout( 155 connect=_request_timeout[0], read=_request_timeout[1]) 156 157 if 'Content-Type' not in headers: 158 headers['Content-Type'] = 'application/json' 159 160 try: 161 # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` 162 if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: 163 if query_params: 164 url += '?' + urlencode(query_params) 165 if re.search('json', headers['Content-Type'], re.IGNORECASE): 166 request_body = '{}' 167 if body is not None: 168 request_body = json.dumps(body) 169 r = self.pool_manager.request( 170 method, url, 171 body=request_body, 172 preload_content=_preload_content, 173 timeout=timeout, 174 headers=headers) 175 elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501 176 r = self.pool_manager.request( 177 method, url, 178 fields=post_params, 179 encode_multipart=False, 180 preload_content=_preload_content, 181 timeout=timeout, 182 headers=headers) 183 elif headers['Content-Type'] == 'multipart/form-data': 184 # must del headers['Content-Type'], or the correct 185 # Content-Type which generated by urllib3 will be 186 # overwritten. 187 del headers['Content-Type'] 188 189 body = None 190 fields = post_params 191 192 # Custom handling for 'multipart/form-data' when there's a single file 193 # The standard approach is not used in this case to avoid overwriting 194 # the 'Content-Type' and 'Content-Disposition' headers generated by urllib3. 195 if len(post_params) == 1: 196 [k, v] = post_params[0] 197 [file_name, file_data, mime_type] = v 198 199 if isinstance(file_data, bytes): 200 # If the file data is bytes, customize headers and set body 201 headers['Content-Type'] = mime_type 202 headers['Content-Disposition'] = 'form-data; name="' + k + '"; filename="' + file_name + '"' 203 body = file_data 204 fields = None # Clear fields when there is only one file 205 206 r = self.pool_manager.request(method, url, 207 fields=fields, 208 body=body, 209 encode_multipart=True, 210 preload_content=_preload_content, 211 timeout=timeout, 212 headers=headers) 213 # Pass a `string` parameter directly in the body to support 214 # other content types than Json when `body` argument is 215 # provided in serialized form 216 elif isinstance(body, str): 217 request_body = body 218 r = self.pool_manager.request( 219 method, url, 220 body=request_body, 221 preload_content=_preload_content, 222 timeout=timeout, 223 headers=headers) 224 else: 225 # Cannot generate the request from given parameters 226 msg = """Cannot prepare a request message for provided 227 arguments. Please check that your arguments match 228 declared content type.""" 229 raise ApiException(status=0, reason=msg) 230 # For `GET`, `HEAD` 231 else: 232 r = self.pool_manager.request(method, url, 233 fields=query_params, 234 preload_content=_preload_content, 235 timeout=timeout, 236 headers=headers) 237 except urllib3.exceptions.SSLError as e: 238 msg = "{0}\n{1}".format(type(e).__name__, str(e)) 239 raise ApiException(status=0, reason=msg) 240 241 # log response body 242 logger.debug("response body: %s", r.data) 243 244 if _preload_content: 245 r = RESTResponse(r) 246 247 if not 200 <= r.status <= 299: 248 raise ApiException(http_resp=r) 249 250 return r
Perform requests.
Parameters
- method: http request method
- url: http request url
- query_params: query parameters in the url
- headers: http request headers
- body: request json body, for
application/json
- post_params: request post parameters,
application/x-www-form-urlencoded
andmultipart/form-data
- _preload_content: if False, the urllib3.HTTPResponse object will be returned without reading/decoding response data. Default is True.
- _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts.
268 def OPTIONS(self, url, headers=None, query_params=None, post_params=None, 269 body=None, _preload_content=True, _request_timeout=None): 270 return self.request("OPTIONS", url, 271 headers=headers, 272 query_params=query_params, 273 post_params=post_params, 274 _preload_content=_preload_content, 275 _request_timeout=_request_timeout, 276 body=body)
278 def DELETE(self, url, headers=None, query_params=None, body=None, 279 _preload_content=True, _request_timeout=None): 280 return self.request("DELETE", url, 281 headers=headers, 282 query_params=query_params, 283 _preload_content=_preload_content, 284 _request_timeout=_request_timeout, 285 body=body)
287 def POST(self, url, headers=None, query_params=None, post_params=None, 288 body=None, _preload_content=True, _request_timeout=None): 289 return self.request("POST", url, 290 headers=headers, 291 query_params=query_params, 292 post_params=post_params, 293 _preload_content=_preload_content, 294 _request_timeout=_request_timeout, 295 body=body)
297 def PUT(self, url, headers=None, query_params=None, post_params=None, 298 body=None, _preload_content=True, _request_timeout=None): 299 return self.request("PUT", url, 300 headers=headers, 301 query_params=query_params, 302 post_params=post_params, 303 _preload_content=_preload_content, 304 _request_timeout=_request_timeout, 305 body=body)
307 def PATCH(self, url, headers=None, query_params=None, post_params=None, 308 body=None, _preload_content=True, _request_timeout=None): 309 return self.request("PATCH", url, 310 headers=headers, 311 query_params=query_params, 312 post_params=post_params, 313 _preload_content=_preload_content, 314 _request_timeout=_request_timeout, 315 body=body)