forked from loafle/openapi-generator-original
fixed error handling in async requests in python client (#5308)
This commit is contained in:
@@ -37,20 +37,16 @@ class {{classname}}(object):
|
||||
{{{notes}}}
|
||||
{{/notes}}
|
||||
This method makes a synchronous HTTP request by default. To make an
|
||||
asynchronous HTTP request, please define a `callback` function
|
||||
to be invoked when receiving the response.
|
||||
>>> def callback_function(response):
|
||||
>>> pprint(response)
|
||||
>>>
|
||||
asynchronous HTTP request, please pass async=True
|
||||
{{#sortParamsByRequiredFlag}}
|
||||
>>> thread = api.{{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}callback=callback_function)
|
||||
>>> thread = api.{{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}async=True)
|
||||
{{/sortParamsByRequiredFlag}}
|
||||
{{^sortParamsByRequiredFlag}}
|
||||
>>> thread = api.{{operationId}}({{#allParams}}{{#required}}{{paramName}}={{paramName}}_value, {{/required}}{{/allParams}}callback=callback_function)
|
||||
>>> thread = api.{{operationId}}({{#allParams}}{{#required}}{{paramName}}={{paramName}}_value, {{/required}}{{/allParams}}async=True)
|
||||
{{/sortParamsByRequiredFlag}}
|
||||
>>> result = thread.get()
|
||||
|
||||
:param callback function: The callback function
|
||||
for asynchronous request. (optional)
|
||||
:param async bool
|
||||
{{#allParams}}
|
||||
:param {{dataType}} {{paramName}}:{{#description}} {{{description}}}{{/description}}{{#required}} (required){{/required}}{{#optional}}(optional){{/optional}}
|
||||
{{/allParams}}
|
||||
@@ -59,7 +55,7 @@ class {{classname}}(object):
|
||||
returns the request thread.
|
||||
"""
|
||||
kwargs['_return_http_data_only'] = True
|
||||
if kwargs.get('callback'):
|
||||
if kwargs.get('async'):
|
||||
return self.{{operationId}}_with_http_info({{#sortParamsByRequiredFlag}}{{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{/sortParamsByRequiredFlag}}**kwargs)
|
||||
else:
|
||||
(data) = self.{{operationId}}_with_http_info({{#sortParamsByRequiredFlag}}{{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{/sortParamsByRequiredFlag}}**kwargs)
|
||||
@@ -74,20 +70,16 @@ class {{classname}}(object):
|
||||
{{{notes}}}
|
||||
{{/notes}}
|
||||
This method makes a synchronous HTTP request by default. To make an
|
||||
asynchronous HTTP request, please define a `callback` function
|
||||
to be invoked when receiving the response.
|
||||
>>> def callback_function(response):
|
||||
>>> pprint(response)
|
||||
>>>
|
||||
asynchronous HTTP request, please pass async=True
|
||||
{{#sortParamsByRequiredFlag}}
|
||||
>>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}callback=callback_function)
|
||||
>>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}async=True)
|
||||
{{/sortParamsByRequiredFlag}}
|
||||
{{^sortParamsByRequiredFlag}}
|
||||
>>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}={{paramName}}_value, {{/required}}{{/allParams}}callback=callback_function)
|
||||
>>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}={{paramName}}_value, {{/required}}{{/allParams}}async=True)
|
||||
{{/sortParamsByRequiredFlag}}
|
||||
>>> result = thread.get()
|
||||
|
||||
:param callback function: The callback function
|
||||
for asynchronous request. (optional)
|
||||
:param async bool
|
||||
{{#allParams}}
|
||||
:param {{dataType}} {{paramName}}:{{#description}} {{{description}}}{{/description}}{{#required}} (required){{/required}}{{#optional}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/optional}}
|
||||
{{/allParams}}
|
||||
@@ -97,7 +89,7 @@ class {{classname}}(object):
|
||||
"""
|
||||
|
||||
all_params = [{{#allParams}}'{{paramName}}'{{#hasMore}}, {{/hasMore}}{{/allParams}}]
|
||||
all_params.append('callback')
|
||||
all_params.append('async')
|
||||
all_params.append('_return_http_data_only')
|
||||
all_params.append('_preload_content')
|
||||
all_params.append('_request_timeout')
|
||||
@@ -214,7 +206,7 @@ class {{classname}}(object):
|
||||
files=local_var_files,
|
||||
response_type={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}},
|
||||
auth_settings=auth_settings,
|
||||
callback=params.get('callback'),
|
||||
async=params.get('async'),
|
||||
_return_http_data_only=params.get('_return_http_data_only'),
|
||||
_preload_content=params.get('_preload_content', True),
|
||||
_request_timeout=params.get('_request_timeout'),
|
||||
|
||||
@@ -7,7 +7,7 @@ import re
|
||||
import json
|
||||
import mimetypes
|
||||
import tempfile
|
||||
import threading
|
||||
from multiprocessing.pool import ThreadPool
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
@@ -55,6 +55,7 @@ class ApiClient(object):
|
||||
configuration = Configuration()
|
||||
self.configuration = configuration
|
||||
|
||||
self.pool = ThreadPool()
|
||||
self.rest_client = RESTClientObject(configuration)
|
||||
self.default_headers = {}
|
||||
if header_name is not None:
|
||||
@@ -83,7 +84,7 @@ class ApiClient(object):
|
||||
def __call_api(self, resource_path, method,
|
||||
path_params=None, query_params=None, header_params=None,
|
||||
body=None, post_params=None, files=None,
|
||||
response_type=None, auth_settings=None, callback=None,
|
||||
response_type=None, auth_settings=None,
|
||||
_return_http_data_only=None, collection_formats=None, _preload_content=True,
|
||||
_request_timeout=None):
|
||||
|
||||
@@ -147,12 +148,7 @@ class ApiClient(object):
|
||||
else:
|
||||
return_data = None
|
||||
|
||||
if callback:
|
||||
if _return_http_data_only:
|
||||
callback(return_data)
|
||||
else:
|
||||
callback((return_data, response_data.status, response_data.getheaders()))
|
||||
elif _return_http_data_only:
|
||||
if _return_http_data_only:
|
||||
return (return_data)
|
||||
else:
|
||||
return (return_data, response_data.status, response_data.getheaders())
|
||||
@@ -266,7 +262,7 @@ class ApiClient(object):
|
||||
def call_api(self, resource_path, method,
|
||||
path_params=None, query_params=None, header_params=None,
|
||||
body=None, post_params=None, files=None,
|
||||
response_type=None, auth_settings=None, callback=None,
|
||||
response_type=None, auth_settings=None, async=None,
|
||||
_return_http_data_only=None, collection_formats=None, _preload_content=True,
|
||||
_request_timeout=None):
|
||||
"""
|
||||
@@ -286,9 +282,7 @@ class ApiClient(object):
|
||||
:param response: Response data type.
|
||||
:param files dict: key -> filename, value -> filepath,
|
||||
for `multipart/form-data`.
|
||||
:param callback function: Callback function for asynchronous request.
|
||||
If provide this parameter,
|
||||
the request will be called asynchronously.
|
||||
:param async bool: execute request asynchronously
|
||||
:param _return_http_data_only: response data without head status code and headers
|
||||
:param collection_formats: dict of collection formats for path, query,
|
||||
header, and post parameters.
|
||||
@@ -303,22 +297,20 @@ class ApiClient(object):
|
||||
If parameter callback is None,
|
||||
then the method will return the response directly.
|
||||
"""
|
||||
if callback is None:
|
||||
if not async:
|
||||
return self.__call_api(resource_path, method,
|
||||
path_params, query_params, header_params,
|
||||
body, post_params, files,
|
||||
response_type, auth_settings, callback,
|
||||
response_type, auth_settings,
|
||||
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
|
||||
else:
|
||||
thread = threading.Thread(target=self.__call_api,
|
||||
args=(resource_path, method,
|
||||
path_params, query_params,
|
||||
header_params, body,
|
||||
post_params, files,
|
||||
response_type, auth_settings,
|
||||
callback, _return_http_data_only,
|
||||
collection_formats, _preload_content, _request_timeout))
|
||||
thread.start()
|
||||
thread = self.pool.apply_async(self.__call_api, (resource_path, method,
|
||||
path_params, query_params,
|
||||
header_params, body,
|
||||
post_params, files,
|
||||
response_type, auth_settings,
|
||||
_return_http_data_only,
|
||||
collection_formats, _preload_content, _request_timeout))
|
||||
return thread
|
||||
|
||||
def request(self, method, url, query_params=None, headers=None,
|
||||
|
||||
Reference in New Issue
Block a user