2023-02-12 21:11:29 +08:00
# coding: utf-8
"""
Echo Server API
2023-08-08 21:42:12 +09:00
Echo Server API
2023-02-12 21:11:29 +08:00
The version of the OpenAPI document: 0.1.0
Contact: team@openapitools.org
2023-03-06 21:22:43 +08:00
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
2023-08-08 21:42:12 +09:00
""" # noqa: E501
2023-02-12 21:11:29 +08:00
import io
import json
import re
import ssl
import urllib3
2023-10-30 13:37:23 +09:00
from openapi_client . exceptions import ApiException , ApiValueError
2023-02-12 21:11:29 +08:00
2023-11-13 20:28:05 -08:00
SUPPORTED_SOCKS_PROXIES = { " socks5 " , " socks5h " , " socks4 " , " socks4a " }
2023-10-30 13:37:23 +09:00
RESTResponseType = urllib3 . HTTPResponse
2023-02-12 21:11:29 +08:00
2023-11-13 20:28:05 -08:00
def is_socks_proxy_url ( url ) :
if url is None :
return False
split_section = url . split ( " :// " )
if len ( split_section ) < 2 :
return False
else :
return split_section [ 0 ] . lower ( ) in SUPPORTED_SOCKS_PROXIES
2023-02-12 21:11:29 +08:00
class RESTResponse ( io . IOBase ) :
python: several typing and style improvements (#16378)
* python: several typing and style improvements
The generated (python) code fails with several standard validation tools,
including `flake8`, `mypy`, and `autoflake`. While fixing every possible
violation -- especially wrto typing -- woudl be a project, some of the
changes are fairly easy.
- The `autoflake` tool picks up on unused imports. These can just be removed.
- The `mypy` tool picks up on numerious typing violations, especially if set
to its strictest mode. As a starting point, all functions ought to annotate
a return type, including constructors, even if the return type is `None`
because otherwise the functions are omitted from type checking and it's
impossible to make incremental progress towards adding types.
- The `flake8` tool mostly finds whitespace and line-length issues; while
line-length standards very, the source already includes several flake8
ignores, so it seems safe to add a few more.
* Add generated files
* Restore imports used by `AbstractPythonCodegen.java`
* Update generated files
2023-09-01 10:03:02 -07:00
def __init__ ( self , resp ) - > None :
2023-10-30 13:37:23 +09:00
self . response = resp
2023-02-12 21:11:29 +08:00
self . status = resp . status
self . reason = resp . reason
2023-10-30 13:37:23 +09:00
self . data = None
def read ( self ) :
if self . data is None :
self . data = self . response . data
return self . data
2023-02-12 21:11:29 +08:00
2025-12-09 02:29:32 -05:00
@property
def headers ( self ) :
""" Returns a dictionary of response headers. """
return self . response . headers
2023-02-12 21:11:29 +08:00
def getheaders ( self ) :
2025-12-09 02:29:32 -05:00
""" Returns a dictionary of the response headers; use ``headers`` instead. """
2023-10-30 13:37:23 +09:00
return self . response . headers
2023-02-12 21:11:29 +08:00
def getheader ( self , name , default = None ) :
2025-12-09 02:29:32 -05:00
""" Returns a given response header; use ``headers.get()`` instead. """
2023-10-30 13:37:23 +09:00
return self . response . headers . get ( name , default )
2023-02-12 21:11:29 +08:00
python: several typing and style improvements (#16378)
* python: several typing and style improvements
The generated (python) code fails with several standard validation tools,
including `flake8`, `mypy`, and `autoflake`. While fixing every possible
violation -- especially wrto typing -- woudl be a project, some of the
changes are fairly easy.
- The `autoflake` tool picks up on unused imports. These can just be removed.
- The `mypy` tool picks up on numerious typing violations, especially if set
to its strictest mode. As a starting point, all functions ought to annotate
a return type, including constructors, even if the return type is `None`
because otherwise the functions are omitted from type checking and it's
impossible to make incremental progress towards adding types.
- The `flake8` tool mostly finds whitespace and line-length issues; while
line-length standards very, the source already includes several flake8
ignores, so it seems safe to add a few more.
* Add generated files
* Restore imports used by `AbstractPythonCodegen.java`
* Update generated files
2023-09-01 10:03:02 -07:00
class RESTClientObject :
2023-02-12 21:11:29 +08:00
2023-10-30 13:37:23 +09:00
def __init__ ( self , configuration ) - > None :
2023-02-12 21:11:29 +08:00
# 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
# Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501
# cert_reqs
if configuration . verify_ssl :
cert_reqs = ssl . CERT_REQUIRED
else :
cert_reqs = ssl . CERT_NONE
2024-01-06 08:40:42 +01:00
pool_args = {
" cert_reqs " : cert_reqs ,
" ca_certs " : configuration . ssl_ca_cert ,
" cert_file " : configuration . cert_file ,
" key_file " : configuration . key_file ,
2025-02-26 18:04:32 +10:00
" ca_cert_data " : configuration . ca_cert_data ,
2024-01-06 08:40:42 +01:00
}
2023-02-12 21:11:29 +08:00
if configuration . assert_hostname is not None :
2024-01-06 08:40:42 +01:00
pool_args [ ' assert_hostname ' ] = (
2023-10-30 13:37:23 +09:00
configuration . assert_hostname
)
2023-02-12 21:11:29 +08:00
if configuration . retries is not None :
2024-01-06 08:40:42 +01:00
pool_args [ ' retries ' ] = configuration . retries
2023-02-12 21:11:29 +08:00
2023-05-05 09:11:57 +01:00
if configuration . tls_server_name :
2024-01-06 08:40:42 +01:00
pool_args [ ' server_hostname ' ] = configuration . tls_server_name
2023-05-05 09:11:57 +01:00
2023-02-12 21:11:29 +08:00
if configuration . socket_options is not None :
2024-01-06 08:40:42 +01:00
pool_args [ ' socket_options ' ] = configuration . socket_options
2023-02-12 21:11:29 +08:00
2023-12-06 08:49:10 +01:00
if configuration . connection_pool_maxsize is not None :
2024-01-06 08:40:42 +01:00
pool_args [ ' maxsize ' ] = configuration . connection_pool_maxsize
2023-12-06 08:49:10 +01:00
2023-02-12 21:11:29 +08:00
# https pool manager
2024-01-06 08:40:42 +01:00
self . pool_manager : urllib3 . PoolManager
2023-02-12 21:11:29 +08:00
if configuration . proxy :
2023-11-13 20:28:05 -08:00
if is_socks_proxy_url ( configuration . proxy ) :
from urllib3 . contrib . socks import SOCKSProxyManager
2024-01-06 08:40:42 +01:00
pool_args [ " proxy_url " ] = configuration . proxy
pool_args [ " headers " ] = configuration . proxy_headers
self . pool_manager = SOCKSProxyManager ( * * pool_args )
2023-11-13 20:28:05 -08:00
else :
2024-01-06 08:40:42 +01:00
pool_args [ " proxy_url " ] = configuration . proxy
pool_args [ " proxy_headers " ] = configuration . proxy_headers
self . pool_manager = urllib3 . ProxyManager ( * * pool_args )
2023-02-12 21:11:29 +08:00
else :
2024-01-06 08:40:42 +01:00
self . pool_manager = urllib3 . PoolManager ( * * pool_args )
2023-02-12 21:11:29 +08:00
2023-10-30 13:37:23 +09:00
def request (
self ,
method ,
url ,
headers = None ,
body = None ,
post_params = None ,
_request_timeout = None
) :
2023-02-12 21:11:29 +08:00
""" Perform requests.
:param method: http request method
:param url: http request 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 _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 ( )
2023-10-30 13:37:23 +09:00
assert method in [
' GET ' ,
' HEAD ' ,
' DELETE ' ,
' POST ' ,
' PUT ' ,
' PATCH ' ,
' OPTIONS '
]
2023-02-12 21:11:29 +08:00
if post_params and body :
raise ApiValueError (
" body parameter cannot be used with post_params parameter. "
)
post_params = post_params or { }
headers = headers or { }
timeout = None
if _request_timeout :
2023-10-30 13:37:23 +09:00
if isinstance ( _request_timeout , ( int , float ) ) :
2023-02-12 21:11:29 +08:00
timeout = urllib3 . Timeout ( total = _request_timeout )
2023-10-30 13:37:23 +09:00
elif (
isinstance ( _request_timeout , tuple )
and len ( _request_timeout ) == 2
) :
2023-02-12 21:11:29 +08:00
timeout = urllib3 . Timeout (
2023-10-30 13:37:23 +09:00
connect = _request_timeout [ 0 ] ,
read = _request_timeout [ 1 ]
)
2023-02-12 21:11:29 +08:00
try :
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in [ ' POST ' , ' PUT ' , ' PATCH ' , ' OPTIONS ' , ' DELETE ' ] :
# no content type provided or payload is json
2023-10-30 13:37:23 +09:00
content_type = headers . get ( ' Content-Type ' )
if (
not content_type
or re . search ( ' json ' , content_type , re . IGNORECASE )
) :
2023-02-12 21:11:29 +08:00
request_body = None
if body is not None :
request_body = json . dumps ( body )
r = self . pool_manager . request (
2023-10-30 13:37:23 +09:00
method ,
url ,
2023-02-12 21:11:29 +08:00
body = request_body ,
timeout = timeout ,
2023-10-30 13:37:23 +09:00
headers = headers ,
preload_content = False
)
elif content_type == ' application/x-www-form-urlencoded ' :
2023-02-12 21:11:29 +08:00
r = self . pool_manager . request (
2023-10-30 13:37:23 +09:00
method ,
url ,
2023-02-12 21:11:29 +08:00
fields = post_params ,
encode_multipart = False ,
timeout = timeout ,
2023-10-30 13:37:23 +09:00
headers = headers ,
preload_content = False
)
elif content_type == ' multipart/form-data ' :
2023-02-12 21:11:29 +08:00
# must del headers['Content-Type'], or the correct
# Content-Type which generated by urllib3 will be
# overwritten.
del headers [ ' Content-Type ' ]
2024-03-19 11:31:08 +01:00
# Ensures that dict objects are serialized
post_params = [ ( a , json . dumps ( b ) ) if isinstance ( b , dict ) else ( a , b ) for a , b in post_params ]
2023-02-12 21:11:29 +08:00
r = self . pool_manager . request (
2023-10-30 13:37:23 +09:00
method ,
url ,
2023-02-12 21:11:29 +08:00
fields = post_params ,
encode_multipart = True ,
timeout = timeout ,
2023-10-30 13:37:23 +09:00
headers = headers ,
preload_content = False
)
2023-02-12 21:11:29 +08:00
# Pass a `string` parameter directly in the body to support
2024-01-09 09:45:05 +01:00
# other content types than JSON when `body` argument is
# provided in serialized form.
2023-02-12 21:11:29 +08:00
elif isinstance ( body , str ) or isinstance ( body , bytes ) :
r = self . pool_manager . request (
2023-10-30 13:37:23 +09:00
method ,
url ,
2024-01-09 09:45:05 +01:00
body = body ,
2023-02-12 21:11:29 +08:00
timeout = timeout ,
2023-10-30 13:37:23 +09:00
headers = headers ,
preload_content = False
)
2024-10-08 03:34:36 +02:00
elif headers [ ' Content-Type ' ] . startswith ( ' text/ ' ) and isinstance ( body , bool ) :
2023-11-14 04:30:10 +01:00
request_body = " true " if body else " false "
r = self . pool_manager . request (
method ,
url ,
body = request_body ,
preload_content = False ,
timeout = timeout ,
headers = headers )
2023-02-12 21:11:29 +08:00
else :
# Cannot generate the request from given parameters
msg = """ Cannot prepare a request message for provided
arguments. Please check that your arguments match
declared content type. """
raise ApiException ( status = 0 , reason = msg )
# For `GET`, `HEAD`
else :
2023-10-30 13:37:23 +09:00
r = self . pool_manager . request (
method ,
url ,
fields = { } ,
timeout = timeout ,
headers = headers ,
preload_content = False
)
2023-02-12 21:11:29 +08:00
except urllib3 . exceptions . SSLError as e :
2023-10-30 13:37:23 +09:00
msg = " \n " . join ( [ type ( e ) . __name__ , str ( e ) ] )
2023-02-12 21:11:29 +08:00
raise ApiException ( status = 0 , reason = msg )
2023-10-30 13:37:23 +09:00
return RESTResponse ( r )