forked from loafle/openapi-generator-original
[python] fix typing for API responses (#16802)
* [python] remove _preload_content Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove _return_http_data_only Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove async_req Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix typing for API responses Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update samples Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix AttributeError Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove _preload_content Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix response_type Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix typo Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update simplify RESTClientObject Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update split call_api into 4 functions Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update samples Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update improve stream Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove kwargs Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update ApiResponse Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add method for each return value Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update test Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update samples Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update docs Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add constantParams Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix ImportError Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix SyntaxError in RESTResponse Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add ApiResponse model_config Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix when isBinary is str Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update type Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update samples Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add rest with pydantic v1 Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update format Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update format and type Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add test Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update type to pydantic strict type Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove leftover files Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove descriptions per field Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add test Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix test Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] fix test Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove multiprocessing Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update samples Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] add blank line to docstring Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] update docstring Signed-off-by: ふぁ <yuki@yuki0311.com> * [python] remove unwanted imports in rest Signed-off-by: ふぁ <yuki@yuki0311.com> --------- Signed-off-by: ふぁ <yuki@yuki0311.com>
This commit is contained in:
@@ -15,43 +15,43 @@
|
||||
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import ssl
|
||||
|
||||
from urllib.parse import urlencode, quote_plus
|
||||
import urllib3
|
||||
|
||||
from openapi_client.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from openapi_client.exceptions import ApiException, ApiValueError
|
||||
|
||||
RESTResponseType = urllib3.HTTPResponse
|
||||
|
||||
class RESTResponse(io.IOBase):
|
||||
|
||||
def __init__(self, resp) -> None:
|
||||
self.urllib3_response = resp
|
||||
self.response = resp
|
||||
self.status = resp.status
|
||||
self.reason = resp.reason
|
||||
self.data = resp.data
|
||||
self.data = None
|
||||
|
||||
def read(self):
|
||||
if self.data is None:
|
||||
self.data = self.response.data
|
||||
return self.data
|
||||
|
||||
def getheaders(self):
|
||||
"""Returns a dictionary of the response headers."""
|
||||
return self.urllib3_response.headers
|
||||
return self.response.headers
|
||||
|
||||
def getheader(self, name, default=None):
|
||||
"""Returns a given response header."""
|
||||
return self.urllib3_response.headers.get(name, default)
|
||||
return self.response.headers.get(name, default)
|
||||
|
||||
|
||||
class RESTClientObject:
|
||||
|
||||
def __init__(self, configuration, pools_size=4, maxsize=None) -> None:
|
||||
def __init__(self, configuration) -> None:
|
||||
# urllib3.PoolManager will pass all kw parameters to connectionpool
|
||||
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501
|
||||
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501
|
||||
# maxsize is the number of requests to host that are allowed in parallel # noqa: E501
|
||||
# Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501
|
||||
|
||||
# cert_reqs
|
||||
@@ -62,7 +62,9 @@ class RESTClientObject:
|
||||
|
||||
addition_pool_args = {}
|
||||
if configuration.assert_hostname is not None:
|
||||
addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501
|
||||
addition_pool_args['assert_hostname'] = (
|
||||
configuration.assert_hostname
|
||||
)
|
||||
|
||||
if configuration.retries is not None:
|
||||
addition_pool_args['retries'] = configuration.retries
|
||||
@@ -74,17 +76,9 @@ class RESTClientObject:
|
||||
if configuration.socket_options is not None:
|
||||
addition_pool_args['socket_options'] = configuration.socket_options
|
||||
|
||||
if maxsize is None:
|
||||
if configuration.connection_pool_maxsize is not None:
|
||||
maxsize = configuration.connection_pool_maxsize
|
||||
else:
|
||||
maxsize = 4
|
||||
|
||||
# https pool manager
|
||||
if configuration.proxy:
|
||||
self.pool_manager = urllib3.ProxyManager(
|
||||
num_pools=pools_size,
|
||||
maxsize=maxsize,
|
||||
cert_reqs=cert_reqs,
|
||||
ca_certs=configuration.ssl_ca_cert,
|
||||
cert_file=configuration.cert_file,
|
||||
@@ -95,8 +89,6 @@ class RESTClientObject:
|
||||
)
|
||||
else:
|
||||
self.pool_manager = urllib3.PoolManager(
|
||||
num_pools=pools_size,
|
||||
maxsize=maxsize,
|
||||
cert_reqs=cert_reqs,
|
||||
ca_certs=configuration.ssl_ca_cert,
|
||||
cert_file=configuration.cert_file,
|
||||
@@ -104,30 +96,39 @@ class RESTClientObject:
|
||||
**addition_pool_args
|
||||
)
|
||||
|
||||
def request(self, method, url, query_params=None, headers=None,
|
||||
body=None, post_params=None, _preload_content=True,
|
||||
_request_timeout=None):
|
||||
def request(
|
||||
self,
|
||||
method,
|
||||
url,
|
||||
headers=None,
|
||||
body=None,
|
||||
post_params=None,
|
||||
_request_timeout=None
|
||||
):
|
||||
"""Perform requests.
|
||||
|
||||
:param method: http request method
|
||||
:param url: http request url
|
||||
:param query_params: query parameters in the url
|
||||
:param headers: http request headers
|
||||
:param body: request json body, for `application/json`
|
||||
:param post_params: request post parameters,
|
||||
`application/x-www-form-urlencoded`
|
||||
and `multipart/form-data`
|
||||
:param _preload_content: if False, the urllib3.HTTPResponse object will
|
||||
be returned without reading/decoding response
|
||||
data. Default is True.
|
||||
:param _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.
|
||||
"""
|
||||
method = method.upper()
|
||||
assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT',
|
||||
'PATCH', 'OPTIONS']
|
||||
assert method in [
|
||||
'GET',
|
||||
'HEAD',
|
||||
'DELETE',
|
||||
'POST',
|
||||
'PUT',
|
||||
'PATCH',
|
||||
'OPTIONS'
|
||||
]
|
||||
|
||||
if post_params and body:
|
||||
raise ApiValueError(
|
||||
@@ -136,65 +137,78 @@ class RESTClientObject:
|
||||
|
||||
post_params = post_params or {}
|
||||
headers = headers or {}
|
||||
# url already contains the URL query string
|
||||
# so reset query_params to empty dict
|
||||
query_params = {}
|
||||
|
||||
timeout = None
|
||||
if _request_timeout:
|
||||
if isinstance(_request_timeout, (int,float)): # noqa: E501,F821
|
||||
if isinstance(_request_timeout, (int, float)):
|
||||
timeout = urllib3.Timeout(total=_request_timeout)
|
||||
elif (isinstance(_request_timeout, tuple) and
|
||||
len(_request_timeout) == 2):
|
||||
elif (
|
||||
isinstance(_request_timeout, tuple)
|
||||
and len(_request_timeout) == 2
|
||||
):
|
||||
timeout = urllib3.Timeout(
|
||||
connect=_request_timeout[0], read=_request_timeout[1])
|
||||
connect=_request_timeout[0],
|
||||
read=_request_timeout[1]
|
||||
)
|
||||
|
||||
try:
|
||||
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
|
||||
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
|
||||
|
||||
# no content type provided or payload is json
|
||||
if not headers.get('Content-Type') or re.search('json', headers['Content-Type'], re.IGNORECASE):
|
||||
content_type = headers.get('Content-Type')
|
||||
if (
|
||||
not content_type
|
||||
or re.search('json', content_type, re.IGNORECASE)
|
||||
):
|
||||
request_body = None
|
||||
if body is not None:
|
||||
request_body = json.dumps(body)
|
||||
r = self.pool_manager.request(
|
||||
method, url,
|
||||
method,
|
||||
url,
|
||||
body=request_body,
|
||||
preload_content=_preload_content,
|
||||
timeout=timeout,
|
||||
headers=headers)
|
||||
elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501
|
||||
headers=headers,
|
||||
preload_content=False
|
||||
)
|
||||
elif content_type == 'application/x-www-form-urlencoded':
|
||||
r = self.pool_manager.request(
|
||||
method, url,
|
||||
method,
|
||||
url,
|
||||
fields=post_params,
|
||||
encode_multipart=False,
|
||||
preload_content=_preload_content,
|
||||
timeout=timeout,
|
||||
headers=headers)
|
||||
elif headers['Content-Type'] == 'multipart/form-data':
|
||||
headers=headers,
|
||||
preload_content=False
|
||||
)
|
||||
elif content_type == 'multipart/form-data':
|
||||
# must del headers['Content-Type'], or the correct
|
||||
# Content-Type which generated by urllib3 will be
|
||||
# overwritten.
|
||||
del headers['Content-Type']
|
||||
r = self.pool_manager.request(
|
||||
method, url,
|
||||
method,
|
||||
url,
|
||||
fields=post_params,
|
||||
encode_multipart=True,
|
||||
preload_content=_preload_content,
|
||||
timeout=timeout,
|
||||
headers=headers)
|
||||
headers=headers,
|
||||
preload_content=False
|
||||
)
|
||||
# Pass a `string` parameter directly in the body to support
|
||||
# other content types than Json when `body` argument is
|
||||
# provided in serialized form
|
||||
elif isinstance(body, str) or isinstance(body, bytes):
|
||||
request_body = body
|
||||
r = self.pool_manager.request(
|
||||
method, url,
|
||||
method,
|
||||
url,
|
||||
body=request_body,
|
||||
preload_content=_preload_content,
|
||||
timeout=timeout,
|
||||
headers=headers)
|
||||
headers=headers,
|
||||
preload_content=False
|
||||
)
|
||||
else:
|
||||
# Cannot generate the request from given parameters
|
||||
msg = """Cannot prepare a request message for provided
|
||||
@@ -203,102 +217,16 @@ class RESTClientObject:
|
||||
raise ApiException(status=0, reason=msg)
|
||||
# For `GET`, `HEAD`
|
||||
else:
|
||||
r = self.pool_manager.request(method, url,
|
||||
fields={},
|
||||
preload_content=_preload_content,
|
||||
timeout=timeout,
|
||||
headers=headers)
|
||||
r = self.pool_manager.request(
|
||||
method,
|
||||
url,
|
||||
fields={},
|
||||
timeout=timeout,
|
||||
headers=headers,
|
||||
preload_content=False
|
||||
)
|
||||
except urllib3.exceptions.SSLError as e:
|
||||
msg = "{0}\n{1}".format(type(e).__name__, str(e))
|
||||
msg = "\n".join([type(e).__name__, str(e)])
|
||||
raise ApiException(status=0, reason=msg)
|
||||
|
||||
if _preload_content:
|
||||
r = RESTResponse(r)
|
||||
|
||||
# log response body
|
||||
logger.debug("response body: %s", r.data)
|
||||
|
||||
if not 200 <= r.status <= 299:
|
||||
if r.status == 400:
|
||||
raise BadRequestException(http_resp=r)
|
||||
|
||||
if r.status == 401:
|
||||
raise UnauthorizedException(http_resp=r)
|
||||
|
||||
if r.status == 403:
|
||||
raise ForbiddenException(http_resp=r)
|
||||
|
||||
if r.status == 404:
|
||||
raise NotFoundException(http_resp=r)
|
||||
|
||||
if 500 <= r.status <= 599:
|
||||
raise ServiceException(http_resp=r)
|
||||
|
||||
raise ApiException(http_resp=r)
|
||||
|
||||
return r
|
||||
|
||||
def get_request(self, url, headers=None, query_params=None, _preload_content=True,
|
||||
_request_timeout=None):
|
||||
return self.request("GET", url,
|
||||
headers=headers,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
query_params=query_params)
|
||||
|
||||
def head_request(self, url, headers=None, query_params=None, _preload_content=True,
|
||||
_request_timeout=None):
|
||||
return self.request("HEAD", url,
|
||||
headers=headers,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
query_params=query_params)
|
||||
|
||||
def options_request(self, url, headers=None, query_params=None, post_params=None,
|
||||
body=None, _preload_content=True, _request_timeout=None):
|
||||
return self.request("OPTIONS", url,
|
||||
headers=headers,
|
||||
query_params=query_params,
|
||||
post_params=post_params,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
body=body)
|
||||
|
||||
def delete_request(self, url, headers=None, query_params=None, body=None,
|
||||
_preload_content=True, _request_timeout=None):
|
||||
return self.request("DELETE", url,
|
||||
headers=headers,
|
||||
query_params=query_params,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
body=body)
|
||||
|
||||
def post_request(self, url, headers=None, query_params=None, post_params=None,
|
||||
body=None, _preload_content=True, _request_timeout=None):
|
||||
return self.request("POST", url,
|
||||
headers=headers,
|
||||
query_params=query_params,
|
||||
post_params=post_params,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
body=body)
|
||||
|
||||
def put_request(self, url, headers=None, query_params=None, post_params=None,
|
||||
body=None, _preload_content=True, _request_timeout=None):
|
||||
return self.request("PUT", url,
|
||||
headers=headers,
|
||||
query_params=query_params,
|
||||
post_params=post_params,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
body=body)
|
||||
|
||||
def patch_request(self, url, headers=None, query_params=None, post_params=None,
|
||||
body=None, _preload_content=True, _request_timeout=None):
|
||||
return self.request("PATCH", url,
|
||||
headers=headers,
|
||||
query_params=query_params,
|
||||
post_params=post_params,
|
||||
_preload_content=_preload_content,
|
||||
_request_timeout=_request_timeout,
|
||||
body=body)
|
||||
return RESTResponse(r)
|
||||
|
||||
Reference in New Issue
Block a user