fixed content type for java client

This commit is contained in:
Tony Tam 2013-08-08 16:05:05 -07:00
parent 17b63b2be8
commit a3f5c0c688
14 changed files with 398 additions and 304 deletions

View File

@ -1,5 +1,5 @@
/**
* Copyright 2012 Wordnik, Inc.
* Copyright 2013 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -2,14 +2,14 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordnik</groupId>
<artifactId>swagger-client</artifactId>
<artifactId>swagger-petstore</artifactId>
<packaging>jar</packaging>
<name>swagger-client</name>
<version>1.0</version>
<name>swagger-petstore</name>
<version>1.0.0</version>
<scm>
<connection>scm:git:git@github.com:wordnik/swagger-mustache.git</connection>
<developerConnection>scm:git:git@github.com:wordnik/swagger-mustache.git</developerConnection>
<url>https://github.com/wordnik/swagger-mustache</url>
<developerConnection>scm:git:git@github.com:wordnik/swagger-codegen.git</developerConnection>
<url>https://github.com/wordnik/swagger-codegen</url>
</scm>
<prerequisites>
<maven>2.2.0</maven>
@ -107,6 +107,7 @@
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin-version}</version>
<executions>
<execution>
<goals>
@ -207,5 +208,7 @@
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
<scala-test-version>1.6.1</scala-test-version>
<scala-maven-plugin-version>3.1.5</scala-maven-plugin-version>
</properties>
</project>

View File

@ -44,9 +44,8 @@ public class ApiInvoker {
return response;
}
else if(String.class.equals(cls)) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
return json.substring(1, json.length() - 2);
}
else
return json;
}
@ -61,15 +60,17 @@ public class ApiInvoker {
public static String serialize(Object obj) throws ApiException {
try {
if (obj != null) return JsonUtil.getJsonMapper().writeValueAsString(obj);
else return null;
if (obj != null)
return JsonUtil.getJsonMapper().writeValueAsString(obj);
else
return null;
}
catch (Exception e) {
throw new ApiException(500, e.getMessage());
}
}
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams) throws ApiException {
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, String contentType) throws ApiException {
Client client = getClient(host);
StringBuilder b = new StringBuilder();
@ -77,14 +78,16 @@ public class ApiInvoker {
for(String key : queryParams.keySet()) {
String value = queryParams.get(key);
if (value != null){
if(b.toString().length() == 0) b.append("?");
else b.append("&");
if(b.toString().length() == 0)
b.append("?");
else
b.append("&");
b.append(escapeString(key)).append("=").append(escapeString(value));
}
}
String querystring = b.toString();
Builder builder = client.resource(host + path + querystring).type("application/json");
Builder builder = client.resource(host + path + querystring).accept("application/json");
for(String key : headerParams.keySet()) {
builder.header(key, headerParams.get(key));
}
@ -100,13 +103,22 @@ public class ApiInvoker {
response = (ClientResponse) builder.get(ClientResponse.class);
}
else if ("POST".equals(method)) {
if(body == null)
response = builder.post(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").post(ClientResponse.class, serialize(body));
}
else if ("PUT".equals(method)) {
response = builder.put(ClientResponse.class, serialize(body));
}
if(body == null)
response = builder.put(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").put(ClientResponse.class, serialize(body));
}
else if ("DELETE".equals(method)) {
if(body == null)
response = builder.delete(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
}
else {
throw new ApiException(500, "unknown method type " + method);
@ -122,12 +134,12 @@ public class ApiInvoker {
}
private Client getClient(String host) {
if(!hostMap.containsKey(host)) {
Client client = Client.create();
client.addFilter(new LoggingFilter());
hostMap.put(host, client);
}
return hostMap.get(host);
if(!hostMap.containsKey(host)) {
Client client = Client.create();
client.addFilter(new LoggingFilter());
hostMap.put(host, client);
}
return hostMap.get(host);
}
}

View File

@ -6,7 +6,7 @@ import com.wordnik.petstore.model.Pet;
import java.util.*;
public class PetApi {
String basePath = "http://petstore.swagger.wordnik.com/api";
String basePath = "http://hello.com";
ApiInvoker apiInvoker = ApiInvoker.getInstance();
public ApiInvoker getInvoker() {
@ -21,9 +21,137 @@ public class PetApi {
return basePath;
}
public Pet getPetById (String petId) throws ApiException {
public List<Pet> findPetsByTags (String tags) throws ApiException {
// create path and map variables
String path = "/pet.{format}/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
String path = "/pet/findByTags".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(tags == null ) {
throw new ApiException(400, "missing required params");
}
if(!"null".equals(String.valueOf(tags)))
queryParams.put("tags", String.valueOf(tags));
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
public List<Pet> findPetsByStatus (String status) throws ApiException {
// create path and map variables
String path = "/pet/findByStatus".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(status == null ) {
throw new ApiException(400, "missing required params");
}
if(!"null".equals(String.valueOf(status)))
queryParams.put("status", String.valueOf(status));
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
public void updatePet (Pet body) throws ApiException {
// create path and map variables
String path = "/pet".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public void addPet (Pet body) throws ApiException {
// create path and map variables
String path = "/pet".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public void deletePet (String petId) throws ApiException {
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -33,8 +161,41 @@ public class PetApi {
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public Pet getPetById (String petId) throws ApiException {
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
}
@ -50,125 +211,5 @@ public class PetApi {
}
}
}
public void addPet (Pet body) throws ApiException {
// create path and map variables
String path = "/pet.{format}".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public void updatePet (Pet body) throws ApiException {
// create path and map variables
String path = "/pet.{format}".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public List<Pet> findPetsByStatus (String status) throws ApiException {
// create path and map variables
String path = "/pet.{format}/findByStatus".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(status == null ) {
throw new ApiException(400, "missing required params");
}
if(!"null".equals(String.valueOf(status)))
queryParams.put("status", String.valueOf(status));
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
public List<Pet> findPetsByTags (String tags) throws ApiException {
// create path and map variables
String path = "/pet.{format}/findByTags".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
// verify required params are set
if(tags == null ) {
throw new ApiException(400, "missing required params");
}
if(!"null".equals(String.valueOf(tags)))
queryParams.put("tags", String.valueOf(tags));
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
}

View File

@ -6,7 +6,7 @@ import com.wordnik.petstore.model.Order;
import java.util.*;
public class StoreApi {
String basePath = "http://petstore.swagger.wordnik.com/api";
String basePath = "http://hello.com";
ApiInvoker apiInvoker = ApiInvoker.getInstance();
public ApiInvoker getInvoker() {
@ -23,7 +23,7 @@ public class StoreApi {
public Order getOrderById (String orderId) throws ApiException {
// create path and map variables
String path = "/store.{format}/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString()));
String path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -33,8 +33,10 @@ public class StoreApi {
if(orderId == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return (Order) ApiInvoker.deserialize(response, "", Order.class);
}
@ -52,7 +54,7 @@ public class StoreApi {
}
public void deleteOrder (String orderId) throws ApiException {
// create path and map variables
String path = "/store.{format}/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString()));
String path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -62,8 +64,10 @@ public class StoreApi {
if(orderId == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType);
if(response != null){
return ;
}
@ -81,7 +85,7 @@ public class StoreApi {
}
public void placeOrder (Order body) throws ApiException {
// create path and map variables
String path = "/store.{format}/order".replaceAll("\\{format\\}","json");
String path = "/store/order".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -91,8 +95,10 @@ public class StoreApi {
if(body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}

View File

@ -6,7 +6,7 @@ import com.wordnik.petstore.model.User;
import java.util.*;
public class UserApi {
String basePath = "http://petstore.swagger.wordnik.com/api";
String basePath = "http://hello.com";
ApiInvoker apiInvoker = ApiInvoker.getInstance();
public ApiInvoker getInvoker() {
@ -21,9 +21,9 @@ public class UserApi {
return basePath;
}
public void createUsersWithArrayInput (List<User> body) throws ApiException {
public void createUser (User body) throws ApiException {
// create path and map variables
String path = "/user.{format}/createWithArray".replaceAll("\\{format\\}","json");
String path = "/user".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -33,8 +33,10 @@ public class UserApi {
if(body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
@ -50,9 +52,9 @@ public class UserApi {
}
}
}
public void createUser (User body) throws ApiException {
public void createUsersWithArrayInput (List<User> body) throws ApiException {
// create path and map variables
String path = "/user.{format}".replaceAll("\\{format\\}","json");
String path = "/user/createWithArray".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -62,8 +64,10 @@ public class UserApi {
if(body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
@ -81,7 +85,7 @@ public class UserApi {
}
public void createUsersWithListInput (List<User> body) throws ApiException {
// create path and map variables
String path = "/user.{format}/createWithList".replaceAll("\\{format\\}","json");
String path = "/user/createWithList".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -91,8 +95,10 @@ public class UserApi {
if(body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
@ -110,7 +116,7 @@ public class UserApi {
}
public void updateUser (String username, User body) throws ApiException {
// create path and map variables
String path = "/user.{format}/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString()));
String path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -120,8 +126,10 @@ public class UserApi {
if(username == null || body == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
@ -139,7 +147,7 @@ public class UserApi {
}
public void deleteUser (String username) throws ApiException {
// create path and map variables
String path = "/user.{format}/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString()));
String path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -149,8 +157,10 @@ public class UserApi {
if(username == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType);
if(response != null){
return ;
}
@ -168,7 +178,7 @@ public class UserApi {
}
public User getUserByName (String username) throws ApiException {
// create path and map variables
String path = "/user.{format}/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString()));
String path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}", apiInvoker.escapeString(username.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -178,8 +188,10 @@ public class UserApi {
if(username == null ) {
throw new ApiException(400, "missing required params");
}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return (User) ApiInvoker.deserialize(response, "", User.class);
}
@ -197,7 +209,7 @@ public class UserApi {
}
public String loginUser (String username, String password) throws ApiException {
// create path and map variables
String path = "/user.{format}/login".replaceAll("\\{format\\}","json");
String path = "/user/login".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
@ -211,8 +223,10 @@ public class UserApi {
queryParams.put("username", String.valueOf(username));
if(!"null".equals(String.valueOf(password)))
queryParams.put("password", String.valueOf(password));
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return (String) ApiInvoker.deserialize(response, "", String.class);
}
@ -230,14 +244,16 @@ public class UserApi {
}
public void logoutUser () throws ApiException {
// create path and map variables
String path = "/user.{format}/logout".replaceAll("\\{format\\}","json");
String path = "/user/logout".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
if(response != null){
return ;
}

View File

@ -1,15 +1,8 @@
package com.wordnik.petstore.model;
public class Category {
private Long id = null;
private String name = null;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private Long id = null;
public String getName() {
return name;
}
@ -17,12 +10,19 @@ public class Category {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Category {\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" name: ").append(name).append("\n");
sb.append(" id: ").append(id).append("\n");
sb.append("}\n");
return sb.toString();
}

View File

@ -2,31 +2,17 @@ package com.wordnik.petstore.model;
import java.util.Date;
public class Order {
private Long id = null;
private Date shipDate = null;
private Integer quantity = null;
private Long petId = null;
private Long id = null;
/* Order Status */
private String status = null;
private Integer quantity = null;
private Date shipDate = null;
public Long getId() {
return id;
public Date getShipDate() {
return shipDate;
}
public void setId(Long id) {
this.id = id;
}
public Long getPetId() {
return petId;
}
public void setPetId(Long petId) {
this.petId = petId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
public void setShipDate(Date shipDate) {
this.shipDate = shipDate;
}
public Integer getQuantity() {
@ -36,22 +22,36 @@ public class Order {
this.quantity = quantity;
}
public Date getShipDate() {
return shipDate;
public Long getPetId() {
return petId;
}
public void setShipDate(Date shipDate) {
this.shipDate = shipDate;
public void setPetId(Long petId) {
this.petId = petId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Order {\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" petId: ").append(petId).append("\n");
sb.append(" status: ").append(status).append("\n");
sb.append(" quantity: ").append(quantity).append("\n");
sb.append(" shipDate: ").append(shipDate).append("\n");
sb.append(" quantity: ").append(quantity).append("\n");
sb.append(" petId: ").append(petId).append("\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" status: ").append(status).append("\n");
sb.append("}\n");
return sb.toString();
}

View File

@ -4,41 +4,13 @@ import java.util.*;
import com.wordnik.petstore.model.Category;
import com.wordnik.petstore.model.Tag;
public class Pet {
private String name = null;
private List<Tag> tags = new ArrayList<Tag>();
private List<String> photoUrls = new ArrayList<String>();
private Long id = null;
private Category category = null;
/* pet status in the store */
private String status = null;
private String name = null;
private List<String> photoUrls = new ArrayList<String>();
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private Category category = null;
public String getName() {
return name;
}
@ -46,6 +18,13 @@ public class Pet {
this.name = name;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public List<String> getPhotoUrls() {
return photoUrls;
}
@ -53,16 +32,37 @@ public class Pet {
this.photoUrls = photoUrls;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Pet {\n");
sb.append(" tags: ").append(tags).append("\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" category: ").append(category).append("\n");
sb.append(" status: ").append(status).append("\n");
sb.append(" name: ").append(name).append("\n");
sb.append(" tags: ").append(tags).append("\n");
sb.append(" photoUrls: ").append(photoUrls).append("\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" status: ").append(status).append("\n");
sb.append(" category: ").append(category).append("\n");
sb.append("}\n");
return sb.toString();
}

View File

@ -1,15 +1,8 @@
package com.wordnik.petstore.model;
public class Tag {
private Long id = null;
private String name = null;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private Long id = null;
public String getName() {
return name;
}
@ -17,12 +10,19 @@ public class Tag {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Tag {\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" name: ").append(name).append("\n");
sb.append(" id: ").append(id).append("\n");
sb.append("}\n");
return sb.toString();
}

View File

@ -1,34 +1,20 @@
package com.wordnik.petstore.model;
public class User {
private Long id = null;
private String lastName = null;
private String phone = null;
private String username = null;
private String email = null;
private String username = null;
/* User Status */
private Integer userStatus = null;
private String lastName = null;
private String firstName = null;
private Long id = null;
private String phone = null;
private String password = null;
public Long getId() {
return id;
public String getEmail() {
return email;
}
public void setId(Long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
@ -38,13 +24,6 @@ public class User {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getUserStatus() {
return userStatus;
}
@ -52,6 +31,13 @@ public class User {
this.userStatus = userStatus;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
@ -59,6 +45,20 @@ public class User {
this.firstName = firstName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
@ -70,13 +70,13 @@ public class User {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class User {\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" lastName: ").append(lastName).append("\n");
sb.append(" phone: ").append(phone).append("\n");
sb.append(" username: ").append(username).append("\n");
sb.append(" email: ").append(email).append("\n");
sb.append(" username: ").append(username).append("\n");
sb.append(" userStatus: ").append(userStatus).append("\n");
sb.append(" lastName: ").append(lastName).append("\n");
sb.append(" firstName: ").append(firstName).append("\n");
sb.append(" id: ").append(id).append("\n");
sb.append(" phone: ").append(phone).append("\n");
sb.append(" password: ").append(password).append("\n");
sb.append("}\n");
return sb.toString();

View File

@ -47,8 +47,10 @@ public class {{classname}} {
{{#headerParams}}headerParams.put("{{paramName}}", {{paramName}});
{{/headerParams}}
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams);
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, contentType);
if(response != null){
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
}

View File

@ -44,9 +44,8 @@ public class ApiInvoker {
return response;
}
else if(String.class.equals(cls)) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
return json.substring(1, json.length() - 2);
}
else
return json;
}
@ -61,15 +60,17 @@ public class ApiInvoker {
public static String serialize(Object obj) throws ApiException {
try {
if (obj != null) return JsonUtil.getJsonMapper().writeValueAsString(obj);
else return null;
if (obj != null)
return JsonUtil.getJsonMapper().writeValueAsString(obj);
else
return null;
}
catch (Exception e) {
throw new ApiException(500, e.getMessage());
}
}
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams) throws ApiException {
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, String contentType) throws ApiException {
Client client = getClient(host);
StringBuilder b = new StringBuilder();
@ -77,14 +78,16 @@ public class ApiInvoker {
for(String key : queryParams.keySet()) {
String value = queryParams.get(key);
if (value != null){
if(b.toString().length() == 0) b.append("?");
else b.append("&");
if(b.toString().length() == 0)
b.append("?");
else
b.append("&");
b.append(escapeString(key)).append("=").append(escapeString(value));
}
}
String querystring = b.toString();
Builder builder = client.resource(host + path + querystring).type("application/json");
Builder builder = client.resource(host + path + querystring).accept("application/json");
for(String key : headerParams.keySet()) {
builder.header(key, headerParams.get(key));
}
@ -100,13 +103,22 @@ public class ApiInvoker {
response = (ClientResponse) builder.get(ClientResponse.class);
}
else if ("POST".equals(method)) {
if(body == null)
response = builder.post(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").post(ClientResponse.class, serialize(body));
}
else if ("PUT".equals(method)) {
response = builder.put(ClientResponse.class, serialize(body));
}
if(body == null)
response = builder.put(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").put(ClientResponse.class, serialize(body));
}
else if ("DELETE".equals(method)) {
if(body == null)
response = builder.delete(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
}
else {
throw new ApiException(500, "unknown method type " + method);
@ -122,11 +134,11 @@ public class ApiInvoker {
}
private Client getClient(String host) {
if(!hostMap.containsKey(host)) {
Client client = Client.create();
client.addFilter(new LoggingFilter());
hostMap.put(host, client);
}
return hostMap.get(host);
if(!hostMap.containsKey(host)) {
Client client = Client.create();
client.addFilter(new LoggingFilter());
hostMap.put(host, client);
}
return hostMap.get(host);
}
}

View File

@ -107,6 +107,7 @@
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin-version}</version>
<executions>
<execution>
<goals>
@ -207,5 +208,6 @@
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
<scala-test-version>1.6.1</scala-test-version>
<scala-maven-plugin-version>3.1.5</scala-maven-plugin-version>
</properties>
</project>