added readme, rebuilt client

This commit is contained in:
Tony Tam 2015-02-15 19:50:23 -08:00
parent 719820c089
commit e943f5c864
8 changed files with 259 additions and 210 deletions

View File

@ -26,6 +26,8 @@ cd $APP_DIR
./bin/java-petstore-filemap.sh ./bin/java-petstore-filemap.sh
./bin/java-petstore.sh ./bin/java-petstore.sh
./bin/java-wordnik-api.sh ./bin/java-wordnik-api.sh
./bin/php-petstore.sh
./bin/python-petstore.sh
./bin/objc-petstore.sh ./bin/objc-petstore.sh
./bin/objc-wordnik-api.sh ./bin/objc-wordnik-api.sh
./bin/tizen-petstore.sh ./bin/tizen-petstore.sh

View File

@ -31,6 +31,6 @@ fi
# if you've executed sbt assembly previously it will use that instead. # if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ -i http://petstore.swagger.io/v2/swagger.json -l python -o samples/client/petstore/python -t modules/swagger-codegen/src/main/resources/python" ags="$@ -i http://petstore.swagger.io/v2/swagger.json -l python -o samples/client/petstore/python"
java $JAVA_OPTS -jar $executable $ags java $JAVA_OPTS -jar $executable $ags

View File

@ -62,6 +62,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
typeMapping.put("date", "datetime"); typeMapping.put("date", "datetime");
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("swagger.mustache", module, "swagger.py")); supportingFiles.add(new SupportingFile("swagger.mustache", module, "swagger.py"));
supportingFiles.add(new SupportingFile("__init__.mustache", module, "__init__.py")); supportingFiles.add(new SupportingFile("__init__.mustache", module, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__.mustache", modelPackage.replaceAll("\\.", File.separator), "__init__.py")); supportingFiles.add(new SupportingFile("__init__.mustache", modelPackage.replaceAll("\\.", File.separator), "__init__.py"));

View File

@ -0,0 +1,25 @@
# Swagger Generated Python client
Usage example, based on the swagger petstore:
```python
# include the client module
from client import *
# build a client connection. In this example, we are passing the hostname as arg0, and
# sending a header with name `api_key` and value `special-key` on each call to the api.
client = swagger.ApiClient('http://petstore.swagger.io/v2', 'api_key', 'special-key')
# create the PetApi class with the client we just created
petApi = PetApi.PetApi(client)
# call the API and fetch a pet, with petId=3
pet = petApi.getPetById(petId=3)
# write it into pretty JSON
json = client.sanitizeForSerialization(pet)
print json
{'category': {'category': None, 'status': None, 'name': 'string', 'tags': None, 'photoUrls': None, 'id': 0L}, 'status': {'category': None, 'status': None, 'name': None, 'tags': None, 'photoUrls': None, 'id': None}, 'name': 'foogly', 'tags': [{'id': 0L, 'name': 'string'}], 'photoUrls': ['string'], 'id': 3L}
```

View File

@ -0,0 +1,25 @@
# Swagger Generated Python client
Usage example, based on the swagger petstore:
```python
# include the client module
from client import *
# build a client connection. In this example, we are passing the hostname as arg0, and
# sending a header with name `api_key` and value `special-key` on each call to the api.
client = swagger.ApiClient('http://petstore.swagger.io/v2', 'api_key', 'special-key')
# create the PetApi class with the client we just created
petApi = PetApi.PetApi(client)
# call the API and fetch a pet, with petId=3
pet = petApi.getPetById(petId=3)
# write it into pretty JSON
json = client.sanitizeForSerialization(pet)
print json
{'category': {'category': None, 'status': None, 'name': 'string', 'tags': None, 'photoUrls': None, 'id': 0L}, 'status': {'category': None, 'status': None, 'name': None, 'tags': None, 'photoUrls': None, 'id': None}, 'name': 'foogly', 'tags': [{'id': 0L, 'name': 'string'}], 'photoUrls': ['string'], 'id': 3L}
```

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
"""Wordnik.com's Swagger generic API client. This client handles the client- """Swagger generic API client. This client handles the client-
server communication, and is invariant across implementations. Specifics of server communication, and is invariant across implementations. Specifics of
the methods and models for each application are generated from the Swagger the methods and models for each application are generated from the Swagger
templates.""" templates."""
@ -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))