Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions modules/openapi-generator/src/main/resources/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,34 @@ class {{classname}}:
self.api_client = api_client
{{#operation}}

@validate_arguments
{{#asyncio}}
@overload
async def {{operationId}}(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}**kwargs) -> {{{returnType}}}{{^returnType}}None{{/returnType}}: # noqa: E501
...

@overload
def {{operationId}}(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}async_req: Optional[bool]=True, **kwargs) -> {{{returnType}}}{{^returnType}}None{{/returnType}}: # noqa: E501
...

{{/asyncio}}
@validate_arguments
def {{operationId}}(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}{{#asyncio}}async_req: Optional[bool]=None, {{/asyncio}}**kwargs) -> {{#asyncio}}Union[{{{returnType}}}{{^returnType}}None{{/returnType}}, Awaitable[{{{returnType}}}{{^returnType}}None{{/returnType}}]]{{/asyncio}}{{^asyncio}}{{{returnType}}}{{^returnType}}None{{/returnType}}{{/asyncio}}: # noqa: E501
{{^asyncio}}
def {{operationId}}(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}**kwargs) -> {{{returnType}}}{{^returnType}}None{{/returnType}}: # noqa: E501
{{/asyncio}}
"""{{#isDeprecated}}(Deprecated) {{/isDeprecated}}{{{summary}}}{{^summary}}{{operationId}}{{/summary}} # noqa: E501

{{#notes}}
{{{.}}} # noqa: E501
{{/notes}}
{{^asyncio}}
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True

>>> thread = api.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}async_req=True)
>>> result = thread.get()
{{/asyncio}}

{{#allParams}}
:param {{paramName}}:{{#description}} {{{.}}}{{/description}}{{#required}} (required){{/required}}{{#optional}}(optional){{/optional}}
:type {{paramName}}: {{dataType}}{{#optional}}, optional{{/optional}}
{{/allParams}}
{{^asyncio}}
:param async_req: Whether to execute the request asynchronously.
:type async_req: bool, optional
{{/asyncio}}
: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
Expand All @@ -79,31 +78,36 @@ class {{classname}}:
if '_preload_content' in kwargs:
message = "Error! Please call the {{operationId}}_with_http_info method with `_preload_content` instead and obtain raw data from ApiResponse.raw_data" # noqa: E501
raise ValueError(message)
{{#asyncio}}
if async_req is not None:
kwargs['async_req'] = async_req
{{/asyncio}}
return self.{{operationId}}_with_http_info({{#allParams}}{{paramName}}, {{/allParams}}**kwargs) # noqa: E501
return {{#asyncio}}await {{/asyncio}}self.{{operationId}}_with_http_info({{#allParams}}{{paramName}}, {{/allParams}}**kwargs) # noqa: E501

@validate_arguments
{{#asyncio}}
async def {{operationId}}_with_http_info(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}**kwargs) -> ApiResponse: # noqa: E501
{{/asyncio}}
{{^asyncio}}
def {{operationId}}_with_http_info(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}**kwargs) -> ApiResponse: # noqa: E501
{{/asyncio}}
"""{{#isDeprecated}}(Deprecated) {{/isDeprecated}}{{{summary}}}{{^summary}}{{operationId}}{{/summary}} # noqa: E501

{{#notes}}
{{{.}}} # noqa: E501
{{/notes}}
{{^asyncio}}
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True

>>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{paramName}}, {{/allParams}}async_req=True)
>>> result = thread.get()
{{/asyncio}}

{{#allParams}}
:param {{paramName}}:{{#description}} {{{.}}}{{/description}}{{#required}} (required){{/required}}{{#optional}}(optional){{/optional}}
:type {{paramName}}: {{dataType}}{{#optional}}, optional{{/optional}}
{{/allParams}}
{{^asyncio}}
:param async_req: Whether to execute the request asynchronously.
:type async_req: bool, optional
{{/asyncio}}
:param _preload_content: if False, the ApiResponse.data will
be set to none and raw_data will store the
HTTP response body without reading/decoding.
Expand Down Expand Up @@ -156,7 +160,9 @@ class {{classname}}:
]
_all_params.extend(
[
{{^asyncio}}
'async_req',
{{/asyncio}}
'_return_http_data_only',
'_preload_content',
'_request_timeout',
Expand Down Expand Up @@ -290,7 +296,7 @@ class {{classname}}:
_response_types_map = {}
{{/returnType}}

return self.api_client.call_api(
return {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
'{{{path}}}', '{{httpMethod}}',
_path_params,
_query_params,
Expand All @@ -300,7 +306,9 @@ class {{classname}}:
files=_files,
response_types_map=_response_types_map,
auth_settings=_auth_settings,
{{^asyncio}}
async_req=_params.get('async_req'),
{{/asyncio}}
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import datetime
from dateutil.parser import parse
import json
import mimetypes
{{^asyncio}}
from multiprocessing.pool import ThreadPool
{{/asyncio}}
import os
import re
import tempfile
Expand Down Expand Up @@ -38,8 +40,10 @@ class ApiClient:
the API.
:param cookie: a cookie to include in the header when making calls
to the API
{{^asyncio}}
:param pool_threads: The number of threads to use for async requests
to the API. More threads means more concurrent API requests.
{{/asyncio}}
"""

PRIMITIVE_TYPES = (float, bool, bytes, str, int)
Expand All @@ -56,12 +60,14 @@ class ApiClient:
_pool = None

def __init__(self, configuration=None, header_name=None, header_value=None,
cookie=None, pool_threads=1) -> None:
cookie=None{{^asyncio}}, pool_threads=1{{/asyncio}}) -> None:
# use default configuration if none is provided
if configuration is None:
configuration = Configuration.get_default()
self.configuration = configuration
{{^asyncio}}
self.pool_threads = pool_threads
{{/asyncio}}

self.rest_client = rest.RESTClientObject(configuration)
self.default_headers = {}
Expand All @@ -72,25 +78,24 @@ class ApiClient:
self.user_agent = '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/python{{/httpUserAgent}}'
self.client_side_validation = configuration.client_side_validation

{{#asyncio}}
{{#asyncio}}
async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc_value, traceback):
await self.close()
{{/asyncio}}
{{^asyncio}}

async def close(self):
await self.rest_client.close()
{{/asyncio}}
{{^asyncio}}
def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.close()
{{/asyncio}}

{{#asyncio}}async {{/asyncio}}def close(self):
{{#asyncio}}
await self.rest_client.close()
{{/asyncio}}
def close(self):
if self._pool:
self._pool.close()
self._pool.join()
Expand All @@ -107,6 +112,7 @@ class ApiClient:
atexit.register(self.close)
self._pool = ThreadPool(self.pool_threads)
return self._pool
{{/asyncio}}

@property
def user_agent(self):
Expand Down Expand Up @@ -230,7 +236,7 @@ class ApiClient:

self.last_response = response_data

return_data = None # assuming derialization is not needed
return_data = None # assuming deserialization is not needed
# data needs deserialization or returns HTTP data (deserialized) only
if _preload_content or _return_http_data_only:
response_type = response_types_map.get(str(response_data.status), None)
Expand Down Expand Up @@ -376,17 +382,19 @@ class ApiClient:
else:
return self.__deserialize_model(data, klass)

def call_api(self, resource_path, method,
{{#asyncio}}async {{/asyncio}}def call_api(self, resource_path, method,
path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None,
response_types_map=None, auth_settings=None,
async_req=None, _return_http_data_only=None,
{{^asyncio}}async_req=None, {{/asyncio}}_return_http_data_only=None,
collection_formats=None, _preload_content=True,
_request_timeout=None, _host=None, _request_auth=None):
"""Makes the HTTP request (synchronous) and returns deserialized data.

{{^asyncio}}
To make an async_req request, set the async_req parameter.

{{/asyncio}}
:param resource_path: Path to method endpoint.
:param method: Method to call.
:param path_params: Path parameters in the url.
Expand All @@ -400,7 +408,9 @@ class ApiClient:
:param response: Response data type.
:param files dict: key -> filename, value -> filepath,
for `multipart/form-data`.
{{^asyncio}}
:param async_req bool: execute request asynchronously
{{/asyncio}}
:param _return_http_data_only: response data instead of ApiResponse
object with status code, headers, etc
:param _preload_content: if False, the ApiResponse.data will
Expand All @@ -418,82 +428,93 @@ class ApiClient:
in the spec for a single request.
:type _request_token: dict, optional
:return:
{{#asyncio}}
The response.
{{/asyncio}}
{{^asyncio}}
If async_req parameter is True,
the request will be called asynchronously.
The method will return the request thread.
If parameter async_req is False or missing,
then the method will return the response directly.
{{/asyncio}}
"""
args = (
resource_path,
method,
path_params,
query_params,
header_params,
body,
post_params,
files,
response_types_map,
auth_settings,
_return_http_data_only,
collection_formats,
_preload_content,
_request_timeout,
_host,
_request_auth,
)
{{#asyncio}}
return await self.__call_api(*args)
{{/asyncio}}
{{^asyncio}}
if not async_req:
return self.__call_api(resource_path, method,
path_params, query_params, header_params,
body, post_params, files,
response_types_map, auth_settings,
_return_http_data_only, collection_formats,
_preload_content, _request_timeout, _host,
_request_auth)

return self.pool.apply_async(self.__call_api, (resource_path,
method, path_params,
query_params,
header_params, body,
post_params, files,
response_types_map,
auth_settings,
_return_http_data_only,
collection_formats,
_preload_content,
_request_timeout,
_host, _request_auth))

def request(self, method, url, query_params=None, headers=None,
return self.__call_api(*args)

return self.pool.apply_async(self.__call_api, args)
{{/asyncio}}

{{#asyncio}}async {{/asyncio}}def request(self, method, url, query_params=None, headers=None,
post_params=None, body=None, _preload_content=True,
_request_timeout=None):
"""Makes the HTTP request using RESTClient."""
if method == "GET":
return self.rest_client.get_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.get_request(url,
query_params=query_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
headers=headers)
elif method == "HEAD":
return self.rest_client.head_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.head_request(url,
query_params=query_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
headers=headers)
elif method == "OPTIONS":
return self.rest_client.options_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.options_request(url,
query_params=query_params,
headers=headers,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
elif method == "POST":
return self.rest_client.post_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.post_request(url,
query_params=query_params,
headers=headers,
post_params=post_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
elif method == "PUT":
return self.rest_client.put_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.put_request(url,
query_params=query_params,
headers=headers,
post_params=post_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
elif method == "PATCH":
return self.rest_client.patch_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.patch_request(url,
query_params=query_params,
headers=headers,
post_params=post_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
elif method == "DELETE":
return self.rest_client.delete_request(url,
return {{#asyncio}}await {{/asyncio}}self.rest_client.delete_request(url,
query_params=query_params,
headers=headers,
_preload_content=_preload_content,
Expand Down
Loading