Move authentications into ApiClient

This commit is contained in:
xhh
2015-06-05 20:00:32 +08:00
parent 947935f3d9
commit fffc5d7c35
9 changed files with 255 additions and 214 deletions

View File

@@ -1,13 +1,5 @@
package {{invokerPackage}};
import java.util.Map;
import java.util.HashMap;
import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
import {{invokerPackage}}.auth.OAuth;
public class Configuration {
private static ApiClient defaultApiClient = new ApiClient();
@@ -26,64 +18,4 @@ public class Configuration {
public static void setDefaultApiClient(ApiClient apiClient) {
defaultApiClient = apiClient;
}
private static final Map<String, Authentication> AUTH;
static {
// setup authentications
AUTH = new HashMap<String, Authentication>();
{{#authMethods}}
{{#isBasic}}AUTH.put("{{name}}", new HttpBasicAuth());{{/isBasic}}
{{#isApiKey}}AUTH.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}
{{#isOAuth}}AUTH.put("{{name}}", new OAuth());{{/isOAuth}}
{{/authMethods}}
}
public static Authentication getAuthentication(String authName) {
return AUTH.get(authName);
}
/** Set username for the first HTTP basic authentication. */
public static void setUsername(String username) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set password for the first HTTP basic authentication. */
public static void setPassword(String password) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set API key value for the first API key authentication. */
public static void setApiKey(String apiKey) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKey(apiKey);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/** Set API key prefix for the first API key authentication. */
public static void setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
}