diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index c6a07b2f7a2..e3c687f7bad 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -99,6 +99,7 @@ class {{classname}}(object): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -213,6 +214,7 @@ class {{classname}}(object): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) {{/operation}} {{/operations}} diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 11646705495..94046a878f5 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -96,7 +96,8 @@ class ApiClient(object): path_params=None, query_params=None, header_params=None, body=None, post_params=None, files=None, response_type=None, auth_settings=None, callback=None, - _return_http_data_only=None, collection_formats=None, _preload_content=True): + _return_http_data_only=None, collection_formats=None, _preload_content=True, + _request_timeout=None): # header parameters header_params = header_params or {} @@ -144,7 +145,9 @@ class ApiClient(object): response_data = self.request(method, url, query_params=query_params, headers=header_params, - post_params=post_params, body=body, _preload_content=_preload_content) + post_params=post_params, body=body, + _preload_content=_preload_content, + _request_timeout=_request_timeout) self.last_response = response_data @@ -279,7 +282,8 @@ class ApiClient(object): path_params=None, query_params=None, header_params=None, body=None, post_params=None, files=None, response_type=None, auth_settings=None, callback=None, - _return_http_data_only=None, collection_formats=None, _preload_content=True): + _return_http_data_only=None, collection_formats=None, _preload_content=True, + _request_timeout=None): """ Makes the HTTP request (synchronous) and return the deserialized data. To make an async request, define a function for callback. @@ -303,21 +307,23 @@ class ApiClient(object): :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. + :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. :return: If provide parameter callback, the request will be called asynchronously. The method will return the request thread. If parameter callback is None, then the method will return the response directly. - :param _preload_content: if False, the urllib3.HTTPResponse object will be returned without - reading/decoding response data. Default is True. """ if callback is None: return self.__call_api(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) + _return_http_data_only, collection_formats, _preload_content, _request_timeout) else: thread = threading.Thread(target=self.__call_api, args=(resource_path, method, @@ -326,12 +332,12 @@ class ApiClient(object): post_params, files, response_type, auth_settings, callback, _return_http_data_only, - collection_formats, _preload_content)) + collection_formats, _preload_content, _request_timeout)) thread.start() return thread def request(self, method, url, query_params=None, headers=None, - post_params=None, body=None, _preload_content=True): + post_params=None, body=None, _preload_content=True, _request_timeout=None): """ Makes the HTTP request using RESTClient. """ @@ -339,11 +345,13 @@ class ApiClient(object): return self.rest_client.GET(url, query_params=query_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, headers=headers) elif method == "HEAD": return self.rest_client.HEAD(url, query_params=query_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, headers=headers) elif method == "OPTIONS": return self.rest_client.OPTIONS(url, @@ -351,6 +359,7 @@ class ApiClient(object): headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "POST": return self.rest_client.POST(url, @@ -358,6 +367,7 @@ class ApiClient(object): headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "PUT": return self.rest_client.PUT(url, @@ -365,6 +375,7 @@ class ApiClient(object): headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "PATCH": return self.rest_client.PATCH(url, @@ -372,12 +383,14 @@ class ApiClient(object): headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "DELETE": return self.rest_client.DELETE(url, query_params=query_params, headers=headers, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) else: raise ValueError( diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index 3f0e3dca292..8ddd17c473c 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -85,7 +85,7 @@ class RESTClientObject(object): ) def request(self, method, url, query_params=None, headers=None, - body=None, post_params=None, _preload_content=True): + body=None, post_params=None, _preload_content=True, _request_timeout=None): """ :param method: http request method :param url: http request url @@ -97,6 +97,8 @@ class RESTClientObject(object): 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'] @@ -109,6 +111,13 @@ class RESTClientObject(object): post_params = post_params or {} headers = headers or {} + timeout = None + if _request_timeout: + if isinstance(_request_timeout, (int, ) if PY3 else (int, long)): + timeout = urllib3.Timeout(total=_request_timeout) + elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2: + timeout = urllib3.Timeout(connect=_request_timeout[0], read=_request_timeout[1]) + if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' @@ -124,12 +133,14 @@ class RESTClientObject(object): r = self.pool_manager.request(method, url, body=request_body, preload_content=_preload_content, + timeout=timeout, headers=headers) elif headers['Content-Type'] == 'application/x-www-form-urlencoded': r = self.pool_manager.request(method, url, fields=post_params, encode_multipart=False, preload_content=_preload_content, + timeout=timeout, headers=headers) elif headers['Content-Type'] == 'multipart/form-data': # must del headers['Content-Type'], or the correct Content-Type @@ -139,6 +150,7 @@ class RESTClientObject(object): fields=post_params, encode_multipart=True, preload_content=_preload_content, + timeout=timeout, headers=headers) # Pass a `string` parameter directly in the body to support # other content types than Json when `body` argument is provided @@ -148,6 +160,7 @@ class RESTClientObject(object): r = self.pool_manager.request(method, url, body=request_body, preload_content=_preload_content, + timeout=timeout, headers=headers) else: # Cannot generate the request from given parameters @@ -159,6 +172,7 @@ class RESTClientObject(object): r = self.pool_manager.request(method, url, fields=query_params, preload_content=_preload_content, + timeout=timeout, headers=headers) except urllib3.exceptions.SSLError as e: msg = "{0}\n{1}".format(type(e).__name__, str(e)) @@ -180,55 +194,66 @@ class RESTClientObject(object): return r - def GET(self, url, headers=None, query_params=None, _preload_content=True): + def GET(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(self, url, headers=None, query_params=None, _preload_content=True): + def HEAD(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def OPTIONS(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(self, url, headers=None, query_params=None, body=None, _preload_content=True): + def DELETE(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def POST(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def PUT(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def PATCH(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) diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index f13cb3772ad..16841117b1b 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -96,7 +96,8 @@ 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, - _return_http_data_only=None, collection_formats=None, _preload_content=True): + _return_http_data_only=None, collection_formats=None, _preload_content=True, + _request_timeout=None): # header parameters header_params = header_params or {} @@ -144,7 +145,9 @@ def __call_api(self, resource_path, method, response_data = self.request(method, url, query_params=query_params, headers=header_params, - post_params=post_params, body=body, _preload_content=_preload_content) + post_params=post_params, body=body, + _preload_content=_preload_content, + _request_timeout=_request_timeout) self.last_response = response_data @@ -279,7 +282,8 @@ 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, - _return_http_data_only=None, collection_formats=None, _preload_content=True): + _return_http_data_only=None, collection_formats=None, _preload_content=True, + _request_timeout=None): """ Makes the HTTP request (synchronous) and return the deserialized data. To make an async request, define a function for callback. @@ -303,21 +307,23 @@ def call_api(self, resource_path, method, :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. + :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. :return: If provide parameter callback, the request will be called asynchronously. The method will return the request thread. If parameter callback is None, then the method will return the response directly. - :param _preload_content: if False, the urllib3.HTTPResponse object will be returned without - reading/decoding response data. Default is True. """ if callback is None: return self.__call_api(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) + _return_http_data_only, collection_formats, _preload_content, _request_timeout) else: thread = threading.Thread(target=self.__call_api, args=(resource_path, method, @@ -326,12 +332,12 @@ def call_api(self, resource_path, method, post_params, files, response_type, auth_settings, callback, _return_http_data_only, - collection_formats, _preload_content)) + collection_formats, _preload_content, _request_timeout)) thread.start() return thread def request(self, method, url, query_params=None, headers=None, - post_params=None, body=None, _preload_content=True): + post_params=None, body=None, _preload_content=True, _request_timeout=None): """ Makes the HTTP request using RESTClient. """ @@ -339,11 +345,13 @@ def request(self, method, url, query_params=None, headers=None, return self.rest_client.GET(url, query_params=query_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, headers=headers) elif method == "HEAD": return self.rest_client.HEAD(url, query_params=query_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, headers=headers) elif method == "OPTIONS": return self.rest_client.OPTIONS(url, @@ -351,6 +359,7 @@ def request(self, method, url, query_params=None, headers=None, headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "POST": return self.rest_client.POST(url, @@ -358,6 +367,7 @@ def request(self, method, url, query_params=None, headers=None, headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "PUT": return self.rest_client.PUT(url, @@ -365,6 +375,7 @@ def request(self, method, url, query_params=None, headers=None, headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "PATCH": return self.rest_client.PATCH(url, @@ -372,12 +383,14 @@ def request(self, method, url, query_params=None, headers=None, headers=headers, post_params=post_params, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) elif method == "DELETE": return self.rest_client.DELETE(url, query_params=query_params, headers=headers, _preload_content=_preload_content, + _request_timeout=_request_timeout, body=body) else: raise ValueError( diff --git a/samples/client/petstore/python/petstore_api/apis/fake_api.py b/samples/client/petstore/python/petstore_api/apis/fake_api.py index cdf79483bc9..44eb2a07e43 100644 --- a/samples/client/petstore/python/petstore_api/apis/fake_api.py +++ b/samples/client/petstore/python/petstore_api/apis/fake_api.py @@ -103,6 +103,7 @@ def test_client_model_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -159,6 +160,7 @@ def test_client_model_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def test_endpoint_parameters(self, number, double, pattern_without_delimiter, byte, **kwargs): @@ -239,6 +241,7 @@ def test_endpoint_parameters_with_http_info(self, number, double, pattern_withou all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -356,6 +359,7 @@ def test_endpoint_parameters_with_http_info(self, number, double, pattern_withou callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def test_enum_parameters(self, **kwargs): @@ -424,6 +428,7 @@ def test_enum_parameters_with_http_info(self, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -494,4 +499,5 @@ def test_enum_parameters_with_http_info(self, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) diff --git a/samples/client/petstore/python/petstore_api/apis/pet_api.py b/samples/client/petstore/python/petstore_api/apis/pet_api.py index d8c5eefc946..431b4deb39a 100644 --- a/samples/client/petstore/python/petstore_api/apis/pet_api.py +++ b/samples/client/petstore/python/petstore_api/apis/pet_api.py @@ -103,6 +103,7 @@ def add_pet_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -159,6 +160,7 @@ def add_pet_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def delete_pet(self, pet_id, **kwargs): @@ -215,6 +217,7 @@ def delete_pet_with_http_info(self, pet_id, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -273,6 +276,7 @@ def delete_pet_with_http_info(self, pet_id, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def find_pets_by_status(self, status, **kwargs): @@ -327,6 +331,7 @@ def find_pets_by_status_with_http_info(self, status, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -384,6 +389,7 @@ def find_pets_by_status_with_http_info(self, status, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def find_pets_by_tags(self, tags, **kwargs): @@ -438,6 +444,7 @@ def find_pets_by_tags_with_http_info(self, tags, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -495,6 +502,7 @@ def find_pets_by_tags_with_http_info(self, tags, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def get_pet_by_id(self, pet_id, **kwargs): @@ -549,6 +557,7 @@ def get_pet_by_id_with_http_info(self, pet_id, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -605,6 +614,7 @@ def get_pet_by_id_with_http_info(self, pet_id, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def update_pet(self, body, **kwargs): @@ -659,6 +669,7 @@ def update_pet_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -715,6 +726,7 @@ def update_pet_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def update_pet_with_form(self, pet_id, **kwargs): @@ -773,6 +785,7 @@ def update_pet_with_form_with_http_info(self, pet_id, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -833,6 +846,7 @@ def update_pet_with_form_with_http_info(self, pet_id, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def upload_file(self, pet_id, **kwargs): @@ -891,6 +905,7 @@ def upload_file_with_http_info(self, pet_id, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -951,4 +966,5 @@ def upload_file_with_http_info(self, pet_id, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) diff --git a/samples/client/petstore/python/petstore_api/apis/store_api.py b/samples/client/petstore/python/petstore_api/apis/store_api.py index adcf8d2b4ed..55a916ef18a 100644 --- a/samples/client/petstore/python/petstore_api/apis/store_api.py +++ b/samples/client/petstore/python/petstore_api/apis/store_api.py @@ -103,6 +103,7 @@ def delete_order_with_http_info(self, order_id, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -161,6 +162,7 @@ def delete_order_with_http_info(self, order_id, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def get_inventory(self, **kwargs): @@ -213,6 +215,7 @@ def get_inventory_with_http_info(self, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -264,6 +267,7 @@ def get_inventory_with_http_info(self, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def get_order_by_id(self, order_id, **kwargs): @@ -318,6 +322,7 @@ def get_order_by_id_with_http_info(self, order_id, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -378,6 +383,7 @@ def get_order_by_id_with_http_info(self, order_id, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def place_order(self, body, **kwargs): @@ -432,6 +438,7 @@ def place_order_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -488,4 +495,5 @@ def place_order_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) diff --git a/samples/client/petstore/python/petstore_api/apis/user_api.py b/samples/client/petstore/python/petstore_api/apis/user_api.py index 255b68a8c7f..6d23713217d 100644 --- a/samples/client/petstore/python/petstore_api/apis/user_api.py +++ b/samples/client/petstore/python/petstore_api/apis/user_api.py @@ -103,6 +103,7 @@ def create_user_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -159,6 +160,7 @@ def create_user_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def create_users_with_array_input(self, body, **kwargs): @@ -213,6 +215,7 @@ def create_users_with_array_input_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -269,6 +272,7 @@ def create_users_with_array_input_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def create_users_with_list_input(self, body, **kwargs): @@ -323,6 +327,7 @@ def create_users_with_list_input_with_http_info(self, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -379,6 +384,7 @@ def create_users_with_list_input_with_http_info(self, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def delete_user(self, username, **kwargs): @@ -433,6 +439,7 @@ def delete_user_with_http_info(self, username, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -489,6 +496,7 @@ def delete_user_with_http_info(self, username, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def get_user_by_name(self, username, **kwargs): @@ -543,6 +551,7 @@ def get_user_by_name_with_http_info(self, username, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -599,6 +608,7 @@ def get_user_by_name_with_http_info(self, username, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def login_user(self, username, password, **kwargs): @@ -655,6 +665,7 @@ def login_user_with_http_info(self, username, password, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -716,6 +727,7 @@ def login_user_with_http_info(self, username, password, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def logout_user(self, **kwargs): @@ -768,6 +780,7 @@ def logout_user_with_http_info(self, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -819,6 +832,7 @@ def logout_user_with_http_info(self, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) def update_user(self, username, body, **kwargs): @@ -875,6 +889,7 @@ def update_user_with_http_info(self, username, body, **kwargs): all_params.append('callback') all_params.append('_return_http_data_only') all_params.append('_preload_content') + all_params.append('_request_timeout') params = locals() for key, val in iteritems(params['kwargs']): @@ -936,4 +951,5 @@ def update_user_with_http_info(self, username, body, **kwargs): callback=params.get('callback'), _return_http_data_only=params.get('_return_http_data_only'), _preload_content=params.get('_preload_content', True), + _request_timeout=params.get('_request_timeout'), collection_formats=collection_formats) diff --git a/samples/client/petstore/python/petstore_api/rest.py b/samples/client/petstore/python/petstore_api/rest.py index c61b29c8120..3a98beec9b1 100644 --- a/samples/client/petstore/python/petstore_api/rest.py +++ b/samples/client/petstore/python/petstore_api/rest.py @@ -105,7 +105,7 @@ def __init__(self, pools_size=4): ) def request(self, method, url, query_params=None, headers=None, - body=None, post_params=None, _preload_content=True): + body=None, post_params=None, _preload_content=True, _request_timeout=None): """ :param method: http request method :param url: http request url @@ -117,6 +117,8 @@ def request(self, method, url, query_params=None, headers=None, 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'] @@ -129,6 +131,13 @@ def request(self, method, url, query_params=None, headers=None, post_params = post_params or {} headers = headers or {} + timeout = None + if _request_timeout: + if isinstance(_request_timeout, (int, ) if PY3 else (int, long)): + timeout = urllib3.Timeout(total=_request_timeout) + elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2: + timeout = urllib3.Timeout(connect=_request_timeout[0], read=_request_timeout[1]) + if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' @@ -144,12 +153,14 @@ def request(self, method, url, query_params=None, headers=None, r = self.pool_manager.request(method, url, body=request_body, preload_content=_preload_content, + timeout=timeout, headers=headers) elif headers['Content-Type'] == 'application/x-www-form-urlencoded': r = self.pool_manager.request(method, url, fields=post_params, encode_multipart=False, preload_content=_preload_content, + timeout=timeout, headers=headers) elif headers['Content-Type'] == 'multipart/form-data': # must del headers['Content-Type'], or the correct Content-Type @@ -159,6 +170,7 @@ def request(self, method, url, query_params=None, headers=None, fields=post_params, encode_multipart=True, preload_content=_preload_content, + timeout=timeout, headers=headers) # Pass a `string` parameter directly in the body to support # other content types than Json when `body` argument is provided @@ -168,6 +180,7 @@ def request(self, method, url, query_params=None, headers=None, r = self.pool_manager.request(method, url, body=request_body, preload_content=_preload_content, + timeout=timeout, headers=headers) else: # Cannot generate the request from given parameters @@ -179,6 +192,7 @@ def request(self, method, url, query_params=None, headers=None, r = self.pool_manager.request(method, url, fields=query_params, preload_content=_preload_content, + timeout=timeout, headers=headers) except urllib3.exceptions.SSLError as e: msg = "{0}\n{1}".format(type(e).__name__, str(e)) @@ -200,55 +214,66 @@ def request(self, method, url, query_params=None, headers=None, return r - def GET(self, url, headers=None, query_params=None, _preload_content=True): + def GET(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(self, url, headers=None, query_params=None, _preload_content=True): + def HEAD(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def OPTIONS(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(self, url, headers=None, query_params=None, body=None, _preload_content=True): + def DELETE(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def POST(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def PUT(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(self, url, headers=None, query_params=None, post_params=None, body=None, _preload_content=True): + def PATCH(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) diff --git a/samples/client/petstore/python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py index a539d2c598f..613b665cf5f 100644 --- a/samples/client/petstore/python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -15,8 +15,38 @@ from .util import id_gen +import json + +import urllib3 + HOST = 'http://petstore.swagger.io/v2' + +class TimeoutWithEqual(urllib3.Timeout): + def __init__(self, *arg, **kwargs): + super(TimeoutWithEqual, self).__init__(*arg, **kwargs) + + def __eq__(self, other): + return self._read == other._read and self._connect == other._connect and self.total == other.total + + +class MockPoolManager(object): + def __init__(self, tc): + self._tc = tc + self._reqs = [] + + def expect_request(self, *args, **kwargs): + self._reqs.append((args, kwargs)) + + def request(self, *args, **kwargs): + self._tc.assertTrue(len(self._reqs)>0) + r = self._reqs.pop(0) + self._tc.maxDiff = None + self._tc.assertEqual(r[0], args) + self._tc.assertEqual(r[1], kwargs) + return urllib3.HTTPResponse(status=200, body=b'test') + + class PetApiTests(unittest.TestCase): def setUp(self): @@ -65,6 +95,28 @@ def test_preload_content_flag(self): resp.close() resp.release_conn() + def test_timeout(self): + mock_pool = MockPoolManager(self) + self.api_client.rest_client.pool_manager = mock_pool + + mock_pool.expect_request('POST', 'http://petstore.swagger.io/v2/pet', + body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), + headers={'Content-Type': 'application/json', + 'Authorization': 'Bearer ', + 'Accept': 'application/json', + 'User-Agent': 'Swagger-Codegen/1.0.0/python'}, + preload_content=True, timeout=TimeoutWithEqual(total=5)) + mock_pool.expect_request('POST', 'http://petstore.swagger.io/v2/pet', + body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), + headers={'Content-Type': 'application/json', + 'Authorization': 'Bearer ', + 'Accept': 'application/json', + 'User-Agent': 'Swagger-Codegen/1.0.0/python'}, + preload_content=True, timeout=TimeoutWithEqual(connect=1, read=2)) + + self.pet_api.add_pet(body=self.pet, _request_timeout=5) + self.pet_api.add_pet(body=self.pet, _request_timeout=(1, 2)) + def test_create_api_instance(self): pet_api = petstore_api.PetApi() pet_api2 = petstore_api.PetApi()