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

@ -17,6 +17,7 @@ import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.HashMap; import java.util.HashMap;
@ -34,6 +35,9 @@ import java.text.SimpleDateFormat;
import java.text.ParseException; import java.text.ParseException;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
import {{invokerPackage}}.auth.OAuth;
public class ApiClient { public class ApiClient {
private Map<String, Client> hostMap = new HashMap<String, Client>(); private Map<String, Client> hostMap = new HashMap<String, Client>();
@ -41,6 +45,8 @@ public class ApiClient {
private boolean debugging = false; private boolean debugging = false;
private String basePath = "{{basePath}}"; private String basePath = "{{basePath}}";
private Map<String, Authentication> authentications;
private DateFormat dateFormat; private DateFormat dateFormat;
public ApiClient() { public ApiClient() {
@ -53,6 +59,14 @@ public class ApiClient {
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
} }
public String getBasePath() { public String getBasePath() {
@ -64,6 +78,75 @@ public class ApiClient {
return this; return this;
} }
/**
* Get authentications (key: authentication name, value: authentication).
*/
public Map<String, Authentication> 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 username for the first HTTP basic authentication.
*/
public void setUsername(String username) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* Helper method to set password for the first HTTP basic authentication.
*/
public void setPassword(String password) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* 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!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */
@ -340,8 +423,8 @@ public class ApiClient {
*/ */
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) { private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
for (String authName : authNames) { for (String authName : authNames) {
Authentication auth = Configuration.getAuthentication(authName); Authentication auth = authentications.get(authName);
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName); if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
auth.applyToParams(queryParams, headerParams); auth.applyToParams(queryParams, headerParams);
} }
} }

View File

@ -1,13 +1,5 @@
package {{invokerPackage}}; 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 { public class Configuration {
private static ApiClient defaultApiClient = new ApiClient(); private static ApiClient defaultApiClient = new ApiClient();
@ -26,64 +18,4 @@ public class Configuration {
public static void setDefaultApiClient(ApiClient apiClient) { public static void setDefaultApiClient(ApiClient apiClient) {
defaultApiClient = 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!");
}
} }

View File

@ -17,6 +17,7 @@ import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.HashMap; import java.util.HashMap;
@ -34,6 +35,9 @@ import java.text.SimpleDateFormat;
import java.text.ParseException; import java.text.ParseException;
import io.swagger.client.auth.Authentication; import io.swagger.client.auth.Authentication;
import io.swagger.client.auth.HttpBasicAuth;
import io.swagger.client.auth.ApiKeyAuth;
import io.swagger.client.auth.OAuth;
public class ApiClient { public class ApiClient {
private Map<String, Client> hostMap = new HashMap<String, Client>(); private Map<String, Client> hostMap = new HashMap<String, Client>();
@ -41,6 +45,8 @@ public class ApiClient {
private boolean debugging = false; private boolean debugging = false;
private String basePath = "http://petstore.swagger.io/v2"; private String basePath = "http://petstore.swagger.io/v2";
private Map<String, Authentication> authentications;
private DateFormat dateFormat; private DateFormat dateFormat;
public ApiClient() { public ApiClient() {
@ -53,6 +59,13 @@ public class ApiClient {
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
authentications.put("petstore_auth", new OAuth());
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
} }
public String getBasePath() { public String getBasePath() {
@ -64,6 +77,75 @@ public class ApiClient {
return this; return this;
} }
/**
* Get authentications (key: authentication name, value: authentication).
*/
public Map<String, Authentication> 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 username for the first HTTP basic authentication.
*/
public void setUsername(String username) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* Helper method to set password for the first HTTP basic authentication.
*/
public void setPassword(String password) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* 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!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */
@ -340,8 +422,8 @@ public class ApiClient {
*/ */
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) { private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
for (String authName : authNames) { for (String authName : authNames) {
Authentication auth = Configuration.getAuthentication(authName); Authentication auth = authentications.get(authName);
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName); if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
auth.applyToParams(queryParams, headerParams); auth.applyToParams(queryParams, headerParams);
} }
} }

View File

@ -1,13 +1,5 @@
package io.swagger.client; 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;
import io.swagger.client.auth.OAuth;
public class Configuration { public class Configuration {
private static ApiClient defaultApiClient = new ApiClient(); private static ApiClient defaultApiClient = new ApiClient();
@ -26,68 +18,4 @@ public class Configuration {
public static void setDefaultApiClient(ApiClient apiClient) { public static void setDefaultApiClient(ApiClient apiClient) {
defaultApiClient = apiClient; defaultApiClient = apiClient;
} }
private static final Map<String, Authentication> AUTH;
static {
// setup authentications
AUTH = new HashMap<String, Authentication>();
AUTH.put("api_key", new ApiKeyAuth("header", "api_key"));
AUTH.put("petstore_auth", new OAuth());
}
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!");
}
} }

View File

@ -0,0 +1,75 @@
package io.swagger.client;
import io.swagger.client.auth.*;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.*;
public class ApiClientTest {
ApiClient apiClient = null;
@Before
public void setup() {
apiClient = new ApiClient();
}
@Test
public void testGetAuthentications() {
Map<String, Authentication> auths = apiClient.getAuthentications();
Authentication auth = auths.get("api_key");
assertNotNull(auth);
assertTrue(auth instanceof ApiKeyAuth);
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth;
assertEquals("header", apiKeyAuth.getLocation());
assertEquals("api_key", apiKeyAuth.getParamName());
auth = auths.get("petstore_auth");
assertTrue(auth instanceof OAuth);
assertSame(auth, apiClient.getAuthentication("petstore_auth"));
assertNull(auths.get("unknown"));
try {
auths.put("my_auth", new HttpBasicAuth());
fail("the authentications returned should not be modifiable");
} catch (UnsupportedOperationException e) {
}
}
@Test
public void testSetUsername() {
try {
apiClient.setUsername("my-username");
fail("there should be no HTTP basic authentications");
} catch (RuntimeException e) {
}
}
@Test
public void testSetPassword() {
try {
apiClient.setPassword("my-password");
fail("there should be no HTTP basic authentications");
} catch (RuntimeException e) {
}
}
@Test
public void testSetApiKeyAndPrefix() {
ApiKeyAuth auth = (ApiKeyAuth) apiClient.getAuthentications().get("api_key");
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
apiClient.setApiKey("my-api-key");
apiClient.setApiKeyPrefix("Token");
assertEquals("my-api-key", auth.getApiKey());
assertEquals("Token", auth.getApiKeyPrefix());
// reset values
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
}
}

View File

@ -1,59 +1,9 @@
package io.swagger.client; package io.swagger.client;
import io.swagger.client.auth.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.*; import org.junit.*;
public class ConfigurationTest { public class ConfigurationTest {
@Test
public void testGetAuthentication() {
Authentication auth = Configuration.getAuthentication("api_key");
assertNotNull(auth);
assertTrue(auth instanceof ApiKeyAuth);
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth;
assertEquals("header", apiKeyAuth.getLocation());
assertEquals("api_key", apiKeyAuth.getParamName());
auth = Configuration.getAuthentication("petstore_auth");
assertTrue(auth instanceof OAuth);
assertNull(Configuration.getAuthentication("unknown"));
}
@Test
public void testSetUsername() {
try {
Configuration.setUsername("my-username");
fail("should throw RuntimeException");
} catch (RuntimeException e) {
}
}
@Test
public void testSetPassword() {
try {
Configuration.setPassword("my-password");
fail("should throw RuntimeException");
} catch (RuntimeException e) {
}
}
@Test
public void testSetApiKeyAndPrefix() {
ApiKeyAuth auth = (ApiKeyAuth) Configuration.getAuthentication("api_key");
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
Configuration.setApiKey("my-api-key");
Configuration.setApiKeyPrefix("Token");
assertEquals("my-api-key", auth.getApiKey());
assertEquals("Token", auth.getApiKeyPrefix());
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
}
@Test @Test
public void testDefaultApiClient() { public void testDefaultApiClient() {
ApiClient apiClient = Configuration.getDefaultApiClient(); ApiClient apiClient = Configuration.getDefaultApiClient();

View File

@ -16,15 +16,12 @@ import org.junit.*;
public class PetApiTest { public class PetApiTest {
PetApi api = null; PetApi api = null;
@BeforeClass
public static void initAuth() {
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) Configuration.getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
}
@Before @Before
public void setup() { public void setup() {
api = new PetApi(); api = new PetApi();
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
} }
@Test @Test

View File

@ -15,15 +15,12 @@ import org.junit.*;
public class StoreApiTest { public class StoreApiTest {
StoreApi api = null; StoreApi api = null;
@BeforeClass
public static void initAuth() {
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) Configuration.getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
}
@Before @Before
public void setup() { public void setup() {
api = new StoreApi(); api = new StoreApi();
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
} }
@Test @Test

View File

@ -15,15 +15,12 @@ import org.junit.*;
public class UserApiTest { public class UserApiTest {
UserApi api = null; UserApi api = null;
@BeforeClass
public static void initAuth() {
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) Configuration.getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
}
@Before @Before
public void setup() { public void setup() {
api = new UserApi(); api = new UserApi();
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
} }
@Test @Test