diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java index 27b730aed771..5b20019aa79d 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java @@ -26,6 +26,7 @@ public class AndroidVolleyClientCodegen extends DefaultCodegen implements Codege protected String sourceFolder = projectFolder + "/java"; protected Boolean useAndroidMavenGradlePlugin = true; protected String requestPackage = "io.swagger.client.request"; + protected String authPackage = "io.swagger.client.auth"; public AndroidVolleyClientCodegen() { super(); @@ -251,6 +252,10 @@ public class AndroidVolleyClientCodegen extends DefaultCodegen implements Codege (sourceFolder + File.separator + requestPackage).replace(".", java.io.File.separator), "GetRequest.java")); supportingFiles.add(new SupportingFile("request/postrequest.mustache", (sourceFolder + File.separator + requestPackage).replace(".", java.io.File.separator), "PostRequest.java")); + supportingFiles.add(new SupportingFile("auth/apikeyauth.mustache", + (sourceFolder + File.separator + authPackage).replace(".", java.io.File.separator), "ApiKeyAuth.java")); + supportingFiles.add(new SupportingFile("auth/authentication.mustache", + (sourceFolder + File.separator + authPackage).replace(".", java.io.File.separator), "Authentication.java")); } public Boolean getUseAndroidMavenGradlePlugin() { diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache index 037ef58ab90e..265c74cb76f9 100644 --- a/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache @@ -5,7 +5,6 @@ import {{invokerPackage}}.Pair; import java.util.Map; import java.util.List; -{{>generatedAnnotation}} public interface Authentication { /** Apply authentication settings to header and query params. */ void applyToParams(List queryParams, Map headerParams); diff --git a/modules/swagger-codegen/src/main/resources/android-volley/api.mustache b/modules/swagger-codegen/src/main/resources/android-volley/api.mustache index fe1b0f20c0b9..b7bb96df0b1b 100644 --- a/modules/swagger-codegen/src/main/resources/android-volley/api.mustache +++ b/modules/swagger-codegen/src/main/resources/android-volley/api.mustache @@ -52,7 +52,8 @@ public class {{classname}} { */ public void {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}},{{/allParams}} final {{#returnBaseType}}Responses.{{returnBaseType}}{{#isListContainer}}List{{/isListContainer}}Response{{/returnBaseType}}{{^returnBaseType}}Response.Listener{{/returnBaseType}} responseListener, final Response.ErrorListener errorListener) { Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; - {{#allParams}}{{#required}} + + {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set if ({{paramName}} == null) { VolleyError error = new VolleyError("Missing the required parameter '{{paramName}}' when calling {{nickname}}", @@ -104,8 +105,10 @@ public class {{classname}} { {{/formParams}} } + String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; + try { - apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, + apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, authNames, new Response.Listener() { @Override public void onResponse(String response) { diff --git a/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache index ce96d62cf9f9..73cf7fbf4126 100644 --- a/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache +++ b/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache @@ -22,12 +22,17 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; +import {{invokerPackage}}.auth.Authentication; +import {{invokerPackage}}.auth.ApiKeyAuth; + public class ApiInvoker { private static ApiInvoker INSTANCE; private Map defaultHeaderMap = new HashMap(); - Context context; - RequestQueue mRequestQueue; + private Context context; + private RequestQueue mRequestQueue; + + private Map authentications; /** Content type "text/plain" with UTF-8 encoding. */ public static final ContentType TEXT_PLAIN_UTF8 = ContentType.create("text/plain", Consts.UTF_8); @@ -156,6 +161,16 @@ public class ApiInvoker { public static void initializeInstance(Context context) { INSTANCE = new ApiInvoker(context); setUserAgent("Android-Volley-Swagger"); + + // Setup authentications (key: authentication name, value: authentication). + INSTANCE.authentications = new HashMap(); + {{#authMethods}} + {{#isApiKey}} + INSTANCE.authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}")); + {{/isApiKey}} + {{/authMethods}} + // Prevent the authentications from being modified. + INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications); } private ApiInvoker(Context context) { this.context = context; @@ -210,9 +225,69 @@ public class ApiInvoker { } } - public void invokeAPI(String host, String path, String method, List queryParams, Object body, Map headerParams, Map formParams, String contentType, Response.Listener stringRequest, Response.ErrorListener errorListener) throws ApiException { + /** + * Get authentications (key: authentication name, value: authentication). + */ + public Map getAuthentications() { + return authentications; + } + + /** + * Get authentication for the given name. + * + * @param authName The authentication name + * @return The authentication, null if not found + */ + public Authentication getAuthentication(String authName) { + return authentications.get(authName); + } + + + /** + * Helper method to set API key value for the first API key authentication. + */ + public void setApiKey(String apiKey) { + for (Authentication auth : authentications.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKey(apiKey); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } + + /** + * Helper method to set API key prefix for the first API key authentication. + */ + public void setApiKeyPrefix(String apiKeyPrefix) { + for (Authentication auth : authentications.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } + + /** + * Update query and header parameters based on authentication settings. + * + * @param authNames The authentications to apply + */ + private void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { + for (String authName : authNames) { + Authentication auth = authentications.get(authName); + if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); + auth.applyToParams(queryParams, headerParams); + } + } + + public void invokeAPI(String host, String path, String method, List queryParams, Object body, Map headerParams, Map formParams, String contentType, String[] authNames, Response.Listener stringRequest, Response.ErrorListener errorListener) throws ApiException { StringBuilder b = new StringBuilder(); b.append("?"); + + updateParamsForAuth(authNames, queryParams, headerParams); + if (queryParams != null){ for (Pair queryParam : queryParams){ if (!queryParam.getName().isEmpty()) { diff --git a/modules/swagger-codegen/src/main/resources/android-volley/auth/apikeyauth.mustache b/modules/swagger-codegen/src/main/resources/android-volley/auth/apikeyauth.mustache new file mode 100644 index 000000000000..a1824b551ca9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/android-volley/auth/apikeyauth.mustache @@ -0,0 +1,58 @@ +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; + +import java.util.Map; +import java.util.List; + +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 applyToParams(List queryParams, Map headerParams) { + String value; + if (apiKeyPrefix != null) { + value = apiKeyPrefix + " " + apiKey; + } else { + value = apiKey; + } + if (location == "query") { + queryParams.add(new Pair(paramName, value)); + } else if (location == "header") { + headerParams.put(paramName, value); + } + } +} diff --git a/modules/swagger-codegen/src/main/resources/android-volley/auth/authentication.mustache b/modules/swagger-codegen/src/main/resources/android-volley/auth/authentication.mustache new file mode 100644 index 000000000000..265c74cb76f9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/android-volley/auth/authentication.mustache @@ -0,0 +1,11 @@ +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; + +import java.util.Map; +import java.util.List; + +public interface Authentication { + /** Apply authentication settings to header and query params. */ + void applyToParams(List queryParams, Map headerParams); +}