added readme, rebuilt client

This commit is contained in:
Tony Tam 2015-02-15 19:50:41 -08:00
parent e943f5c864
commit 4daac48c6b
2 changed files with 202 additions and 206 deletions

View File

@ -20,27 +20,31 @@ from models import *
class ApiClient: class ApiClient:
"""Generic API client for Swagger client library builds""" """Generic API client for Swagger client library builds
def __init__(self, apiKey=None, apiServer=None): Attributes:
if apiKey == None: host: The base path for the server to call
raise Exception('You must pass an apiKey when instantiating the ' headerName: a header to pass when making calls to the API
'APIClient') headerValue: a header value to pass when making calls to the API
self.apiKey = apiKey """
self.apiServer = apiServer def __init__(self, host=None, headerName=None, headerValue=None):
self.headerName = headerName
self.headerValue = headerValue
self.host = host
self.cookie = None self.cookie = None
self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30)) self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30))
def callAPI(self, resourcePath, method, queryParams, postData, def callAPI(self, resourcePath, method, queryParams, postData,
headerParams=None, files=None): headerParams=None, files=None):
url = self.apiServer + resourcePath url = self.host + resourcePath
headers = {} headers = {}
if headerParams: if headerParams:
for param, value in headerParams.iteritems(): for param, value in headerParams.iteritems():
headers[param] = value headers[param] = value
headers['api_key'] = self.apiKey if self.headerName:
headers[self.headerName] = self.headerValue
if self.cookie: if self.cookie:
headers['Cookie'] = self.cookie headers['Cookie'] = self.cookie
@ -56,12 +60,10 @@ class ApiClient:
url = url + '?' + urllib.urlencode(sentQueryParams) url = url + '?' + urllib.urlencode(sentQueryParams)
if method in ['GET']: if method in ['GET']:
#Options to add statements later on and for compatibility #Options to add statements later on and for compatibility
pass pass
elif method in ['POST', 'PUT', 'DELETE']: elif method in ['POST', 'PUT', 'DELETE']:
if postData: if postData:
postData = self.sanitizeForSerialization(postData) postData = self.sanitizeForSerialization(postData)
if 'Content-type' not in headers: if 'Content-type' not in headers:
@ -195,8 +197,7 @@ class ApiClient:
# Server will always return a time stamp in UTC, but with # Server will always return a time stamp in UTC, but with
# trailing +0000 indicating no offset from UTC. So don't process # trailing +0000 indicating no offset from UTC. So don't process
# last 5 characters. # last 5 characters.
return datetime.datetime.strptime(obj[:-5], return datetime.datetime.strptime(obj[:-5], "%Y-%m-%dT%H:%M:%S.%f")
"%Y-%m-%dT%H:%M:%S.%f")
instance = objClass() instance = objClass()
@ -213,8 +214,7 @@ class ApiClient:
value = value value = value
setattr(instance, attr, value) setattr(instance, attr, value)
elif (attrType == 'datetime'): elif (attrType == 'datetime'):
setattr(instance, attr, datetime.datetime.strptime(value[:-5], setattr(instance, attr, datetime.datetime.strptime(value[:-5], "%Y-%m-%dT%H:%M:%S.%f"))
"%Y-%m-%dT%H:%M:%S.%f"))
elif 'list[' in attrType: elif 'list[' in attrType:
match = re.match('list\[(.*)\]', attrType) match = re.match('list\[(.*)\]', attrType)
subClass = match.group(1) subClass = match.group(1)
@ -223,18 +223,15 @@ class ApiClient:
setattr(instance, attr, None) setattr(instance, attr, None)
else: else:
for subValue in value: for subValue in value:
subValues.append(self.deserialize(subValue, subValues.append(self.deserialize(subValue, subClass))
subClass))
setattr(instance, attr, subValues) setattr(instance, attr, subValues)
else: else:
setattr(instance, attr, self.deserialize(value, setattr(instance, attr, self.deserialize(value, objClass))
objClass))
return instance return instance
class MethodRequest(urllib2.Request): class MethodRequest(urllib2.Request):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
"""Construct a MethodRequest. Usage is the same as for """Construct a MethodRequest. Usage is the same as for
`urllib2.Request` except it also takes an optional `method` `urllib2.Request` except it also takes an optional `method`
@ -247,4 +244,3 @@ class MethodRequest(urllib2.Request):
def get_method(self): def get_method(self):
return getattr(self, 'method', urllib2.Request.get_method(self)) return getattr(self, 'method', urllib2.Request.get_method(self))