forked from loafle/openapi-generator-original
Merge pull request #1132 from geekerzp/python-configuration
[Python] Add option `verify_ssl` to switch SSL/TLS verification
This commit is contained in:
commit
9e5de7da5e
@ -43,7 +43,7 @@ class {{classname}}(object):
|
|||||||
self.api_client = api_client
|
self.api_client = api_client
|
||||||
else:
|
else:
|
||||||
if not config.api_client:
|
if not config.api_client:
|
||||||
config.api_client = ApiClient('{{basePath}}')
|
config.api_client = ApiClient()
|
||||||
self.api_client = config.api_client
|
self.api_client = config.api_client
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ Copyright 2015 SmartBear Software
|
|||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from . import models
|
from . import models
|
||||||
from .rest import RESTClient
|
from .rest import RESTClientObject
|
||||||
from .rest import ApiException
|
from .rest import ApiException
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@ -71,6 +71,7 @@ class ApiClient(object):
|
|||||||
"""
|
"""
|
||||||
Constructor of the class.
|
Constructor of the class.
|
||||||
"""
|
"""
|
||||||
|
self.rest_client = RESTClientObject()
|
||||||
self.default_headers = {}
|
self.default_headers = {}
|
||||||
if header_name is not None:
|
if header_name is not None:
|
||||||
self.default_headers[header_name] = header_value
|
self.default_headers[header_name] = header_value
|
||||||
@ -330,35 +331,35 @@ class ApiClient(object):
|
|||||||
Makes the HTTP request using RESTClient.
|
Makes the HTTP request using RESTClient.
|
||||||
"""
|
"""
|
||||||
if method == "GET":
|
if method == "GET":
|
||||||
return RESTClient.GET(url,
|
return self.rest_client.GET(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
elif method == "HEAD":
|
elif method == "HEAD":
|
||||||
return RESTClient.HEAD(url,
|
return self.rest_client.HEAD(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
elif method == "POST":
|
elif method == "POST":
|
||||||
return RESTClient.POST(url,
|
return self.rest_client.POST(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
post_params=post_params,
|
post_params=post_params,
|
||||||
body=body)
|
body=body)
|
||||||
elif method == "PUT":
|
elif method == "PUT":
|
||||||
return RESTClient.PUT(url,
|
return self.rest_client.PUT(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
post_params=post_params,
|
post_params=post_params,
|
||||||
body=body)
|
body=body)
|
||||||
elif method == "PATCH":
|
elif method == "PATCH":
|
||||||
return RESTClient.PATCH(url,
|
return self.rest_client.PATCH(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
post_params=post_params,
|
post_params=post_params,
|
||||||
body=body)
|
body=body)
|
||||||
elif method == "DELETE":
|
elif method == "DELETE":
|
||||||
return RESTClient.DELETE(url,
|
return self.rest_client.DELETE(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"http method must be `GET`, `HEAD`,"
|
"http method must be `GET`, `HEAD`,"
|
||||||
|
@ -79,6 +79,12 @@ class Configuration(object):
|
|||||||
self.__debug = False
|
self.__debug = False
|
||||||
self.init_logger()
|
self.init_logger()
|
||||||
|
|
||||||
|
# SSL/TLS verification
|
||||||
|
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
||||||
|
self.verify_ssl = True
|
||||||
|
# Set this to customize the certificate file to verify the peer.
|
||||||
|
self.ssl_ca_cert = None
|
||||||
|
|
||||||
def init_logger(self):
|
def init_logger(self):
|
||||||
"""
|
"""
|
||||||
Initializes logger settings.
|
Initializes logger settings.
|
||||||
|
@ -18,6 +18,7 @@ Copyright 2015 SmartBear Software
|
|||||||
Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK:
|
Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK:
|
||||||
https://www.dropbox.com/developers/core/sdks/python
|
https://www.dropbox.com/developers/core/sdks/python
|
||||||
"""
|
"""
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import io
|
import io
|
||||||
@ -29,6 +30,8 @@ import logging
|
|||||||
# python 2 and python 3 compatibility library
|
# python 2 and python 3 compatibility library
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
|
||||||
|
from .configuration import Configuration
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib3
|
import urllib3
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -69,31 +72,24 @@ class RESTResponse(io.IOBase):
|
|||||||
class RESTClientObject(object):
|
class RESTClientObject(object):
|
||||||
|
|
||||||
def __init__(self, pools_size=4):
|
def __init__(self, pools_size=4):
|
||||||
# http pool manager
|
if Configuration().verify_ssl:
|
||||||
self.pool_manager = urllib3.PoolManager(
|
cert_reqs = ssl.CERT_REQUIRED
|
||||||
num_pools=pools_size
|
else:
|
||||||
)
|
cert_reqs = ssl.CERT_NONE
|
||||||
|
|
||||||
|
if Configuration().ssl_ca_cert:
|
||||||
|
ca_certs = Configuration().ssl_ca_cert
|
||||||
|
else:
|
||||||
|
# if not set certificate file, use Mozilla's root certificates.
|
||||||
|
ca_certs = certifi.where()
|
||||||
|
|
||||||
# https pool manager
|
# https pool manager
|
||||||
# certificates validated using Mozilla’s root certificates
|
self.pool_manager = urllib3.PoolManager(
|
||||||
self.ssl_pool_manager = urllib3.PoolManager(
|
|
||||||
num_pools=pools_size,
|
num_pools=pools_size,
|
||||||
cert_reqs=ssl.CERT_REQUIRED,
|
cert_reqs=cert_reqs,
|
||||||
ca_certs=certifi.where()
|
ca_certs=ca_certs
|
||||||
)
|
)
|
||||||
|
|
||||||
def agent(self, url):
|
|
||||||
"""
|
|
||||||
Use `urllib3.util.parse_url` for backward compatibility.
|
|
||||||
Return proper pool manager for the http/https schemes.
|
|
||||||
"""
|
|
||||||
url = urllib3.util.parse_url(url)
|
|
||||||
scheme = url.scheme
|
|
||||||
if scheme == 'https':
|
|
||||||
return self.ssl_pool_manager
|
|
||||||
else:
|
|
||||||
return self.pool_manager
|
|
||||||
|
|
||||||
def request(self, method, url, query_params=None, headers=None,
|
def request(self, method, url, query_params=None, headers=None,
|
||||||
body=None, post_params=None):
|
body=None, post_params=None):
|
||||||
"""
|
"""
|
||||||
@ -120,32 +116,37 @@ class RESTClientObject(object):
|
|||||||
if 'Content-Type' not in headers:
|
if 'Content-Type' not in headers:
|
||||||
headers['Content-Type'] = 'application/json'
|
headers['Content-Type'] = 'application/json'
|
||||||
|
|
||||||
# For `POST`, `PUT`, `PATCH`
|
try:
|
||||||
if method in ['POST', 'PUT', 'PATCH']:
|
# For `POST`, `PUT`, `PATCH`
|
||||||
if query_params:
|
if method in ['POST', 'PUT', 'PATCH']:
|
||||||
url += '?' + urlencode(query_params)
|
if query_params:
|
||||||
if headers['Content-Type'] == 'application/json':
|
url += '?' + urlencode(query_params)
|
||||||
r = self.agent(url).request(method, url,
|
if headers['Content-Type'] == 'application/json':
|
||||||
body=json.dumps(body),
|
r = self.pool_manager.request(method, url,
|
||||||
headers=headers)
|
body=json.dumps(body),
|
||||||
if headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
headers=headers)
|
||||||
r = self.agent(url).request(method, url,
|
if headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||||
fields=post_params,
|
r = self.pool_manager.request(method, url,
|
||||||
encode_multipart=False,
|
fields=post_params,
|
||||||
headers=headers)
|
encode_multipart=False,
|
||||||
if headers['Content-Type'] == 'multipart/form-data':
|
headers=headers)
|
||||||
# must del headers['Content-Type'], or the correct Content-Type
|
if headers['Content-Type'] == 'multipart/form-data':
|
||||||
# which generated by urllib3 will be overwritten.
|
# must del headers['Content-Type'], or the correct Content-Type
|
||||||
del headers['Content-Type']
|
# which generated by urllib3 will be overwritten.
|
||||||
r = self.agent(url).request(method, url,
|
del headers['Content-Type']
|
||||||
fields=post_params,
|
r = self.pool_manager.request(method, url,
|
||||||
encode_multipart=True,
|
fields=post_params,
|
||||||
headers=headers)
|
encode_multipart=True,
|
||||||
# For `GET`, `HEAD`, `DELETE`
|
headers=headers)
|
||||||
else:
|
# For `GET`, `HEAD`, `DELETE`
|
||||||
r = self.agent(url).request(method, url,
|
else:
|
||||||
fields=query_params,
|
r = self.pool_manager.request(method, url,
|
||||||
headers=headers)
|
fields=query_params,
|
||||||
|
headers=headers)
|
||||||
|
except urllib3.exceptions.SSLError as e:
|
||||||
|
msg = "{0}\n{1}".format(type(e).__name__, str(e))
|
||||||
|
raise ApiException(status=0, reason=msg)
|
||||||
|
|
||||||
r = RESTResponse(r)
|
r = RESTResponse(r)
|
||||||
|
|
||||||
# In the python 3, the response.data is bytes.
|
# In the python 3, the response.data is bytes.
|
||||||
@ -227,58 +228,20 @@ class ApiException(Exception):
|
|||||||
return error_message
|
return error_message
|
||||||
|
|
||||||
|
|
||||||
class RESTClient(object):
|
|
||||||
"""
|
|
||||||
A class with all class methods to perform JSON requests.
|
|
||||||
"""
|
|
||||||
|
|
||||||
IMPL = RESTClientObject()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def request(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a REST request and parse the response.
|
|
||||||
"""
|
|
||||||
return cls.IMPL.request(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def GET(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a GET request using `RESTClient.request()`.
|
|
||||||
"""
|
|
||||||
return cls.IMPL.GET(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def HEAD(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a HEAD request using `RESTClient.request()`.
|
|
||||||
"""
|
|
||||||
return cls.IMPL.GET(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def POST(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a POST request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.POST(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def PUT(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a PUT request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.PUT(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def PATCH(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a PATCH request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.PATCH(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def DELETE(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a DELETE request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.DELETE(*n, **kw)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ Copyright 2015 SmartBear Software
|
|||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from . import models
|
from . import models
|
||||||
from .rest import RESTClient
|
from .rest import RESTClientObject
|
||||||
from .rest import ApiException
|
from .rest import ApiException
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@ -71,6 +71,7 @@ class ApiClient(object):
|
|||||||
"""
|
"""
|
||||||
Constructor of the class.
|
Constructor of the class.
|
||||||
"""
|
"""
|
||||||
|
self.rest_client = RESTClientObject()
|
||||||
self.default_headers = {}
|
self.default_headers = {}
|
||||||
if header_name is not None:
|
if header_name is not None:
|
||||||
self.default_headers[header_name] = header_value
|
self.default_headers[header_name] = header_value
|
||||||
@ -330,35 +331,35 @@ class ApiClient(object):
|
|||||||
Makes the HTTP request using RESTClient.
|
Makes the HTTP request using RESTClient.
|
||||||
"""
|
"""
|
||||||
if method == "GET":
|
if method == "GET":
|
||||||
return RESTClient.GET(url,
|
return self.rest_client.GET(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
elif method == "HEAD":
|
elif method == "HEAD":
|
||||||
return RESTClient.HEAD(url,
|
return self.rest_client.HEAD(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
elif method == "POST":
|
elif method == "POST":
|
||||||
return RESTClient.POST(url,
|
return self.rest_client.POST(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
post_params=post_params,
|
post_params=post_params,
|
||||||
body=body)
|
body=body)
|
||||||
elif method == "PUT":
|
elif method == "PUT":
|
||||||
return RESTClient.PUT(url,
|
return self.rest_client.PUT(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
post_params=post_params,
|
post_params=post_params,
|
||||||
body=body)
|
body=body)
|
||||||
elif method == "PATCH":
|
elif method == "PATCH":
|
||||||
return RESTClient.PATCH(url,
|
return self.rest_client.PATCH(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
post_params=post_params,
|
post_params=post_params,
|
||||||
body=body)
|
body=body)
|
||||||
elif method == "DELETE":
|
elif method == "DELETE":
|
||||||
return RESTClient.DELETE(url,
|
return self.rest_client.DELETE(url,
|
||||||
query_params=query_params,
|
query_params=query_params,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"http method must be `GET`, `HEAD`,"
|
"http method must be `GET`, `HEAD`,"
|
||||||
|
@ -42,7 +42,7 @@ class PetApi(object):
|
|||||||
self.api_client = api_client
|
self.api_client = api_client
|
||||||
else:
|
else:
|
||||||
if not config.api_client:
|
if not config.api_client:
|
||||||
config.api_client = ApiClient('http://petstore.swagger.io/v2')
|
config.api_client = ApiClient()
|
||||||
self.api_client = config.api_client
|
self.api_client = config.api_client
|
||||||
|
|
||||||
def update_pet(self, **kwargs):
|
def update_pet(self, **kwargs):
|
||||||
|
@ -42,7 +42,7 @@ class StoreApi(object):
|
|||||||
self.api_client = api_client
|
self.api_client = api_client
|
||||||
else:
|
else:
|
||||||
if not config.api_client:
|
if not config.api_client:
|
||||||
config.api_client = ApiClient('http://petstore.swagger.io/v2')
|
config.api_client = ApiClient()
|
||||||
self.api_client = config.api_client
|
self.api_client = config.api_client
|
||||||
|
|
||||||
def get_inventory(self, **kwargs):
|
def get_inventory(self, **kwargs):
|
||||||
|
@ -42,7 +42,7 @@ class UserApi(object):
|
|||||||
self.api_client = api_client
|
self.api_client = api_client
|
||||||
else:
|
else:
|
||||||
if not config.api_client:
|
if not config.api_client:
|
||||||
config.api_client = ApiClient('http://petstore.swagger.io/v2')
|
config.api_client = ApiClient()
|
||||||
self.api_client = config.api_client
|
self.api_client = config.api_client
|
||||||
|
|
||||||
def create_user(self, **kwargs):
|
def create_user(self, **kwargs):
|
||||||
|
@ -79,6 +79,12 @@ class Configuration(object):
|
|||||||
self.__debug = False
|
self.__debug = False
|
||||||
self.init_logger()
|
self.init_logger()
|
||||||
|
|
||||||
|
# SSL/TLS verification
|
||||||
|
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
||||||
|
self.verify_ssl = True
|
||||||
|
# Set this to customize the certificate file to verify the peer.
|
||||||
|
self.ssl_ca_cert = None
|
||||||
|
|
||||||
def init_logger(self):
|
def init_logger(self):
|
||||||
"""
|
"""
|
||||||
Initializes logger settings.
|
Initializes logger settings.
|
||||||
|
@ -18,6 +18,7 @@ Copyright 2015 SmartBear Software
|
|||||||
Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK:
|
Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK:
|
||||||
https://www.dropbox.com/developers/core/sdks/python
|
https://www.dropbox.com/developers/core/sdks/python
|
||||||
"""
|
"""
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import io
|
import io
|
||||||
@ -29,6 +30,8 @@ import logging
|
|||||||
# python 2 and python 3 compatibility library
|
# python 2 and python 3 compatibility library
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
|
||||||
|
from .configuration import Configuration
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib3
|
import urllib3
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -69,31 +72,24 @@ class RESTResponse(io.IOBase):
|
|||||||
class RESTClientObject(object):
|
class RESTClientObject(object):
|
||||||
|
|
||||||
def __init__(self, pools_size=4):
|
def __init__(self, pools_size=4):
|
||||||
# http pool manager
|
if Configuration().verify_ssl:
|
||||||
self.pool_manager = urllib3.PoolManager(
|
cert_reqs = ssl.CERT_REQUIRED
|
||||||
num_pools=pools_size
|
else:
|
||||||
)
|
cert_reqs = ssl.CERT_NONE
|
||||||
|
|
||||||
|
if Configuration().ssl_ca_cert:
|
||||||
|
ca_certs = Configuration().ssl_ca_cert
|
||||||
|
else:
|
||||||
|
# if not set certificate file, use Mozilla's root certificates.
|
||||||
|
ca_certs = certifi.where()
|
||||||
|
|
||||||
# https pool manager
|
# https pool manager
|
||||||
# certificates validated using Mozilla’s root certificates
|
self.pool_manager = urllib3.PoolManager(
|
||||||
self.ssl_pool_manager = urllib3.PoolManager(
|
|
||||||
num_pools=pools_size,
|
num_pools=pools_size,
|
||||||
cert_reqs=ssl.CERT_REQUIRED,
|
cert_reqs=cert_reqs,
|
||||||
ca_certs=certifi.where()
|
ca_certs=ca_certs
|
||||||
)
|
)
|
||||||
|
|
||||||
def agent(self, url):
|
|
||||||
"""
|
|
||||||
Use `urllib3.util.parse_url` for backward compatibility.
|
|
||||||
Return proper pool manager for the http/https schemes.
|
|
||||||
"""
|
|
||||||
url = urllib3.util.parse_url(url)
|
|
||||||
scheme = url.scheme
|
|
||||||
if scheme == 'https':
|
|
||||||
return self.ssl_pool_manager
|
|
||||||
else:
|
|
||||||
return self.pool_manager
|
|
||||||
|
|
||||||
def request(self, method, url, query_params=None, headers=None,
|
def request(self, method, url, query_params=None, headers=None,
|
||||||
body=None, post_params=None):
|
body=None, post_params=None):
|
||||||
"""
|
"""
|
||||||
@ -120,32 +116,37 @@ class RESTClientObject(object):
|
|||||||
if 'Content-Type' not in headers:
|
if 'Content-Type' not in headers:
|
||||||
headers['Content-Type'] = 'application/json'
|
headers['Content-Type'] = 'application/json'
|
||||||
|
|
||||||
# For `POST`, `PUT`, `PATCH`
|
try:
|
||||||
if method in ['POST', 'PUT', 'PATCH']:
|
# For `POST`, `PUT`, `PATCH`
|
||||||
if query_params:
|
if method in ['POST', 'PUT', 'PATCH']:
|
||||||
url += '?' + urlencode(query_params)
|
if query_params:
|
||||||
if headers['Content-Type'] == 'application/json':
|
url += '?' + urlencode(query_params)
|
||||||
r = self.agent(url).request(method, url,
|
if headers['Content-Type'] == 'application/json':
|
||||||
body=json.dumps(body),
|
r = self.pool_manager.request(method, url,
|
||||||
headers=headers)
|
body=json.dumps(body),
|
||||||
if headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
headers=headers)
|
||||||
r = self.agent(url).request(method, url,
|
if headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||||
fields=post_params,
|
r = self.pool_manager.request(method, url,
|
||||||
encode_multipart=False,
|
fields=post_params,
|
||||||
headers=headers)
|
encode_multipart=False,
|
||||||
if headers['Content-Type'] == 'multipart/form-data':
|
headers=headers)
|
||||||
# must del headers['Content-Type'], or the correct Content-Type
|
if headers['Content-Type'] == 'multipart/form-data':
|
||||||
# which generated by urllib3 will be overwritten.
|
# must del headers['Content-Type'], or the correct Content-Type
|
||||||
del headers['Content-Type']
|
# which generated by urllib3 will be overwritten.
|
||||||
r = self.agent(url).request(method, url,
|
del headers['Content-Type']
|
||||||
fields=post_params,
|
r = self.pool_manager.request(method, url,
|
||||||
encode_multipart=True,
|
fields=post_params,
|
||||||
headers=headers)
|
encode_multipart=True,
|
||||||
# For `GET`, `HEAD`, `DELETE`
|
headers=headers)
|
||||||
else:
|
# For `GET`, `HEAD`, `DELETE`
|
||||||
r = self.agent(url).request(method, url,
|
else:
|
||||||
fields=query_params,
|
r = self.pool_manager.request(method, url,
|
||||||
headers=headers)
|
fields=query_params,
|
||||||
|
headers=headers)
|
||||||
|
except urllib3.exceptions.SSLError as e:
|
||||||
|
msg = "{0}\n{1}".format(type(e).__name__, str(e))
|
||||||
|
raise ApiException(status=0, reason=msg)
|
||||||
|
|
||||||
r = RESTResponse(r)
|
r = RESTResponse(r)
|
||||||
|
|
||||||
# In the python 3, the response.data is bytes.
|
# In the python 3, the response.data is bytes.
|
||||||
@ -227,58 +228,20 @@ class ApiException(Exception):
|
|||||||
return error_message
|
return error_message
|
||||||
|
|
||||||
|
|
||||||
class RESTClient(object):
|
|
||||||
"""
|
|
||||||
A class with all class methods to perform JSON requests.
|
|
||||||
"""
|
|
||||||
|
|
||||||
IMPL = RESTClientObject()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def request(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a REST request and parse the response.
|
|
||||||
"""
|
|
||||||
return cls.IMPL.request(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def GET(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a GET request using `RESTClient.request()`.
|
|
||||||
"""
|
|
||||||
return cls.IMPL.GET(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def HEAD(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a HEAD request using `RESTClient.request()`.
|
|
||||||
"""
|
|
||||||
return cls.IMPL.GET(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def POST(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a POST request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.POST(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def PUT(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a PUT request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.PUT(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def PATCH(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a PATCH request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.PATCH(*n, **kw)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def DELETE(cls, *n, **kw):
|
|
||||||
"""
|
|
||||||
Perform a DELETE request using `RESTClient.request()`
|
|
||||||
"""
|
|
||||||
return cls.IMPL.DELETE(*n, **kw)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user