Regenerate Java Petstore sample

This commit is contained in:
xhh 2015-05-21 22:09:08 +08:00
parent 3d4b5a10c9
commit f616605e7e
8 changed files with 180 additions and 22 deletions

View File

@ -31,6 +31,8 @@ import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.text.ParseException; import java.text.ParseException;
import io.swagger.client.auth.Authentication;
public class ApiInvoker { public class ApiInvoker {
private static ApiInvoker INSTANCE = new ApiInvoker(); private static ApiInvoker INSTANCE = new ApiInvoker();
private Map<String, Client> hostMap = new HashMap<String, Client>(); private Map<String, Client> hostMap = new HashMap<String, Client>();
@ -162,11 +164,12 @@ public class ApiInvoker {
} }
} }
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType) throws ApiException { public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException {
processAuthParams(authNames, queryParams, headerParams);
Client client = getClient(host); Client client = getClient(host);
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
for(String key : queryParams.keySet()) { for(String key : queryParams.keySet()) {
String value = queryParams.get(key); String value = queryParams.get(key);
if (value != null){ if (value != null){
@ -267,6 +270,14 @@ public class ApiInvoker {
} }
} }
private void processAuthParams(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
for(String authName : authNames) {
Authentication auth = Configuration.getAuthentication(authName);
if(auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
auth.processParams(queryParams, headerParams);
}
}
private Client getClient(String host) { private Client getClient(String host) {
if(!hostMap.containsKey(host)) { if(!hostMap.containsKey(host)) {
Client client = Client.create(); Client client = Client.create();

View File

@ -0,0 +1,29 @@
package io.swagger.client;
import java.util.Map;
import java.util.HashMap;
import io.swagger.client.auth.Authentication;
import io.swagger.client.auth.HttpBasicAuth;
import io.swagger.client.auth.ApiKeyAuth;
public class Configuration {
private static final Map<String, Authentication> AUTH;
static {
AUTH = new HashMap<String, Authentication>();
AUTH.put("api_key", new ApiKeyAuth("header", "api_key"));
// TODO: support oauth
}
public static Authentication getAuthentication(String authName) {
return AUTH.get(authName);
}
}

View File

@ -74,7 +74,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -124,7 +125,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -176,7 +178,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "array", Pet.class); return (List<Pet>) ApiInvoker.deserialize(response, "array", Pet.class);
} }
@ -228,7 +231,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "array", Pet.class); return (List<Pet>) ApiInvoker.deserialize(response, "array", Pet.class);
} }
@ -284,7 +288,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "api_key", "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (Pet) ApiInvoker.deserialize(response, "", Pet.class); return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
} }
@ -350,7 +355,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -408,7 +414,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -475,7 +482,8 @@ public class PetApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "petstore_auth" };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }

View File

@ -73,7 +73,8 @@ public class StoreApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { "api_key" };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (Map<String, Integer>) ApiInvoker.deserialize(response, "map", Map.class); return (Map<String, Integer>) ApiInvoker.deserialize(response, "map", Map.class);
} }
@ -123,7 +124,8 @@ public class StoreApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (Order) ApiInvoker.deserialize(response, "", Order.class); return (Order) ApiInvoker.deserialize(response, "", Order.class);
} }
@ -179,7 +181,8 @@ public class StoreApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (Order) ApiInvoker.deserialize(response, "", Order.class); return (Order) ApiInvoker.deserialize(response, "", Order.class);
} }
@ -235,7 +238,8 @@ public class StoreApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }

View File

@ -74,7 +74,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -124,7 +125,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -174,7 +176,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -229,7 +232,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (String) ApiInvoker.deserialize(response, "", String.class); return (String) ApiInvoker.deserialize(response, "", String.class);
} }
@ -278,7 +282,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -334,7 +339,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return (User) ApiInvoker.deserialize(response, "", User.class); return (User) ApiInvoker.deserialize(response, "", User.class);
} }
@ -391,7 +397,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }
@ -447,7 +454,8 @@ public class UserApi {
} }
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType); String[] authNames = new String[] { };
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames);
if(response != null){ if(response != null){
return ; return ;
} }

View File

@ -0,0 +1,55 @@
package io.swagger.client.auth;
import java.util.Map;
public class ApiKeyAuth implements Authentication {
private final String location;
private final String paramName;
private String apiKey;
private String apiKeyPrefix;
public ApiKeyAuth(String location, String paramName) {
this.location = location;
this.paramName = paramName;
}
public String getLocation() {
return location;
}
public String getParamName() {
return paramName;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getApiKeyPrefix() {
return apiKeyPrefix;
}
public void setApiKeyPrefix(String apiKeyPrefix) {
this.apiKeyPrefix = apiKeyPrefix;
}
@Override
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
String value;
if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey;
} else {
value = apiKey;
}
if (location == "query") {
queryParams.put(paramName, value);
} else if (location == "header") {
headerParams.put(paramName, value);
}
}
}

View File

@ -0,0 +1,7 @@
package io.swagger.client.auth;
import java.util.Map;
public interface Authentication {
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
}

View File

@ -0,0 +1,36 @@
package io.swagger.client.auth;
import java.util.Map;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.DatatypeConverter;
public class HttpBasicAuth implements Authentication {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
try {
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}