Instantiate HttpBearerToken authentications if so declared and add helper methods. (#2485)

This commit is contained in:
davidwcarlson 2019-03-26 23:07:43 -07:00 committed by William Cheng
parent 1c1c1ef9c3
commit 7ab73ff587
19 changed files with 300 additions and 21 deletions

View File

@ -281,6 +281,7 @@ public class ApiClient {
} }
{{/hasOAuthMethods}} {{/hasOAuthMethods}}
/** /**
* Helper method to set access token for the first Bearer authentication. * Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token * @param bearerToken Bearer token

View File

@ -57,10 +57,11 @@ public class ApiClient {
{{#hasAuthMethods}} {{#hasAuthMethods}}
RequestInterceptor auth; RequestInterceptor auth;
{{#authMethods}}if ("{{name}}".equals(authName)) { {{#authMethods}}if ("{{name}}".equals(authName)) {
{{#isBasic}} {{#isBasic}}{{#isBasicBasic}}
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
{{/isBasic}} {{/isBasicBasic}}{{^isBasicBasic}}
{{#isApiKey}} auth = new HttpBearerAuth("{{scheme}}");
{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"); auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
{{/isApiKey}} {{/isApiKey}}
{{#isOAuth}} {{#isOAuth}}
@ -225,6 +226,21 @@ public class ApiClient {
return contentTypes[0]; return contentTypes[0];
} }
/**
* Helper method to configure the bearer token.
* @param bearerToken the bearer token.
*/
public void setBearerToken(String bearerToken) {
for(RequestInterceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to configure the first api key found * Helper method to configure the first api key found
* @param apiKey API key * @param apiKey API key

View File

@ -66,6 +66,7 @@ import java.util.TimeZone;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
@ -125,8 +126,9 @@ public class ApiClient {
setUserAgent("Java-SDK"); setUserAgent("Java-SDK");
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}} authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}");{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified. // Prevent the authentications from being modified.
@ -185,6 +187,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set token for HTTP bearer authentication.
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* @param username the username * @param username the username

View File

