forked from loafle/openapi-generator-original
refactor python client using urllib3
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env python
|
||||
"""Add all of the modules in the current directory to __all__"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
|
||||
{{#apiInfo}}{{#apis}}
|
||||
from .{{classVarName}} import {{classname}}
|
||||
{{/apis}}{{/apiInfo}}
|
||||
|
||||
__all__ = []
|
||||
|
||||
for module in os.listdir(os.path.dirname(__file__)):
|
||||
if module != '__init__.py' and module[-3:] == '.py':
|
||||
__all__.append(module[:-3])
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
"""Add all of the modules in the current directory to __all__"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
|
||||
{{#models}}{{#model}}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
"""Add all of the modules in the current directory to __all__"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
|
||||
# import models into package
|
||||
@@ -9,7 +11,7 @@ from .models.{{classVarName}} import {{classname}}
|
||||
|
||||
# import apis into package
|
||||
{{#apiInfo}}{{#apis}}
|
||||
from .{{classVarName}} import {{classname}}
|
||||
from .apis.{{classVarName}} import {{classname}}
|
||||
{{/apis}}{{/apiInfo}}
|
||||
|
||||
# import ApiClient
|
||||
|
||||
@@ -21,98 +21,65 @@ NOTE: This class is auto generated by the swagger code generator program. Do not
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import urllib
|
||||
|
||||
from models import *
|
||||
|
||||
# python 2 and python 3 compatibility library
|
||||
from six import iteritems
|
||||
|
||||
{{#operations}}
|
||||
class {{classname}}(object):
|
||||
|
||||
def __init__(self, apiClient):
|
||||
self.apiClient = apiClient
|
||||
def __init__(self, api_client):
|
||||
self.api_client = api_client
|
||||
|
||||
{{newline}}
|
||||
{{#operation}}
|
||||
def {{nickname}}(self, {{#requiredParams}}{{paramName}}{{#defaultValue}} = None{{/defaultValue}}, {{/requiredParams}}**kwargs):
|
||||
"""{{{summary}}}
|
||||
"""
|
||||
{{{summary}}}
|
||||
{{{notes}}}
|
||||
|
||||
Args:
|
||||
{{#allParams}}{{paramName}}, {{dataType}}: {{{description}}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
|
||||
{{/allParams}}
|
||||
{{#allParams}}
|
||||
:param {{dataType}} {{paramName}}: {{{description}}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
|
||||
{{/allParams}}
|
||||
|
||||
Returns: {{returnType}}
|
||||
:return: {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}None{{/returnType}}
|
||||
"""
|
||||
|
||||
allParams = [{{#allParams}}'{{paramName}}'{{#hasMore}}, {{/hasMore}}{{/allParams}}]
|
||||
all_params = [{{#allParams}}'{{paramName}}'{{#hasMore}}, {{/hasMore}}{{/allParams}}]
|
||||
|
||||
params = locals()
|
||||
for (key, val) in params['kwargs'].iteritems():
|
||||
if key not in allParams:
|
||||
for key, val in iteritems(params['kwargs']):
|
||||
if key not in all_params:
|
||||
raise TypeError("Got an unexpected keyword argument '%s' to method {{nickname}}" % key)
|
||||
params[key] = val
|
||||
del params['kwargs']
|
||||
|
||||
resourcePath = '{{path}}'
|
||||
resourcePath = resourcePath.replace('{format}', 'json')
|
||||
resource_path = '{{path}}'.replace('{format}', 'json')
|
||||
method = '{{httpMethod}}'
|
||||
|
||||
queryParams = {}
|
||||
headerParams = {}
|
||||
formParams = {}
|
||||
files = {}
|
||||
bodyParam = None
|
||||
path_params = dict({{#pathParams}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/pathParams}})
|
||||
path_params = {k: v for k, v in iteritems(path_params) if v}
|
||||
query_params = dict({{#queryParams}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/queryParams}})
|
||||
query_params = {k: v for k, v in iteritems(query_params) if v}
|
||||
header_params = dict({{#headerParams}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/headerParams}})
|
||||
header_params = {k: v for k, v in iteritems(header_params) if v}
|
||||
form_params = dict({{#formParams}}{{^isFile}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/isFile}}{{/formParams}})
|
||||
form_params = {k: v for k, v in iteritems(form_params) if v}
|
||||
files = dict({{#formParams}}{{#isFile}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/isFile}}{{/formParams}})
|
||||
files = {k: v for k, v in iteritems(files) if v}
|
||||
body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}
|
||||
|
||||
accepts = [{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]
|
||||
headerParams['Accept'] = ', '.join(accepts)
|
||||
header_params['Accept'] = ', '.join(accepts)
|
||||
|
||||
content_types = [{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]
|
||||
headerParams['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
|
||||
|
||||
{{#queryParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
queryParams['{{baseName}}'] = self.apiClient.toPathValue(params['{{paramName}}'])
|
||||
{{/queryParams}}
|
||||
|
||||
{{#headerParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
headerParams['{{baseName}}'] = params['{{paramName}}']
|
||||
{{/headerParams}}
|
||||
|
||||
{{#pathParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
replacement = str(self.apiClient.toPathValue(params['{{paramName}}']))
|
||||
replacement = urllib.quote(replacement)
|
||||
resourcePath = resourcePath.replace('{' + '{{baseName}}' + '}',
|
||||
replacement)
|
||||
{{/pathParams}}
|
||||
|
||||
{{#formParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
{{#notFile}}formParams['{{baseName}}'] = params['{{paramName}}']{{/notFile}}{{#isFile}}files['{{baseName}}'] = params['{{paramName}}']{{/isFile}}
|
||||
{{/formParams}}
|
||||
|
||||
{{#bodyParam}}
|
||||
if ('{{paramName}}' in params):
|
||||
bodyParam = params['{{paramName}}']
|
||||
{{/bodyParam}}
|
||||
|
||||
postData = (formParams if formParams else bodyParam)
|
||||
|
||||
response = self.apiClient.callAPI(resourcePath, method, queryParams,
|
||||
postData, headerParams, files=files)
|
||||
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
|
||||
|
||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||
body=body_params, post_params=form_params, files=files,
|
||||
response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}})
|
||||
{{#returnType}}
|
||||
if not response:
|
||||
return None
|
||||
|
||||
responseObject = self.apiClient.deserialize(response, '{{returnType}}')
|
||||
return responseObject
|
||||
return response
|
||||
{{/returnType}}
|
||||
{{newline}}
|
||||
{{newline}}
|
||||
{{/operation}}
|
||||
{{newline}}
|
||||
{{/operations}}
|
||||
{{newline}}
|
||||
|
||||
@@ -20,24 +20,27 @@ Copyright 2015 Reverb Technologies, Inc.
|
||||
{{#model}}
|
||||
|
||||
class {{classname}}(object):
|
||||
"""NOTE: This class is auto generated by the swagger code generator program.
|
||||
Do not edit the class manually."""
|
||||
"""
|
||||
NOTE: This class is auto generated by the swagger code generator program.
|
||||
Do not edit the class manually.
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Attributes:
|
||||
swaggerTypes (dict): The key is attribute name and the value is attribute type.
|
||||
attributeMap (dict): The key is attribute name and the value is json key in definition.
|
||||
Swagger model
|
||||
|
||||
:param dict swaggerTypes: The key is attribute name and the value is attribute type.
|
||||
:param dict attributeMap: The key is attribute name and the value is json key in definition.
|
||||
"""
|
||||
self.swaggerTypes = {
|
||||
self.swagger_types = {
|
||||
{{#vars}}
|
||||
'{{name}}': '{{{datatype}}}'{{#hasMore}},
|
||||
{{/hasMore}}
|
||||
{{/vars}}{{newline}}
|
||||
}
|
||||
|
||||
self.attributeMap = {
|
||||
self.attribute_map = {
|
||||
{{#vars}}
|
||||
'{{name}}': '{{baseName}}'{{#hasMore}},{{/hasMore}}
|
||||
{{/vars}}
|
||||
|
||||
223
modules/swagger-codegen/src/main/resources/python/rest.mustache
Normal file
223
modules/swagger-codegen/src/main/resources/python/rest.mustache
Normal file
@@ -0,0 +1,223 @@
|
||||
# coding: utf-8
|
||||
import sys
|
||||
import io
|
||||
import json
|
||||
|
||||
# python 2 and python 3 compatibility library
|
||||
from six import iteritems
|
||||
|
||||
try:
|
||||
import urllib3
|
||||
except ImportError:
|
||||
raise ImportError('Swagger python client requires urllib3.')
|
||||
|
||||
try:
|
||||
# for python3
|
||||
from urllib.parse import urlencode
|
||||
except ImportError:
|
||||
# for python2
|
||||
from urllib import urlencode
|
||||
|
||||
|
||||
class RESTResponse(io.IOBase):
|
||||
|
||||
def __init__(self, resp):
|
||||
self.urllib3_response = resp
|
||||
self.status = resp.status
|
||||
self.reason = resp.reason
|
||||
self.data = resp.data
|
||||
|
||||
def getheaders(self):
|
||||
"""
|
||||
Returns a dictionary of the response headers.
|
||||
"""
|
||||
return self.urllib3_response.getheaders()
|
||||
|
||||
def getheader(self, name, default=None):
|
||||
"""
|
||||
Returns a given response header.
|
||||
"""
|
||||
return self.urllib3_response.getheader(name, default)
|
||||
|
||||
class RESTClientObject(object):
|
||||
|
||||
def __init__(self, pools_size=4):
|
||||
self.pool_manager = urllib3.PoolManager(
|
||||
num_pools=pools_size
|
||||
)
|
||||
|
||||
def request(self, method, url, query_params=None, headers=None,
|
||||
body=None, post_params=None):
|
||||
"""
|
||||
:param method: http request method
|
||||
:param url: http request url
|
||||
:param query_params: query parameters in the url
|
||||
:param headers: http request headers
|
||||
:param body: request json body, for `application/json`
|
||||
:param post_params: request post parameters, `application/x-www-form-urlencode`
|
||||
and `multipart/form-data`
|
||||
:param raw_response: if return the raw response
|
||||
"""
|
||||
method = method.upper()
|
||||
assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH']
|
||||
|
||||
if post_params and body:
|
||||
raise ValueError("body parameter cannot be used with post_params parameter.")
|
||||
|
||||
post_params = post_params or {}
|
||||
headers = headers or {}
|
||||
|
||||
if 'Content-Type' not in headers:
|
||||
headers['Content-Type'] = 'application/json'
|
||||
|
||||
# For `POST`, `PUT`, `PATCH`
|
||||
if method in ['POST', 'PUT', 'PATCH']:
|
||||
if query_params:
|
||||
url += '?' + urlencode(query_params)
|
||||
if headers['Content-Type'] == 'application/json':
|
||||
r = self.pool_manager.request(method, url,
|
||||
body=json.dumps(body),
|
||||
headers=headers)
|
||||
if headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
r = self.pool_manager.request(method, url,
|
||||
fields=post_params,
|
||||
encode_multipart=False,
|
||||
headers=headers)
|
||||
if headers['Content-Type'] == 'multipart/form-data':
|
||||
# must del headers['Content-Type'], or the correct Content-Type
|
||||
# which generated by urllib3 will be overwritten.
|
||||
del headers['Content-Type']
|
||||
r = self.pool_manager.request(method, url,
|
||||
fields=post_params,
|
||||
encode_multipart=True,
|
||||
headers=headers)
|
||||
# For `GET`, `HEAD`, `DELETE`
|
||||
else:
|
||||
r = self.pool_manager.request(method, url,
|
||||
fields=query_params,
|
||||
headers=headers)
|
||||
r = RESTResponse(r)
|
||||
|
||||
if r.status not in range(200, 206):
|
||||
raise ErrorResponse(r)
|
||||
|
||||
return self.process_response(r)
|
||||
|
||||
def process_response(self, response):
|
||||
# In the python 3, the response.data is bytes.
|
||||
# we need to decode it to string.
|
||||
if sys.version_info > (3,):
|
||||
data = response.data.decode('utf8')
|
||||
else:
|
||||
data = response.data
|
||||
try:
|
||||
resp = json.loads(data)
|
||||
except ValueError:
|
||||
resp = data
|
||||
|
||||
return resp
|
||||
|
||||
def GET(self, url, headers=None, query_params=None):
|
||||
return self.request("GET", url, headers=headers, query_params=query_params)
|
||||
|
||||
def HEAD(self, url, headers=None, query_params=None):
|
||||
return self.request("HEAD", url, headers=headers, query_params=query_params)
|
||||
|
||||
def DELETE(self, url, headers=None, query_params=None):
|
||||
return self.request("DELETE", url, headers=headers, query_params=query_params)
|
||||
|
||||
def POST(self, url, headers=None, post_params=None, body=None):
|
||||
return self.request("POST", url, headers=headers, post_params=post_params, body=body)
|
||||
|
||||
def PUT(self, url, headers=None, post_params=None, body=None):
|
||||
return self.request("PUT", url, headers=headers, post_params=post_params, body=body)
|
||||
|
||||
def PATCH(self, url, headers=None, post_params=None, body=None):
|
||||
return self.request("PATCH", url, headers=headers, post_params=post_params, body=body)
|
||||
|
||||
|
||||
class ErrorResponse(Exception):
|
||||
"""
|
||||
Non-2xx HTTP response
|
||||
"""
|
||||
|
||||
def __init__(self, http_resp):
|
||||
self.status = http_resp.status
|
||||
self.reason = http_resp.reason
|
||||
self.body = http_resp.data
|
||||
self.headers = http_resp.getheaders()
|
||||
|
||||
# In the python 3, the self.body is bytes.
|
||||
# we need to decode it to string.
|
||||
if sys.version_info > (3,):
|
||||
data = self.body.decode('utf8')
|
||||
else:
|
||||
data = self.body
|
||||
|
||||
try:
|
||||
self.body = json.loads(data)
|
||||
except ValueError:
|
||||
self.body = data
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Custom error response messages
|
||||
"""
|
||||
return "({0})\nReason: {1}\nHeader: {2}\nBody: {3}\n".\
|
||||
format(self.status, self.reason, self.headers, self.body)
|
||||
|
||||
class RESTClient(object):
|
||||
"""
|
||||
A class with all class methods to perform JSON requests.
|
||||
"""
|
||||
|
||||
IMPL = RESTClientObject()
|
||||
|
||||
@classmethod
|
||||
def request(cls, *n, **kw):
|
||||
"""
|
||||
Perform a REST request and parse the response.
|
||||
"""
|
||||
return cls.IMPL.request(*n, **kw)
|
||||
|
||||
@classmethod
|
||||
def GET(cls, *n, **kw):
|
||||
"""
|
||||
Perform a GET request using `RESTClient.request()`.
|
||||
"""
|
||||
return cls.IMPL.GET(*n, **kw)
|
||||
|
||||
@classmethod
|
||||
def HEAD(cls, *n, **kw):
|
||||
"""
|
||||
Perform a HEAD request using `RESTClient.request()`.
|
||||
"""
|
||||
return cls.IMPL.GET(*n, **kw)
|
||||
|
||||
@classmethod
|
||||
def POST(cls, *n, **kw):
|
||||
"""
|
||||
Perform a POST request using `RESTClient.request()`
|
||||
"""
|
||||
return cls.IMPL.POST(*n, **kw)
|
||||
|
||||
@classmethod
|
||||
def PUT(cls, *n, **kw):
|
||||
"""
|
||||
Perform a PUT request using `RESTClient.request()`
|
||||
"""
|
||||
return cls.IMPL.PUT(*n, **kw)
|
||||
|
||||
@classmethod
|
||||
def PATCH(cls, *n, **kw):
|
||||
"""
|
||||
Perform a PATCH request using `RESTClient.request()`
|
||||
"""
|
||||
return cls.IMPL.PATCH(*n, **kw)
|
||||
|
||||
@classmethod
|
||||
def DELETE(cls, *n, **kw):
|
||||
"""
|
||||
Perform a DELETE request using `RESTClient.request()`
|
||||
"""
|
||||
return cls.IMPL.DELETE(*n, **kw)
|
||||
@@ -12,7 +12,7 @@ from setuptools import setup, find_packages
|
||||
# Try reading the setuptools documentation:
|
||||
# http://pypi.python.org/pypi/setuptools
|
||||
|
||||
REQUIRES = []
|
||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9"]
|
||||
|
||||
setup(
|
||||
name="{{module}}",
|
||||
|
||||
@@ -6,118 +6,109 @@ server communication, and is invariant across implementations. Specifics of
|
||||
the methods and models for each application are generated from the Swagger
|
||||
templates."""
|
||||
|
||||
import sys
|
||||
from __future__ import absolute_import
|
||||
from . import models
|
||||
from .rest import RESTClient
|
||||
|
||||
import os
|
||||
import re
|
||||
import urllib
|
||||
import urllib2
|
||||
import httplib
|
||||
import json
|
||||
import datetime
|
||||
import mimetypes
|
||||
import random
|
||||
import string
|
||||
import models
|
||||
|
||||
# python 2 and python 3 compatibility library
|
||||
from six import iteritems
|
||||
|
||||
try:
|
||||
# for python3
|
||||
from urllib.parse import quote
|
||||
except ImportError:
|
||||
# for python2
|
||||
from urllib import quote
|
||||
|
||||
|
||||
class ApiClient(object):
|
||||
"""Generic API client for Swagger client library builds
|
||||
|
||||
Attributes:
|
||||
host: The base path for the server to call
|
||||
headerName: a header to pass when making calls to the API
|
||||
headerValue: a header value to pass when making calls to the API
|
||||
"""
|
||||
def __init__(self, host=None, headerName=None, headerValue=None):
|
||||
self.defaultHeaders = {}
|
||||
if (headerName is not None):
|
||||
self.defaultHeaders[headerName] = headerValue
|
||||
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
|
||||
"""
|
||||
def __init__(self, host=None, header_name=None, header_value=None):
|
||||
self.default_headers = {}
|
||||
if header_name is not None:
|
||||
self.default_headers[header_name] = header_value
|
||||
self.host = host
|
||||
self.cookie = None
|
||||
self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30))
|
||||
# Set default User-Agent.
|
||||
self.user_agent = 'Python-Swagger'
|
||||
|
||||
@property
|
||||
def user_agent(self):
|
||||
return self.defaultHeaders['User-Agent']
|
||||
return self.default_headers['User-Agent']
|
||||
|
||||
@user_agent.setter
|
||||
def user_agent(self, value):
|
||||
self.defaultHeaders['User-Agent'] = value
|
||||
self.default_headers['User-Agent'] = value
|
||||
|
||||
def setDefaultHeader(self, headerName, headerValue):
|
||||
self.defaultHeaders[headerName] = headerValue
|
||||
def set_default_header(self, header_name, header_value):
|
||||
self.default_headers[header_name] = header_value
|
||||
|
||||
def callAPI(self, resourcePath, method, queryParams, postData,
|
||||
headerParams=None, files=None):
|
||||
|
||||
url = self.host + resourcePath
|
||||
|
||||
mergedHeaderParams = self.defaultHeaders.copy()
|
||||
mergedHeaderParams.update(headerParams)
|
||||
headers = {}
|
||||
if mergedHeaderParams:
|
||||
for param, value in mergedHeaderParams.iteritems():
|
||||
headers[param] = ApiClient.sanitizeForSerialization(value)
|
||||
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
|
||||
body=None, post_params=None, files=None, response=None):
|
||||
|
||||
# headers parameters
|
||||
headers = self.default_headers.copy()
|
||||
headers.update(header_params)
|
||||
if self.cookie:
|
||||
headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)
|
||||
headers['Cookie'] = self.cookie
|
||||
if headers:
|
||||
headers = ApiClient.sanitize_for_serialization(headers)
|
||||
|
||||
data = None
|
||||
# path parameters
|
||||
if path_params:
|
||||
path_params = ApiClient.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)
|
||||
|
||||
if queryParams:
|
||||
# Need to remove None values, these should not be sent
|
||||
sentQueryParams = {}
|
||||
for param, value in queryParams.items():
|
||||
if value is not None:
|
||||
sentQueryParams[param] = ApiClient.sanitizeForSerialization(value)
|
||||
url = url + '?' + urllib.urlencode(sentQueryParams)
|
||||
# query parameters
|
||||
if query_params:
|
||||
query_params = ApiClient.sanitize_for_serialization(query_params)
|
||||
query_params = {k: self.to_path_value(v) for k, v in iteritems(query_params)}
|
||||
|
||||
if method in ['GET']:
|
||||
#Options to add statements later on and for compatibility
|
||||
pass
|
||||
# post parameters
|
||||
if post_params:
|
||||
post_params = self.prepare_post_parameters(post_params, files)
|
||||
post_params = ApiClient.sanitize_for_serialization(post_params)
|
||||
|
||||
elif method in ['POST', 'PUT', 'DELETE']:
|
||||
if postData:
|
||||
postData = ApiClient.sanitizeForSerialization(postData)
|
||||
if 'Content-Type' not in headers:
|
||||
headers['Content-Type'] = 'application/json'
|
||||
data = json.dumps(postData)
|
||||
elif headers['Content-Type'] == 'application/json':
|
||||
data = json.dumps(postData)
|
||||
elif headers['Content-Type'] == 'multipart/form-data':
|
||||
data = self.buildMultipartFormData(postData, files)
|
||||
headers['Content-Type'] = 'multipart/form-data; boundary={0}'.format(self.boundary)
|
||||
headers['Content-length'] = str(len(data))
|
||||
else:
|
||||
data = urllib.urlencode(postData)
|
||||
# body
|
||||
if body:
|
||||
body = ApiClient.sanitize_for_serialization(body)
|
||||
|
||||
# request url
|
||||
url = self.host + resource_path
|
||||
|
||||
# perform request and return response
|
||||
response_data = self.request(method, url, query_params=query_params, headers=headers,
|
||||
post_params=post_params, body=body)
|
||||
|
||||
# deserialize response data
|
||||
if response:
|
||||
return self.deserialize(response_data, response)
|
||||
else:
|
||||
raise Exception('Method ' + method + ' is not recognized.')
|
||||
return None
|
||||
|
||||
request = MethodRequest(method=method, url=url, headers=headers,
|
||||
data=data)
|
||||
def to_path_value(self, obj):
|
||||
"""
|
||||
Convert a string or object to a path-friendly value
|
||||
|
||||
:param obj: object or string value
|
||||
|
||||
# Make the request
|
||||
response = urllib2.urlopen(request)
|
||||
if 'Set-Cookie' in response.headers:
|
||||
self.cookie = response.headers['Set-Cookie']
|
||||
string = response.read()
|
||||
|
||||
try:
|
||||
data = json.loads(string)
|
||||
except ValueError: # PUT requests don't return anything
|
||||
data = None
|
||||
|
||||
return data
|
||||
|
||||
def toPathValue(self, obj):
|
||||
"""Convert a string or object to a path-friendly value
|
||||
Args:
|
||||
obj -- object or string value
|
||||
Returns:
|
||||
string -- quoted value
|
||||
:return string: quoted value
|
||||
"""
|
||||
if type(obj) == list:
|
||||
return ','.join(obj)
|
||||
@@ -125,12 +116,12 @@ class ApiClient(object):
|
||||
return str(obj)
|
||||
|
||||
@staticmethod
|
||||
def sanitizeForSerialization(obj):
|
||||
def sanitize_for_serialization(obj):
|
||||
"""
|
||||
Sanitize an object for Request.
|
||||
|
||||
If obj is None, return None.
|
||||
If obj is str, int, long, float, bool, return directly.
|
||||
If obj is str, int, float, bool, return directly.
|
||||
If obj is datetime.datetime, datetime.date convert to string in iso8601 format.
|
||||
If obj is list, santize each element in the list.
|
||||
If obj is dict, return the dict.
|
||||
@@ -138,113 +129,80 @@ class ApiClient(object):
|
||||
"""
|
||||
if isinstance(obj, type(None)):
|
||||
return None
|
||||
elif isinstance(obj, (str, int, long, float, bool, file)):
|
||||
elif isinstance(obj, (str, int, float, bool, tuple)):
|
||||
return obj
|
||||
elif isinstance(obj, list):
|
||||
return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj]
|
||||
return [ApiClient.sanitize_for_serialization(sub_obj) for sub_obj in obj]
|
||||
elif isinstance(obj, (datetime.datetime, datetime.date)):
|
||||
return obj.isoformat()
|
||||
else:
|
||||
if isinstance(obj, dict):
|
||||
objDict = obj
|
||||
obj_dict = obj
|
||||
else:
|
||||
# Convert model obj to dict except attributes `swaggerTypes`, `attributeMap`
|
||||
# Convert model obj to dict except attributes `swagger_types`, `attribute_map`
|
||||
# and attributes which value is not None.
|
||||
# Convert attribute name to json key in model definition for request.
|
||||
objDict = {obj.attributeMap[key]: val
|
||||
for key, val in obj.__dict__.iteritems()
|
||||
if key != 'swaggerTypes' and key != 'attributeMap' and val is not None}
|
||||
return {key: ApiClient.sanitizeForSerialization(val)
|
||||
for (key, val) in objDict.iteritems()}
|
||||
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)
|
||||
for key, val in iteritems(obj_dict)}
|
||||
|
||||
def buildMultipartFormData(self, postData, files):
|
||||
def escape_quotes(s):
|
||||
return s.replace('"', '\\"')
|
||||
def deserialize(self, obj, obj_class):
|
||||
"""
|
||||
Derialize a JSON string into an object.
|
||||
|
||||
lines = []
|
||||
:param obj: string or object to be deserialized
|
||||
:param obj_class: class literal for deserialzied object, or string of class name
|
||||
|
||||
for name, value in postData.items():
|
||||
lines.extend((
|
||||
'--{0}'.format(self.boundary),
|
||||
'Content-Disposition: form-data; name="{0}"'.format(escape_quotes(name)),
|
||||
'',
|
||||
str(value),
|
||||
))
|
||||
|
||||
for name, filepath in files.items():
|
||||
f = open(filepath, 'r')
|
||||
filename = filepath.split('/')[-1]
|
||||
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
||||
lines.extend((
|
||||
'--{0}'.format(self.boundary),
|
||||
'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(escape_quotes(name), escape_quotes(filename)),
|
||||
'Content-Type: {0}'.format(mimetype),
|
||||
'',
|
||||
f.read()
|
||||
))
|
||||
|
||||
lines.extend((
|
||||
'--{0}--'.format(self.boundary),
|
||||
''
|
||||
))
|
||||
return '\r\n'.join(lines)
|
||||
|
||||
def deserialize(self, obj, objClass):
|
||||
"""Derialize a JSON string into an object.
|
||||
|
||||
Args:
|
||||
obj -- string or object to be deserialized
|
||||
objClass -- class literal for deserialzied object, or string
|
||||
of class name
|
||||
Returns:
|
||||
object -- deserialized object"""
|
||||
|
||||
# Have to accept objClass as string or actual type. Type could be a
|
||||
:return object: deserialized object
|
||||
"""
|
||||
# Have to accept obj_class as string or actual type. Type could be a
|
||||
# native Python type, or one of the model classes.
|
||||
if type(objClass) == str:
|
||||
if 'list[' in objClass:
|
||||
match = re.match('list\[(.*)\]', objClass)
|
||||
subClass = match.group(1)
|
||||
return [self.deserialize(subObj, subClass) for subObj in obj]
|
||||
if type(obj_class) == str:
|
||||
if 'list[' in obj_class:
|
||||
match = re.match('list\[(.*)\]', obj_class)
|
||||
sub_class = match.group(1)
|
||||
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
|
||||
|
||||
if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']):
|
||||
objClass = eval(objClass)
|
||||
if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime']:
|
||||
obj_class = eval(obj_class)
|
||||
else: # not a native type, must be model class
|
||||
objClass = eval('models.' + objClass)
|
||||
obj_class = eval('models.' + obj_class)
|
||||
|
||||
if objClass in [int, long, float, dict, list, str, bool]:
|
||||
return objClass(obj)
|
||||
elif objClass == datetime:
|
||||
if obj_class in [int, float, dict, list, str, bool]:
|
||||
return obj_class(obj)
|
||||
elif obj_class == datetime:
|
||||
return self.__parse_string_to_datetime(obj)
|
||||
|
||||
instance = objClass()
|
||||
instance = obj_class()
|
||||
|
||||
for attr, attrType in instance.swaggerTypes.iteritems():
|
||||
if obj is not None and instance.attributeMap[attr] in obj and type(obj) in [list, dict]:
|
||||
value = obj[instance.attributeMap[attr]]
|
||||
if attrType in ['str', 'int', 'long', 'float', 'bool']:
|
||||
attrType = eval(attrType)
|
||||
for attr, attr_type in iteritems(instance.swagger_types):
|
||||
if obj is not None and instance.attribute_map[attr] in obj and type(obj) in [list, dict]:
|
||||
value = obj[instance.attribute_map[attr]]
|
||||
if attr_type in ['str', 'int', 'float', 'bool']:
|
||||
attr_type = eval(attr_type)
|
||||
try:
|
||||
value = attrType(value)
|
||||
value = attr_type(value)
|
||||
except UnicodeEncodeError:
|
||||
value = unicode(value)
|
||||
except TypeError:
|
||||
value = value
|
||||
setattr(instance, attr, value)
|
||||
elif (attrType == 'datetime'):
|
||||
elif attr_type == 'datetime':
|
||||
setattr(instance, attr, self.__parse_string_to_datetime(value))
|
||||
elif 'list[' in attrType:
|
||||
match = re.match('list\[(.*)\]', attrType)
|
||||
subClass = match.group(1)
|
||||
subValues = []
|
||||
elif 'list[' in attr_type:
|
||||
match = re.match('list\[(.*)\]', attr_type)
|
||||
sub_class = match.group(1)
|
||||
sub_values = []
|
||||
if not value:
|
||||
setattr(instance, attr, None)
|
||||
else:
|
||||
for subValue in value:
|
||||
subValues.append(self.deserialize(subValue, subClass))
|
||||
setattr(instance, attr, subValues)
|
||||
for sub_value in value:
|
||||
sub_values.append(self.deserialize(sub_value, sub_class))
|
||||
setattr(instance, attr, sub_values)
|
||||
else:
|
||||
setattr(instance, attr, self.deserialize(value, attrType))
|
||||
setattr(instance, attr, self.deserialize(value, attr_type))
|
||||
|
||||
return instance
|
||||
|
||||
@@ -260,16 +218,42 @@ class ApiClient(object):
|
||||
except ImportError:
|
||||
return string
|
||||
|
||||
class MethodRequest(urllib2.Request):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Construct a MethodRequest. Usage is the same as for
|
||||
`urllib2.Request` except it also takes an optional `method`
|
||||
keyword argument. If supplied, `method` will be used instead of
|
||||
the default."""
|
||||
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
|
||||
"""
|
||||
Perform http request using RESTClient.
|
||||
"""
|
||||
if method == "GET":
|
||||
return RESTClient.GET(url, query_params=query_params, headers=headers)
|
||||
elif method == "HEAD":
|
||||
return RESTClient.HEAD(url, query_params=query_params, headers=headers)
|
||||
elif method == "POST":
|
||||
return RESTClient.POST(url, headers=headers, post_params=post_params, body=body)
|
||||
elif method == "PUT":
|
||||
return RESTClient.PUT(url, headers=headers, post_params=post_params, body=body)
|
||||
elif method == "PATCH":
|
||||
return RESTClient.PATCH(url, headers=headers, post_params=post_params, body=body)
|
||||
elif method == "DELETE":
|
||||
return RESTClient.DELETE(url, query_params=query_params, headers=headers)
|
||||
else:
|
||||
raise ValueError("http method must be `GET`, `HEAD`, `POST`, `PATCH`, `PUT` or `DELETE`")
|
||||
|
||||
def prepare_post_parameters(self, post_params=None, files=None):
|
||||
params = {}
|
||||
|
||||
if post_params:
|
||||
params.update(post_params)
|
||||
|
||||
if files:
|
||||
for k, v in iteritems(files):
|
||||
with open(v, 'rb') as f:
|
||||
filename = os.path.basename(f.name)
|
||||
filedata = f.read()
|
||||
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
||||
params[k] = tuple([filename, filedata, mimetype])
|
||||
|
||||
return params
|
||||
|
||||
|
||||
|
||||
|
||||
if 'method' in kwargs:
|
||||
self.method = kwargs.pop('method')
|
||||
return urllib2.Request.__init__(self, *args, **kwargs)
|
||||
|
||||
def get_method(self):
|
||||
return getattr(self, 'method', urllib2.Request.get_method(self))
|
||||
|
||||
Reference in New Issue
Block a user