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