@ -33,6 +33,7 @@ import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
@ -57,9 +58,11 @@ public class ApiClient {
{{#hasAuthMethods}} {{#hasAuthMethods}}
Interceptor auth; Interceptor auth;
{{#authMethods}}if ("{{name}}".equals(authName)) { {{#authMethods}}if ("{{name}}".equals(authName)) {
{{#isBasic}} {{#isBasic}}{{#isBasicBasic}}
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
{{/isBasic}} {{/isBasicBasic}}{{^isBasicBasic}}
auth = new HttpBearerAuth("{{scheme}}");
{{/isBasicBasic}}{{/isBasic}}
{{#isApiKey}} {{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}} auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}
{{#isOAuth}} {{#isOAuth}}
@ -159,6 +162,19 @@ public class ApiClient {
} }
} }
/**
* Helper method to set token for the first Http Bearer authentication found.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
for (Interceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return;
}
}
}
/** /**
* Helper method to configure the username/password for basic auth or password oauth * Helper method to configure the username/password for basic auth or password oauth
* @param username Username * @param username Username

View File

@ -26,6 +26,7 @@ import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
@ -62,9 +63,11 @@ public class ApiClient {
{{#hasAuthMethods}} {{#hasAuthMethods}}
Interceptor auth; Interceptor auth;
{{#authMethods}}if ("{{name}}".equals(authName)) { {{#authMethods}}if ("{{name}}".equals(authName)) {
{{#isBasic}} {{#isBasic}}{{#isBasicBasic}}
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
{{/isBasic}} {{/isBasicBasic}}{{^isBasicBasic}}
auth = new HttpBearerAuth("{{scheme}}");
{{/isBasicBasic}}{{/isBasic}}
{{#isApiKey}} {{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"); auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
{{/isApiKey}} {{/isApiKey}}
@ -209,6 +212,21 @@ public class ApiClient {
return this; return this;
} }
/**
* Helper method to set token for the first Http Bearer authentication found.
* @param bearerToken Bearer token
* @return ApiClient
*/
public ApiClient setBearerToken(String bearerToken) {
for (Interceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return this;
}
}
return this;
}
/** /**
* Helper method to configure the username/password for basic auth or password oauth * Helper method to configure the username/password for basic auth or password oauth
* @param username Username * @param username Username

View File

@ -40,8 +40,9 @@ public class ApiClient {
public ApiClient() { public ApiClient() {
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<{{#supportJava6}}String, Authentication{{/supportJava6}}>();{{#authMethods}}{{#isBasic}} authentications = new HashMap<{{#supportJava6}}String, Authentication{{/supportJava6}}>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
// authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} // authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
// authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}");{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
// authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} // authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified. // Prevent the authentications from being modified.

View File

@ -2,6 +2,7 @@ package {{invokerPackage}};
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
@ -78,8 +79,9 @@ public class ApiClient {
this.objectMapper.setDateFormat(dateFormat); this.objectMapper.setDateFormat(dateFormat);
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
this.authentications = new HashMap<>();{{#authMethods}}{{#isBasic}} this.authentications = new HashMap<>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}");{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified. // Prevent the authentications from being modified.
@ -161,6 +163,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public ApiClient setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return this;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* *

View File

@ -59,6 +59,7 @@ import java.util.TimeZone;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
@ -122,8 +123,9 @@ public class ApiClient {
protected void init() { protected void init() {
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}} authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}");{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified. // Prevent the authentications from being modified.
@ -182,6 +184,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* @param username the username * @param username the username

View File

@ -44,10 +44,13 @@ public class ApiClient {
for(String authName : authNames) { for(String authName : authNames) {
RequestInterceptor auth; RequestInterceptor auth;
if ("api_key".equals(authName)) { if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key"); auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) { } else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query"); auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) { } else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
} else if ("petstore_auth".equals(authName)) { } else if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets"); auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
@ -196,6 +199,21 @@ public class ApiClient {
return contentTypes[0]; return contentTypes[0];
} }
/**
* Helper method to configure the bearer token.
* @param bearerToken the bearer token.
*/
public void setBearerToken(String bearerToken) {
for(RequestInterceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to configure the first api key found * Helper method to configure the first api key found
* @param apiKey API key * @param apiKey API key

View File

@ -44,10 +44,13 @@ public class ApiClient {
for(String authName : authNames) { for(String authName : authNames) {
RequestInterceptor auth; RequestInterceptor auth;
if ("api_key".equals(authName)) { if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key"); auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) { } else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query"); auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) { } else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
} else if ("petstore_auth".equals(authName)) { } else if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets"); auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
@ -196,6 +199,21 @@ public class ApiClient {
return contentTypes[0]; return contentTypes[0];
} }
/**
* Helper method to configure the bearer token.
* @param bearerToken the bearer token.
*/
public void setBearerToken(String bearerToken) {
for(RequestInterceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to configure the first api key found * Helper method to configure the first api key found
* @param apiKey API key * @param apiKey API key

View File

@ -270,6 +270,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
/** /**
* Helper method to set access token for the first Bearer authentication. * Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token * @param bearerToken Bearer token

View File

@ -60,6 +60,7 @@ import java.util.TimeZone;
import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.Authentication;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
@ -178,6 +179,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set token for HTTP bearer authentication.
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* @param username the username * @param username the username

View File

@ -55,6 +55,7 @@ import java.util.TimeZone;
import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.Authentication;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
@ -173,6 +174,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set token for HTTP bearer authentication.
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* @param username the username * @param username the username

View File

@ -33,6 +33,7 @@ import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.OAuth.AccessTokenListener; import org.openapitools.client.auth.OAuth.AccessTokenListener;
@ -54,14 +55,19 @@ public class ApiClient {
for(String authName : authNames) { for(String authName : authNames) {
Interceptor auth; Interceptor auth;
if ("api_key".equals(authName)) { if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key"); auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) { } else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query"); auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) { } else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
} else if ("petstore_auth".equals(authName)) { } else if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets"); auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else { } else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names"); throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
@ -151,6 +157,19 @@ public class ApiClient {
} }
} }
/**
* Helper method to set token for the first Http Bearer authentication found.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
for (Interceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return;
}
}
}
/** /**
* Helper method to configure the username/password for basic auth or password oauth * Helper method to configure the username/password for basic auth or password oauth
* @param username Username * @param username Username

View File

@ -15,6 +15,7 @@ import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.OAuth.AccessTokenListener; import org.openapitools.client.auth.OAuth.AccessTokenListener;
@ -45,12 +46,17 @@ public class ApiClient {
for(String authName : authNames) { for(String authName : authNames) {
Interceptor auth; Interceptor auth;
if ("api_key".equals(authName)) { if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key"); auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) { } else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query"); auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) { } else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
} else if ("petstore_auth".equals(authName)) { } else if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets"); auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else { } else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names"); throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
@ -165,6 +171,21 @@ public class ApiClient {
return this; return this;
} }
/**
* Helper method to set token for the first Http Bearer authentication found.
* @param bearerToken Bearer token
* @return ApiClient
*/
public ApiClient setBearerToken(String bearerToken) {
for (Interceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return this;
}
}
return this;
}
/** /**
* Helper method to configure the username/password for basic auth or password oauth * Helper method to configure the username/password for basic auth or password oauth
* @param username Username * @param username Username

View File

@ -16,6 +16,7 @@ import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.OAuth.AccessTokenListener; import org.openapitools.client.auth.OAuth.AccessTokenListener;
@ -46,12 +47,17 @@ public class ApiClient {
for(String authName : authNames) { for(String authName : authNames) {
Interceptor auth; Interceptor auth;
if ("api_key".equals(authName)) { if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key"); auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) { } else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query"); auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) { } else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
} else if ("petstore_auth".equals(authName)) { } else if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets"); auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else { } else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names"); throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
@ -167,6 +173,21 @@ public class ApiClient {
return this; return this;
} }
/**
* Helper method to set token for the first Http Bearer authentication found.
* @param bearerToken Bearer token
* @return ApiClient
*/
public ApiClient setBearerToken(String bearerToken) {
for (Interceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return this;
}
}
return this;
}
/** /**
* Helper method to configure the username/password for basic auth or password oauth * Helper method to configure the username/password for basic auth or password oauth
* @param username Username * @param username Username

View File

@ -16,6 +16,7 @@ import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.OAuth.AccessTokenListener; import org.openapitools.client.auth.OAuth.AccessTokenListener;
@ -46,12 +47,17 @@ public class ApiClient {
for(String authName : authNames) { for(String authName : authNames) {
Interceptor auth; Interceptor auth;
if ("api_key".equals(authName)) { if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key"); auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) { } else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query"); auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) { } else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth(); auth = new HttpBasicAuth();
} else if ("petstore_auth".equals(authName)) { } else if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets"); auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else { } else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names"); throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
@ -168,6 +174,21 @@ public class ApiClient {
return this; return this;
} }
/**
* Helper method to set token for the first Http Bearer authentication found.
* @param bearerToken Bearer token
* @return ApiClient
*/
public ApiClient setBearerToken(String bearerToken) {
for (Interceptor apiAuthorization : apiAuthorizations.values()) {
if (apiAuthorization instanceof HttpBearerAuth) {
((HttpBearerAuth) apiAuthorization).setBearerToken(bearerToken);
return this;
}
}
return this;
}
/** /**
* Helper method to configure the username/password for basic auth or password oauth * Helper method to configure the username/password for basic auth or password oauth
* @param username Username * @param username Username

View File

@ -2,6 +2,7 @@ package org.openapitools.client;
import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.Authentication;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
@ -160,6 +161,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public ApiClient setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return this;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* *

View File

@ -59,6 +59,7 @@ import java.util.TimeZone;
import org.openapitools.client.auth.Authentication; import org.openapitools.client.auth.Authentication;
import org.openapitools.client.auth.HttpBasicAuth; import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.ApiKeyAuth; import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth; import org.openapitools.client.auth.OAuth;
@ -181,6 +182,20 @@ public class ApiClient {
return authentications.get(authName); return authentications.get(authName);
} }
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/** /**
* Helper method to set username for the first HTTP basic authentication. * Helper method to set username for the first HTTP basic authentication.
* @param username the username * @param username the username