diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java b/samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java index ab6f551327f..a667ebf2c65 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java @@ -31,6 +31,8 @@ import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.text.ParseException; +import io.swagger.client.auth.Authentication; + public class ApiInvoker { private static ApiInvoker INSTANCE = new ApiInvoker(); private Map hostMap = new HashMap(); @@ -162,11 +164,12 @@ public class ApiInvoker { } } - public String invokeAPI(String host, String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String contentType) throws ApiException { + public String invokeAPI(String host, String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String contentType, String[] authNames) throws ApiException { + processAuthParams(authNames, queryParams, headerParams); + Client client = getClient(host); StringBuilder b = new StringBuilder(); - for(String key : queryParams.keySet()) { String value = queryParams.get(key); if (value != null){ @@ -267,6 +270,14 @@ public class ApiInvoker { } } + private void processAuthParams(String[] authNames, Map queryParams, Map 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) { if(!hostMap.containsKey(host)) { Client client = Client.create(); diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java b/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java new file mode 100644 index 00000000000..366fd5256a4 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java @@ -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 AUTH; + + static { + AUTH = new HashMap(); + + + AUTH.put("api_key", new ApiKeyAuth("header", "api_key")); + + + + + // TODO: support oauth + + } + + public static Authentication getAuthentication(String authName) { + return AUTH.get(authName); + } +} diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java b/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java index 10326d8a2d6..e99bcfafc08 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java @@ -74,7 +74,8 @@ public class PetApi { } 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){ return ; } @@ -124,7 +125,8 @@ public class PetApi { } 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){ return ; } @@ -176,7 +178,8 @@ public class PetApi { } 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){ return (List) ApiInvoker.deserialize(response, "array", Pet.class); } @@ -228,7 +231,8 @@ public class PetApi { } 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){ return (List) ApiInvoker.deserialize(response, "array", Pet.class); } @@ -284,7 +288,8 @@ public class PetApi { } 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){ return (Pet) ApiInvoker.deserialize(response, "", Pet.class); } @@ -350,7 +355,8 @@ public class PetApi { } 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){ return ; } @@ -408,7 +414,8 @@ public class PetApi { } 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){ return ; } @@ -475,7 +482,8 @@ public class PetApi { } 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){ return ; } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java b/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java index 6da7b5abb2f..dd3629ab1b7 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java @@ -73,7 +73,8 @@ public class StoreApi { } 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){ return (Map) ApiInvoker.deserialize(response, "map", Map.class); } @@ -123,7 +124,8 @@ public class StoreApi { } 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){ return (Order) ApiInvoker.deserialize(response, "", Order.class); } @@ -179,7 +181,8 @@ public class StoreApi { } 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){ return (Order) ApiInvoker.deserialize(response, "", Order.class); } @@ -235,7 +238,8 @@ public class StoreApi { } 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){ return ; } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java b/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java index d0974d95a52..b627d2c70e9 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java @@ -74,7 +74,8 @@ public class UserApi { } 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){ return ; } @@ -124,7 +125,8 @@ public class UserApi { } 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){ return ; } @@ -174,7 +176,8 @@ public class UserApi { } 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){ return ; } @@ -229,7 +232,8 @@ public class UserApi { } 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){ return (String) ApiInvoker.deserialize(response, "", String.class); } @@ -278,7 +282,8 @@ public class UserApi { } 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){ return ; } @@ -334,7 +339,8 @@ public class UserApi { } 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){ return (User) ApiInvoker.deserialize(response, "", User.class); } @@ -391,7 +397,8 @@ public class UserApi { } 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){ return ; } @@ -447,7 +454,8 @@ public class UserApi { } 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){ return ; } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/auth/ApiKeyAuth.java b/samples/client/petstore/java/src/main/java/io/swagger/client/auth/ApiKeyAuth.java new file mode 100644 index 00000000000..1f84fd12685 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/auth/ApiKeyAuth.java @@ -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 queryParams, Map 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); + } + } +} diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/auth/Authentication.java b/samples/client/petstore/java/src/main/java/io/swagger/client/auth/Authentication.java new file mode 100644 index 00000000000..50ed42e15f2 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/auth/Authentication.java @@ -0,0 +1,7 @@ +package io.swagger.client.auth; + +import java.util.Map; + +public interface Authentication { + void processParams(Map queryParams, Map headerParams); +} diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/auth/HttpBasicAuth.java b/samples/client/petstore/java/src/main/java/io/swagger/client/auth/HttpBasicAuth.java new file mode 100644 index 00000000000..b4a1266a1b4 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/auth/HttpBasicAuth.java @@ -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 queryParams, Map headerParams) { + try { + headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"))); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } +}