Skip to content

[Python] Make API client more pluggable #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public PythonClientCodegen() {
supportingFiles.add(new SupportingFile("swagger.mustache", invokerPackage, "swagger.py"));
supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py"));
supportingFiles.add(new SupportingFile("util.mustache", invokerPackage, "util.py"));
supportingFiles.add(new SupportingFile("config.mustache", invokerPackage, "config.py"));
supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage.replace('.', File.separatorChar), "__init__.py"));
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage.replace('.', File.separatorChar), "__init__.py"));
Expand Down
13 changes: 8 additions & 5 deletions modules/swagger-codegen/src/main/resources/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ from six import iteritems

from ..util import remove_none

from ..swagger import ApiClient
from .. import config

{{#operations}}
class {{classname}}(object):

def __init__(self, api_client):
self.api_client = api_client
def __init__(self, api_client=None):
if api_client:
self.api_client = api_client
else:
self.api_client = config.api_client
{{#operation}}
def {{nickname}}(self, {{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}**kwargs):
"""
Expand Down Expand Up @@ -71,12 +74,12 @@ class {{classname}}(object):
body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
header_params['Accept'] = self.api_client.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
header_params['Content-Type'] = self.api_client.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import absolute_import

from .swagger import ApiClient

# Configuration variables

api_client = ApiClient("{{basePath}}")

23 changes: 10 additions & 13 deletions modules/swagger-codegen/src/main/resources/python/swagger.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,28 @@ class ApiClient(object):
if self.cookie:
headers['Cookie'] = self.cookie
if headers:
headers = ApiClient.sanitize_for_serialization(headers)
headers = self.sanitize_for_serialization(headers)

# path parameters
if path_params:
path_params = ApiClient.sanitize_for_serialization(path_params)
path_params = self.sanitize_for_serialization(path_params)
for k, v in iteritems(path_params):
replacement = quote(str(self.to_path_value(v)))
resource_path = resource_path.replace('{' + k + '}', replacement)

# query parameters
if query_params:
query_params = ApiClient.sanitize_for_serialization(query_params)
query_params = self.sanitize_for_serialization(query_params)
query_params = {k: self.to_path_value(v) for k, v in iteritems(query_params)}

# post parameters
if post_params:
post_params = self.prepare_post_parameters(post_params, files)
post_params = ApiClient.sanitize_for_serialization(post_params)
post_params = self.sanitize_for_serialization(post_params)

# body
if body:
body = ApiClient.sanitize_for_serialization(body)
body = self.sanitize_for_serialization(body)

# request url
url = self.host + resource_path
Expand Down Expand Up @@ -115,8 +115,7 @@ class ApiClient(object):
else:
return str(obj)

@staticmethod
def sanitize_for_serialization(obj):
def sanitize_for_serialization(self, obj):
"""
Sanitize an object for Request.

Expand All @@ -132,7 +131,7 @@ class ApiClient(object):
elif isinstance(obj, (str, int, float, bool, tuple)):
return obj
elif isinstance(obj, list):
return [ApiClient.sanitize_for_serialization(sub_obj) for sub_obj in obj]
return [self.sanitize_for_serialization(sub_obj) for sub_obj in obj]
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
else:
Expand All @@ -145,7 +144,7 @@ class ApiClient(object):
obj_dict = {obj.attribute_map[key]: val
for key, val in iteritems(obj.__dict__)
if key != 'swagger_types' and key != 'attribute_map' and val is not None}
return {key: ApiClient.sanitize_for_serialization(val)
return {key: self.sanitize_for_serialization(val)
for key, val in iteritems(obj_dict)}

def deserialize(self, obj, obj_class):
Expand Down Expand Up @@ -253,8 +252,7 @@ class ApiClient(object):

return params

@staticmethod
def select_header_accept(accepts):
def select_header_accept(self, accepts):
"""
Return `Accept` based on an array of accepts provided
"""
Expand All @@ -268,8 +266,7 @@ class ApiClient(object):
else:
return ', '.join(accepts)

@staticmethod
def select_header_content_type(content_types):
def select_header_content_type(self, content_types):
"""
Return `Content-Type` baseed on an array of content_types provided
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@

from ..util import remove_none

from ..swagger import ApiClient
from .. import config

class PetApi(object):

def __init__(self, api_client):
self.api_client = api_client
def __init__(self, api_client=None):
if api_client:
self.api_client = api_client
else:
self.api_client = config.api_client

def update_pet(self, **kwargs):
"""
Expand Down Expand Up @@ -66,12 +69,12 @@ def update_pet(self, **kwargs):
body_params = params.get('body')

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml'])
header_params['Content-Type'] = self.api_client.select_header_content_type(['application/json', 'application/xml'])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -107,12 +110,12 @@ def add_pet(self, **kwargs):
body_params = params.get('body')

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml'])
header_params['Content-Type'] = self.api_client.select_header_content_type(['application/json', 'application/xml'])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -148,12 +151,12 @@ def find_pets_by_status(self, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -191,12 +194,12 @@ def find_pets_by_tags(self, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -238,12 +241,12 @@ def get_pet_by_id(self, pet_id, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -287,12 +290,12 @@ def update_pet_with_form(self, pet_id, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/x-www-form-urlencoded'])
header_params['Content-Type'] = self.api_client.select_header_content_type(['application/x-www-form-urlencoded'])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -333,12 +336,12 @@ def delete_pet(self, pet_id, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -380,12 +383,12 @@ def upload_file(self, pet_id, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type(['multipart/form-data'])
header_params['Content-Type'] = self.api_client.select_header_content_type(['multipart/form-data'])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@

from ..util import remove_none

from ..swagger import ApiClient
from .. import config

class StoreApi(object):

def __init__(self, api_client):
self.api_client = api_client
def __init__(self, api_client=None):
if api_client:
self.api_client = api_client
else:
self.api_client = config.api_client

def get_inventory(self, **kwargs):
"""
Expand Down Expand Up @@ -65,12 +68,12 @@ def get_inventory(self, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -108,12 +111,12 @@ def place_order(self, **kwargs):
body_params = params.get('body')

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -155,12 +158,12 @@ def get_order_by_id(self, order_id, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down Expand Up @@ -202,12 +205,12 @@ def delete_order(self, order_id, **kwargs):
body_params = None

# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
if not header_params['Accept']:
del header_params['Accept']

# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([])
header_params['Content-Type'] = self.api_client.select_header_content_type([])

response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
Expand Down
Loading