From 3265ec0f724e77985bebd82a712c7991bd9a8491 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 24 Jul 2015 17:30:09 +0800 Subject: [PATCH 1/5] Refactor python client. --- .../src/main/resources/python/api.mustache | 8 +- .../main/resources/python/api_client.mustache | 138 +++++++++++------ .../resources/python/configuration.mustache | 63 +++++++- .../src/main/resources/python/model.mustache | 1 + .../src/main/resources/python/rest.mustache | 14 ++ .../src/main/resources/python/setup.mustache | 8 - samples/client/petstore/python/setup.py | 8 - .../python/swagger_client/api_client.py | 139 ++++++++++++------ .../python/swagger_client/apis/pet_api.py | 8 +- .../python/swagger_client/apis/store_api.py | 8 +- .../python/swagger_client/apis/user_api.py | 8 +- .../python/swagger_client/configuration.py | 63 +++++++- .../python/swagger_client/models/category.py | 1 + .../python/swagger_client/models/order.py | 1 + .../python/swagger_client/models/pet.py | 1 + .../python/swagger_client/models/tag.py | 1 + .../python/swagger_client/models/user.py | 1 + .../petstore/python/swagger_client/rest.py | 14 ++ 18 files changed, 364 insertions(+), 121 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 9811288e929..31d88380c90 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -16,10 +16,8 @@ Copyright 2015 SmartBear Software WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - -NOTE: This class is auto generated by the swagger code generator program. - Do not edit the class manually. """ + from __future__ import absolute_import import sys @@ -34,6 +32,10 @@ from ..api_client import ApiClient {{#operations}} class {{classname}}(object): + """ + NOTE: This class is auto generated by the swagger code generator program. + Do not edit the class manually. + """ def __init__(self, api_client=None): config = Configuration() 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 637bba9d765..409a7379745 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -1,10 +1,21 @@ #!/usr/bin/env python # coding: utf-8 -"""Swagger generic API client. This client handles the client- -server communication, and is invariant across implementations. Specifics of -the methods and models for each application are generated from the Swagger -templates.""" +""" +Copyright 2015 SmartBear Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" from __future__ import absolute_import from . import models @@ -38,15 +49,27 @@ from .configuration import Configuration class ApiClient(object): """ - Generic API client for Swagger client library builds + Generic API client for Swagger client library builds. - :param host: The base path for the server to call - :param header_name: a header to pass when making calls to the API - :param header_value: a header value to pass when making calls to the API + Swagger generic API client. This client handles the client- + server communication, and is invariant across implementations. Specifics of + the methods and models for each application are generated from the Swagger + templates. + + NOTE: This class is auto generated by the swagger code generator program. + https://github.com/swagger-api/swagger-codegen + Do not edit the class manually. + + :param host: The base path for the server to call. + :param header_name: a header to pass when making calls to the API. + :param header_value: a header value to pass when making calls to the API. """ def __init__(self, host=Configuration().host, header_name=None, header_value=None, cookie=None): + """ + Constructor of the class. + """ self.default_headers = {} if header_name is not None: self.default_headers[header_name] = header_value @@ -57,10 +80,16 @@ class ApiClient(object): @property def user_agent(self): + """ + Gets user agent. + """ return self.default_headers['User-Agent'] @user_agent.setter def user_agent(self, value): + """ + Sets user agent. + """ self.default_headers['User-Agent'] = value def set_default_header(self, header_name, header_value): @@ -129,11 +158,12 @@ class ApiClient(object): def to_path_value(self, obj): """ - Convert a string or object to a path-friendly value + Takes value and turn it into a string suitable for inclusion in + the path, by url-encoding. - :param obj: object or string value + :param obj: object or string value. - :return string: quoted value + :return string: quoted value. """ if type(obj) == list: return ','.join(obj) @@ -142,7 +172,7 @@ class ApiClient(object): def sanitize_for_serialization(self, obj): """ - Sanitize an object for Request. + Builds a JSON POST object. If obj is None, return None. If obj is str, int, float, bool, return directly. @@ -151,6 +181,9 @@ class ApiClient(object): If obj is list, santize each element in the list. If obj is dict, return the dict. If obj is swagger model, return the properties dict. + + :param obj: The data to serialize. + :return: The serialized form of data. """ if isinstance(obj, type(None)): return None @@ -181,13 +214,13 @@ class ApiClient(object): def deserialize(self, response, response_type): """ - Derialize response into an object. + Deserializes response into an object. - :param response: RESTResponse object to be deserialized + :param response: RESTResponse object to be deserialized. :param response_type: class literal for - deserialzied object, or string of class name + deserialzied object, or string of class name. - :return: deserialized object + :return: deserialized object. """ # handle file downloading # save response body into a tmp file and return the instance @@ -204,10 +237,12 @@ class ApiClient(object): def __deserialize(self, data, klass): """ - :param data: dict, list or str - :param klass: class literal, or string of class name + Deserializes dict, list, str into an object. - :return: object + :param data: dict, list or str. + :param klass: class literal, or string of class name. + + :return: object. """ if data is None: return None @@ -248,8 +283,7 @@ class ApiClient(object): body=None, post_params=None, files=None, response_type=None, auth_settings=None, callback=None): """ - Perform http request and return deserialized data - + Makes the HTTP request and return the deserialized data. :param resource_path: Path to method endpoint. :param method: Method to call. @@ -293,7 +327,7 @@ class ApiClient(object): def request(self, method, url, query_params=None, headers=None, post_params=None, body=None): """ - Perform http request using RESTClient. + Makes the HTTP request using RESTClient. """ if method == "GET": return RESTClient.GET(url, @@ -329,6 +363,13 @@ class ApiClient(object): ) def prepare_post_parameters(self, post_params=None, files=None): + """ + Builds form parameters. + + :param post_params: Normal form parameters. + :param files: File parameters. + :return: Form parameters with files. + """ params = {} if post_params: @@ -350,7 +391,10 @@ class ApiClient(object): def select_header_accept(self, accepts): """ - Return `Accept` based on an array of accepts provided + Returns `Accept` based on an array of accepts provided. + + :param accepts: List of headers. + :return: Accept (e.g. application/json). """ if not accepts: return @@ -364,7 +408,10 @@ class ApiClient(object): def select_header_content_type(self, content_types): """ - Return `Content-Type` baseed on an array of content_types provided + Returns `Content-Type` based on an array of content_types provided. + + :param content_types: List of content-types. + :return: Content-Type (e.g. application/json). """ if not content_types: return 'application/json' @@ -378,7 +425,11 @@ class ApiClient(object): def update_params_for_auth(self, headers, querys, auth_settings): """ - Update header and query params based on authentication setting + Updates header and query params based on authentication setting. + + :param headers: Header parameters dict to be updated. + :param querys: Query parameters dict to be updated. + :param auth_settings: Authentication setting identifiers list. """ config = Configuration() @@ -399,12 +450,12 @@ class ApiClient(object): def __deserialize_file(self, response): """ - Save response body into a file in (the defined) temporary folder, + Saves response body into a file in (the defined) temporary folder, using the filename from the `Content-Disposition` header if provided, otherwise a random filename. - :param response: RESTResponse - :return: file path + :param response: RESTResponse. + :return: file path. """ config = Configuration() @@ -426,12 +477,12 @@ class ApiClient(object): def __deserialize_primitive(self, data, klass): """ - Deserialize string to primitive type + Deserializes string to primitive type. - :param data: str - :param klass: class literal + :param data: str. + :param klass: class literal. - :return: int, float, str, bool + :return: int, float, str, bool. """ try: value = klass(data) @@ -443,16 +494,18 @@ class ApiClient(object): def __deserialize_object(self): """ - Deserialize empty object + Deserializes empty object. + + :return: object. """ return object() def __deserialize_date(self, string): """ - Deserialize string to date + Deserializes string to date. - :param string: str - :return: date + :param string: str. + :return: date. """ try: from dateutil.parser import parse @@ -468,12 +521,12 @@ class ApiClient(object): def __deserialize_datatime(self, string): """ - Deserialize string to datetime. + Deserializes string to datetime. The string should be in iso8601 datetime format. - :param string: str - :return: datetime + :param string: str. + :return: datetime. """ try: from dateutil.parser import parse @@ -489,10 +542,11 @@ class ApiClient(object): def __deserialize_model(self, data, klass): """ - Deserialize list or dict to model + Deserializes list or dict to model. - :param data: dict, list - :param klass: class literal + :param data: dict, list. + :param klass: class literal. + :return: model object. """ instance = klass() diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index bf26ab362b1..b732224b21e 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -1,3 +1,21 @@ +# coding: utf-8 + +""" +Copyright 2015 SmartBear Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" + from __future__ import absolute_import import base64 import urllib3 @@ -18,26 +36,45 @@ def singleton(cls, *args, **kw): @singleton class Configuration(object): + """ + NOTE: This class is auto generated by the swagger code generator program. + https://github.com/swagger-api/swagger-codegen + Do not edit the class manually. + """ def __init__(self): + """ + Constructor + """ # Default Base url self.host = "{{basePath}}" # Default api client self.api_client = None - # Temp file folder + # Temp file folder for download self.temp_folder_path = None + # Authentication Settings + # dict to store API key(s) self.api_key = {} + # dict to store API prefix (e.g. Bearer) self.api_key_prefix = {} + # Username for HTTP basic authentication self.username = "" + # Password for HTTP basic authentication self.password = "" + # Logging Settings self.logging_format = '%(asctime)s %(levelname)s %(message)s' + # Debug file location self.__logging_file = None + # Debug switch self.__debug = False self.init_logger() def init_logger(self): + """ + Initializes logger settings. + """ self.logger = logging.getLogger() formatter = logging.Formatter(self.logging_format) stream_handler = logging.StreamHandler() @@ -83,19 +120,32 @@ class Configuration(object): self.logger.setLevel(logging.WARNING) def get_api_key_with_prefix(self, key): - """ Return api key prepend prefix for key """ + """ + Gets API key (with prefix if set). + + :param key: Name of apiKey. + :return: The token for api key authentication. + """ if self.api_key.get(key) and self.api_key_prefix.get(key): return self.api_key_prefix[key] + ' ' + self.api_key[key] elif self.api_key.get(key): return self.api_key[key] def get_basic_auth_token(self): - """ Return basic auth header string """ + """ + Gets basic auth header string. + + :return: The token for basic HTTP authentication. + """ return urllib3.util.make_headers(basic_auth=self.username + ':' + self.password)\ .get('authorization') def auth_settings(self): - """ Return Auth Settings for api client """ + """ + Gets Auth Settings dict for api client. + + :return: The Auth Settings information dict. + """ return { {{#authMethods}} {{#isApiKey}} @@ -120,6 +170,11 @@ class Configuration(object): } def to_debug_report(self): + """ + Gets the essential information for debugging. + + :return: The report for debugging. + """ return "Python SDK Debug Report:\n"\ "OS: {env}\n"\ "Python Version: {pyversion}\n"\ diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 7314e9ee9cd..074a66395fe 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -16,6 +16,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. """ + {{#models}} {{#model}} from pprint import pformat diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index fab813a4cc9..f6161a6d60c 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -1,6 +1,20 @@ # coding: utf-8 """ +Copyright 2015 SmartBear Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK: https://www.dropbox.com/developers/core/sdks/python """ diff --git a/modules/swagger-codegen/src/main/resources/python/setup.mustache b/modules/swagger-codegen/src/main/resources/python/setup.mustache index 9c19e413e9c..f3122f9f39c 100644 --- a/modules/swagger-codegen/src/main/resources/python/setup.mustache +++ b/modules/swagger-codegen/src/main/resources/python/setup.mustache @@ -34,11 +34,3 @@ setup( ) {{/hasMore}}{{/apis}}{{/apiInfo}} - - - - - - - - diff --git a/samples/client/petstore/python/setup.py b/samples/client/petstore/python/setup.py index ffc6f6a3ad2..2b50c5c4cf5 100644 --- a/samples/client/petstore/python/setup.py +++ b/samples/client/petstore/python/setup.py @@ -33,11 +33,3 @@ setup( ) - - - - - - - - diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index 37bf1be69c0..9a1f6c6da76 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -1,10 +1,21 @@ #!/usr/bin/env python # coding: utf-8 -"""Swagger generic API client. This client handles the client- -server communication, and is invariant across implementations. Specifics of -the methods and models for each application are generated from the Swagger -templates.""" +""" +Copyright 2015 SmartBear Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" from __future__ import absolute_import from . import models @@ -38,15 +49,26 @@ from .configuration import Configuration class ApiClient(object): """ - Generic API client for Swagger client library builds + Generic API client for Swagger client library builds. - :param host: The base path for the server to call - :param header_name: a header to pass when making calls to the API - :param header_value: a header value to pass when making calls to the API + Swagger generic API client. This client handles the client- + server communication, and is invariant across implementations. Specifics of + the methods and models for each application are generated from the Swagger + templates. + + NOTE: This class is auto generated by the swagger code generator program. + https://github.com/swagger-api/swagger-codegen + Do not edit the class manually. + + :param host: The base path for the server to call. + :param header_name: a header to pass when making calls to the API. + :param header_value: a header value to pass when making calls to the API. """ def __init__(self, host=Configuration().host, header_name=None, header_value=None): - + """ + Constructor of the class. + """ self.default_headers = {} if header_name is not None: self.default_headers[header_name] = header_value @@ -57,10 +79,16 @@ class ApiClient(object): @property def user_agent(self): + """ + Gets user agent. + """ return self.default_headers['User-Agent'] @user_agent.setter def user_agent(self, value): + """ + Sets user agent. + """ self.default_headers['User-Agent'] = value def set_default_header(self, header_name, header_value): @@ -129,11 +157,12 @@ class ApiClient(object): def to_path_value(self, obj): """ - Convert a string or object to a path-friendly value + Takes value and turn it into a string suitable for inclusion in + the path, by url-encoding. - :param obj: object or string value + :param obj: object or string value. - :return string: quoted value + :return string: quoted value. """ if type(obj) == list: return ','.join(obj) @@ -142,7 +171,7 @@ class ApiClient(object): def sanitize_for_serialization(self, obj): """ - Sanitize an object for Request. + Builds a JSON POST object. If obj is None, return None. If obj is str, int, float, bool, return directly. @@ -151,6 +180,9 @@ class ApiClient(object): If obj is list, santize each element in the list. If obj is dict, return the dict. If obj is swagger model, return the properties dict. + + :param obj: The data to serialize. + :return: The serialized form of data. """ if isinstance(obj, type(None)): return None @@ -181,13 +213,13 @@ class ApiClient(object): def deserialize(self, response, response_type): """ - Derialize response into an object. + Deserializes response into an object. - :param response: RESTResponse object to be deserialized + :param response: RESTResponse object to be deserialized. :param response_type: class literal for - deserialzied object, or string of class name + deserialzied object, or string of class name. - :return: deserialized object + :return: deserialized object. """ # handle file downloading # save response body into a tmp file and return the instance @@ -204,10 +236,12 @@ class ApiClient(object): def __deserialize(self, data, klass): """ - :param data: dict, list or str - :param klass: class literal, or string of class name + Deserializes dict, list, str into an object. - :return: object + :param data: dict, list or str. + :param klass: class literal, or string of class name. + + :return: object. """ if data is None: return None @@ -248,8 +282,7 @@ class ApiClient(object): body=None, post_params=None, files=None, response_type=None, auth_settings=None, callback=None): """ - Perform http request and return deserialized data - + Makes the HTTP request and return the deserialized data. :param resource_path: Path to method endpoint. :param method: Method to call. @@ -293,7 +326,7 @@ class ApiClient(object): def request(self, method, url, query_params=None, headers=None, post_params=None, body=None): """ - Perform http request using RESTClient. + Makes the HTTP request using RESTClient. """ if method == "GET": return RESTClient.GET(url, @@ -329,6 +362,13 @@ class ApiClient(object): ) def prepare_post_parameters(self, post_params=None, files=None): + """ + Builds form parameters. + + :param post_params: Normal form parameters. + :param files: File parameters. + :return: Form parameters with files. + """ params = {} if post_params: @@ -350,7 +390,10 @@ class ApiClient(object): def select_header_accept(self, accepts): """ - Return `Accept` based on an array of accepts provided + Returns `Accept` based on an array of accepts provided. + + :param accepts: List of headers. + :return: Accept (e.g. application/json). """ if not accepts: return @@ -364,7 +407,10 @@ class ApiClient(object): def select_header_content_type(self, content_types): """ - Return `Content-Type` baseed on an array of content_types provided + Returns `Content-Type` based on an array of content_types provided. + + :param content_types: List of content-types. + :return: Content-Type (e.g. application/json). """ if not content_types: return 'application/json' @@ -378,7 +424,11 @@ class ApiClient(object): def update_params_for_auth(self, headers, querys, auth_settings): """ - Update header and query params based on authentication setting + Updates header and query params based on authentication setting. + + :param headers: Header parameters dict to be updated. + :param querys: Query parameters dict to be updated. + :param auth_settings: Authentication setting identifiers list. """ config = Configuration() @@ -399,12 +449,12 @@ class ApiClient(object): def __deserialize_file(self, response): """ - Save response body into a file in (the defined) temporary folder, + Saves response body into a file in (the defined) temporary folder, using the filename from the `Content-Disposition` header if provided, otherwise a random filename. - :param response: RESTResponse - :return: file path + :param response: RESTResponse. + :return: file path. """ config = Configuration() @@ -426,12 +476,12 @@ class ApiClient(object): def __deserialize_primitive(self, data, klass): """ - Deserialize string to primitive type + Deserializes string to primitive type. - :param data: str - :param klass: class literal + :param data: str. + :param klass: class literal. - :return: int, float, str, bool + :return: int, float, str, bool. """ try: value = klass(data) @@ -443,16 +493,18 @@ class ApiClient(object): def __deserialize_object(self): """ - Deserialize empty object + Deserializes empty object. + + :return: object. """ return object() def __deserialize_date(self, string): """ - Deserialize string to date + Deserializes string to date. - :param string: str - :return: date + :param string: str. + :return: date. """ try: from dateutil.parser import parse @@ -468,12 +520,12 @@ class ApiClient(object): def __deserialize_datatime(self, string): """ - Deserialize string to datetime. + Deserializes string to datetime. The string should be in iso8601 datetime format. - :param string: str - :return: datetime + :param string: str. + :return: datetime. """ try: from dateutil.parser import parse @@ -489,10 +541,11 @@ class ApiClient(object): def __deserialize_model(self, data, klass): """ - Deserialize list or dict to model + Deserializes list or dict to model. - :param data: dict, list - :param klass: class literal + :param data: dict, list. + :param klass: class literal. + :return: model object. """ instance = klass() diff --git a/samples/client/petstore/python/swagger_client/apis/pet_api.py b/samples/client/petstore/python/swagger_client/apis/pet_api.py index 3f3e523a15a..3c80becf7d9 100644 --- a/samples/client/petstore/python/swagger_client/apis/pet_api.py +++ b/samples/client/petstore/python/swagger_client/apis/pet_api.py @@ -16,10 +16,8 @@ Copyright 2015 SmartBear Software WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - -NOTE: This class is auto generated by the swagger code generator program. - Do not edit the class manually. """ + from __future__ import absolute_import import sys @@ -33,6 +31,10 @@ from ..api_client import ApiClient class PetApi(object): + """ + NOTE: This class is auto generated by the swagger code generator program. + Do not edit the class manually. + """ def __init__(self, api_client=None): config = Configuration() diff --git a/samples/client/petstore/python/swagger_client/apis/store_api.py b/samples/client/petstore/python/swagger_client/apis/store_api.py index ac465f792ec..8e46f786192 100644 --- a/samples/client/petstore/python/swagger_client/apis/store_api.py +++ b/samples/client/petstore/python/swagger_client/apis/store_api.py @@ -16,10 +16,8 @@ Copyright 2015 SmartBear Software WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - -NOTE: This class is auto generated by the swagger code generator program. - Do not edit the class manually. """ + from __future__ import absolute_import import sys @@ -33,6 +31,10 @@ from ..api_client import ApiClient class StoreApi(object): + """ + NOTE: This class is auto generated by the swagger code generator program. + Do not edit the class manually. + """ def __init__(self, api_client=None): config = Configuration() diff --git a/samples/client/petstore/python/swagger_client/apis/user_api.py b/samples/client/petstore/python/swagger_client/apis/user_api.py index 0469e83ae2f..83158956c39 100644 --- a/samples/client/petstore/python/swagger_client/apis/user_api.py +++ b/samples/client/petstore/python/swagger_client/apis/user_api.py @@ -16,10 +16,8 @@ Copyright 2015 SmartBear Software WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - -NOTE: This class is auto generated by the swagger code generator program. - Do not edit the class manually. """ + from __future__ import absolute_import import sys @@ -33,6 +31,10 @@ from ..api_client import ApiClient class UserApi(object): + """ + NOTE: This class is auto generated by the swagger code generator program. + Do not edit the class manually. + """ def __init__(self, api_client=None): config = Configuration() diff --git a/samples/client/petstore/python/swagger_client/configuration.py b/samples/client/petstore/python/swagger_client/configuration.py index 0fe945bee1b..cfe4100e9bd 100644 --- a/samples/client/petstore/python/swagger_client/configuration.py +++ b/samples/client/petstore/python/swagger_client/configuration.py @@ -1,3 +1,21 @@ +# coding: utf-8 + +""" +Copyright 2015 SmartBear Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" + from __future__ import absolute_import import base64 import urllib3 @@ -18,26 +36,45 @@ def singleton(cls, *args, **kw): @singleton class Configuration(object): + """ + NOTE: This class is auto generated by the swagger code generator program. + https://github.com/swagger-api/swagger-codegen + Do not edit the class manually. + """ def __init__(self): + """ + Constructor + """ # Default Base url self.host = "http://petstore.swagger.io/v2" # Default api client self.api_client = None - # Temp file folder + # Temp file folder for download self.temp_folder_path = None + # Authentication Settings + # dict to store API key(s) self.api_key = {} + # dict to store API prefix (e.g. Bearer) self.api_key_prefix = {} + # Username for HTTP basic authentication self.username = "" + # Password for HTTP basic authentication self.password = "" + # Logging Settings self.logging_format = '%(asctime)s %(levelname)s %(message)s' + # Debug file location self.__logging_file = None + # Debug switch self.__debug = False self.init_logger() def init_logger(self): + """ + Initializes logger settings. + """ self.logger = logging.getLogger() formatter = logging.Formatter(self.logging_format) stream_handler = logging.StreamHandler() @@ -83,19 +120,32 @@ class Configuration(object): self.logger.setLevel(logging.WARNING) def get_api_key_with_prefix(self, key): - """ Return api key prepend prefix for key """ + """ + Gets API key (with prefix if set). + + :param key: Name of apiKey. + :return: The token for api key authentication. + """ if self.api_key.get(key) and self.api_key_prefix.get(key): return self.api_key_prefix[key] + ' ' + self.api_key[key] elif self.api_key.get(key): return self.api_key[key] def get_basic_auth_token(self): - """ Return basic auth header string """ + """ + Gets basic auth header string. + + :return: The token for basic HTTP authentication. + """ return urllib3.util.make_headers(basic_auth=self.username + ':' + self.password)\ .get('authorization') def auth_settings(self): - """ Return Auth Settings for api client """ + """ + Gets Auth Settings dict for api client. + + :return: The Auth Settings information dict. + """ return { 'api_key': { @@ -107,6 +157,11 @@ class Configuration(object): } def to_debug_report(self): + """ + Gets the essential information for debugging. + + :return: The report for debugging. + """ return "Python SDK Debug Report:\n"\ "OS: {env}\n"\ "Python Version: {pyversion}\n"\ diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index ae03bb7ec78..65b0ac40f85 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -16,6 +16,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. """ + from pprint import pformat from six import iteritems diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index 7498561372e..e6a11470417 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -16,6 +16,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. """ + from pprint import pformat from six import iteritems diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index 62f7bea7fd2..fe040ec1a48 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -16,6 +16,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. """ + from pprint import pformat from six import iteritems diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index ea07acacfdb..abc35e419c8 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -16,6 +16,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. """ + from pprint import pformat from six import iteritems diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index d81a73318e7..1fd25a1d5aa 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -16,6 +16,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. """ + from pprint import pformat from six import iteritems diff --git a/samples/client/petstore/python/swagger_client/rest.py b/samples/client/petstore/python/swagger_client/rest.py index fab813a4cc9..f6161a6d60c 100644 --- a/samples/client/petstore/python/swagger_client/rest.py +++ b/samples/client/petstore/python/swagger_client/rest.py @@ -1,6 +1,20 @@ # coding: utf-8 """ +Copyright 2015 SmartBear Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK: https://www.dropbox.com/developers/core/sdks/python """ From b51ef77401983d6ddcbee7f042a0036de91a91cb Mon Sep 17 00:00:00 2001 From: geekerzp Date: Mon, 27 Jul 2015 15:14:47 +0800 Subject: [PATCH 2/5] Remove `#!/usr/bin/env python` from python client --- modules/swagger-codegen/src/main/resources/python/api.mustache | 1 - .../src/main/resources/python/api_client.mustache | 1 - .../swagger-codegen/src/main/resources/python/model.mustache | 1 - .../swagger-codegen/src/main/resources/python/setup.mustache | 3 ++- samples/client/petstore/python/setup.py | 2 ++ samples/client/petstore/python/swagger_client/api_client.py | 1 - samples/client/petstore/python/swagger_client/apis/pet_api.py | 1 - .../client/petstore/python/swagger_client/apis/store_api.py | 1 - samples/client/petstore/python/swagger_client/apis/user_api.py | 1 - .../client/petstore/python/swagger_client/models/category.py | 1 - samples/client/petstore/python/swagger_client/models/order.py | 1 - samples/client/petstore/python/swagger_client/models/pet.py | 1 - samples/client/petstore/python/swagger_client/models/tag.py | 1 - samples/client/petstore/python/swagger_client/models/user.py | 1 - 14 files changed, 4 insertions(+), 13 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 31d88380c90..3620e9fb9ee 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ 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 409a7379745..24b1e52ff3e 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 074a66395fe..40140ea0aa9 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/modules/swagger-codegen/src/main/resources/python/setup.mustache b/modules/swagger-codegen/src/main/resources/python/setup.mustache index f3122f9f39c..a939ce01958 100644 --- a/modules/swagger-codegen/src/main/resources/python/setup.mustache +++ b/modules/swagger-codegen/src/main/resources/python/setup.mustache @@ -1,4 +1,5 @@ -# -*- coding: utf-8 -*- +# coding: utf-8 + import sys from setuptools import setup, find_packages diff --git a/samples/client/petstore/python/setup.py b/samples/client/petstore/python/setup.py index 2b50c5c4cf5..e66e9689343 100644 --- a/samples/client/petstore/python/setup.py +++ b/samples/client/petstore/python/setup.py @@ -1,3 +1,5 @@ +# coding: utf-8 + import sys from setuptools import setup, find_packages diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index 9a1f6c6da76..a183bb4cda3 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/apis/pet_api.py b/samples/client/petstore/python/swagger_client/apis/pet_api.py index 3c80becf7d9..1c34da118c1 100644 --- a/samples/client/petstore/python/swagger_client/apis/pet_api.py +++ b/samples/client/petstore/python/swagger_client/apis/pet_api.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/apis/store_api.py b/samples/client/petstore/python/swagger_client/apis/store_api.py index 8e46f786192..4dca0af85e2 100644 --- a/samples/client/petstore/python/swagger_client/apis/store_api.py +++ b/samples/client/petstore/python/swagger_client/apis/store_api.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/apis/user_api.py b/samples/client/petstore/python/swagger_client/apis/user_api.py index 83158956c39..4897abe8801 100644 --- a/samples/client/petstore/python/swagger_client/apis/user_api.py +++ b/samples/client/petstore/python/swagger_client/apis/user_api.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index 65b0ac40f85..cba8855b1ba 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index e6a11470417..0e75af96779 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index fe040ec1a48..8c210525d3a 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index abc35e419c8..4f8ca6036e1 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index 1fd25a1d5aa..4994ba691dc 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # coding: utf-8 """ From 8bd282acd6d01ba610588948ce7ff6d7bcc09e67 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Mon, 27 Jul 2015 15:47:13 +0800 Subject: [PATCH 3/5] Change parameter name `key` to `identifier` of Configuration#get_api_key_with_prefix in python client. --- .../src/main/resources/python/configuration.mustache | 12 ++++++------ .../petstore/python/swagger_client/api_client.py | 5 +++-- .../petstore/python/swagger_client/configuration.py | 12 ++++++------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index b732224b21e..f2a52cb6e84 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -119,17 +119,17 @@ class Configuration(object): # setting log level to default `logging.WARNING` self.logger.setLevel(logging.WARNING) - def get_api_key_with_prefix(self, key): + def get_api_key_with_prefix(self, identifier): """ Gets API key (with prefix if set). - :param key: Name of apiKey. + :param identifier: The identifier of apiKey. :return: The token for api key authentication. """ - if self.api_key.get(key) and self.api_key_prefix.get(key): - return self.api_key_prefix[key] + ' ' + self.api_key[key] - elif self.api_key.get(key): - return self.api_key[key] + if self.api_key.get(identifier) and self.api_key_prefix.get(identifier): + return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier] + elif self.api_key.get(identifier): + return self.api_key[identifier] def get_basic_auth_token(self): """ diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index a183bb4cda3..934cfda1c1a 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -64,7 +64,8 @@ class ApiClient(object): :param header_value: a header value to pass when making calls to the API. """ def __init__(self, host=Configuration().host, - header_name=None, header_value=None): + header_name=None, header_value=None, cookie=None): + """ Constructor of the class. """ @@ -72,7 +73,7 @@ class ApiClient(object): if header_name is not None: self.default_headers[header_name] = header_value self.host = host - self.cookie = None + self.cookie = cookie # Set default User-Agent. self.user_agent = 'Python-Swagger' diff --git a/samples/client/petstore/python/swagger_client/configuration.py b/samples/client/petstore/python/swagger_client/configuration.py index cfe4100e9bd..a175303a222 100644 --- a/samples/client/petstore/python/swagger_client/configuration.py +++ b/samples/client/petstore/python/swagger_client/configuration.py @@ -119,17 +119,17 @@ class Configuration(object): # setting log level to default `logging.WARNING` self.logger.setLevel(logging.WARNING) - def get_api_key_with_prefix(self, key): + def get_api_key_with_prefix(self, identifier): """ Gets API key (with prefix if set). - :param key: Name of apiKey. + :param identifier: The identifier of apiKey. :return: The token for api key authentication. """ - if self.api_key.get(key) and self.api_key_prefix.get(key): - return self.api_key_prefix[key] + ' ' + self.api_key[key] - elif self.api_key.get(key): - return self.api_key[key] + if self.api_key.get(identifier) and self.api_key_prefix.get(identifier): + return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier] + elif self.api_key.get(identifier): + return self.api_key[identifier] def get_basic_auth_token(self): """ From ea901bf1ce3f0f0868854da11a4abceb7f4352d9 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 28 Jul 2015 14:34:50 +0800 Subject: [PATCH 4/5] Update python client. Add test case for nested dict deserialization. --- .../petstore/python/swagger_client/api_client.py | 2 +- .../petstore/python/tests/test_deserialization.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index 934cfda1c1a..24b1e52ff3e 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -253,7 +253,7 @@ class ApiClient(object): for sub_data in data] if 'dict(' in klass: - sub_kls = re.match('dict\((.*), (.*)\)', klass).group(2) + sub_kls = re.match('dict\(([^,]*), (.*)\)', klass).group(2) return {k: self.__deserialize(v, sub_kls) for k, v in iteritems(data)} diff --git a/samples/client/petstore/python/tests/test_deserialization.py b/samples/client/petstore/python/tests/test_deserialization.py index 1121558adca..b205c2f165e 100644 --- a/samples/client/petstore/python/tests/test_deserialization.py +++ b/samples/client/petstore/python/tests/test_deserialization.py @@ -150,6 +150,19 @@ class DeserializationTests(unittest.TestCase): self.assertEqual(deserialized[0].name, "doggie0") self.assertEqual(deserialized[1].name, "doggie1") + def test_deserialize_nested_dict(self): + """ deserialize dict(str, dict(str, int)) """ + data = { + "foo": { + "bar": 1 + } + } + + deserialized = self.deserialize(data, "dict(str, dict(str, int))") + self.assertTrue(isinstance(deserialized, dict)) + self.assertTrue(isinstance(deserialized["foo"], dict)) + self.assertTrue(isinstance(deserialized["foo"]["bar"], int)) + def test_deserialize_none(self): """ deserialize None """ deserialized = self.deserialize(None, "datetime") From 2be34143578b87b85d33dedfeb2d1a7bc4713c23 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 28 Jul 2015 15:23:44 +0800 Subject: [PATCH 5/5] Update python client. Add test case for nested list deserialization. --- .../client/petstore/python/tests/test_deserialization.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/samples/client/petstore/python/tests/test_deserialization.py b/samples/client/petstore/python/tests/test_deserialization.py index b205c2f165e..1eae045ff76 100644 --- a/samples/client/petstore/python/tests/test_deserialization.py +++ b/samples/client/petstore/python/tests/test_deserialization.py @@ -163,6 +163,15 @@ class DeserializationTests(unittest.TestCase): self.assertTrue(isinstance(deserialized["foo"], dict)) self.assertTrue(isinstance(deserialized["foo"]["bar"], int)) + def test_deserialize_nested_list(self): + """ deserialize list[list[str]] """ + data = [["foo"]] + + deserialized = self.deserialize(data, "list[list[str]]") + self.assertTrue(isinstance(deserialized, list)) + self.assertTrue(isinstance(deserialized[0], list)) + self.assertTrue(isinstance(deserialized[0][0], str)) + def test_deserialize_none(self): """ deserialize None """ deserialized = self.deserialize(None, "datetime")