forked from loafle/openapi-generator-original
fix retrofit2 indentation (#5221)
This commit is contained in:
+379
-373
@@ -22,8 +22,12 @@ import java.time.format.DateTimeFormatter;
|
||||
{{/java8}}
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.Retrofit;
|
||||
{{#useRxJava}}import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;{{/useRxJava}}
|
||||
{{#useRxJava2}}import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;{{/useRxJava2}}
|
||||
{{#useRxJava}}
|
||||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
||||
{{/useRxJava}}
|
||||
{{#useRxJava2}}
|
||||
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
{{/useRxJava2}}
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||
|
||||
@@ -38,292 +42,293 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
|
||||
import {{invokerPackage}}.auth.HttpBasicAuth;
|
||||
import {{invokerPackage}}.auth.ApiKeyAuth;
|
||||
import {{invokerPackage}}.auth.OAuth;
|
||||
import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
|
||||
import {{invokerPackage}}.auth.OAuthFlow;
|
||||
|
||||
|
||||
public class ApiClient {
|
||||
|
||||
private Map<String, Interceptor> apiAuthorizations;
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
private Retrofit.Builder adapterBuilder;
|
||||
private Map<String, Interceptor> apiAuthorizations;
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
private Retrofit.Builder adapterBuilder;
|
||||
|
||||
public ApiClient() {
|
||||
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
|
||||
createDefaultAdapter();
|
||||
public ApiClient() {
|
||||
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
|
||||
createDefaultAdapter();
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
{{#hasAuthMethods}}
|
||||
Interceptor auth;
|
||||
{{#authMethods}}if ("{{name}}".equals(authName)) {
|
||||
{{#isBasic}}
|
||||
auth = new HttpBasicAuth();
|
||||
{{/isBasic}}
|
||||
{{#isApiKey}}
|
||||
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
|
||||
{{/isApiKey}}
|
||||
{{#isOAuth}}
|
||||
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{#hasMore}}, {{/hasMore}}{{/scopes}}");
|
||||
{{/isOAuth}}
|
||||
} else {{/authMethods}}{
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
{{/hasAuthMethods}}
|
||||
{{^hasAuthMethods}}
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
{{/hasAuthMethods}}
|
||||
}
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
{{#hasAuthMethods}}
|
||||
Interceptor auth;
|
||||
{{#authMethods}}if ("{{name}}".equals(authName)) {
|
||||
{{#isBasic}}
|
||||
auth = new HttpBasicAuth();
|
||||
{{/isBasic}}
|
||||
{{#isApiKey}}
|
||||
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
|
||||
{{/isApiKey}}
|
||||
{{#isOAuth}}
|
||||
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{#hasMore}}, {{/hasMore}}{{/scopes}}");
|
||||
{{/isOAuth}}
|
||||
} else {{/authMethods}}{
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
{{/hasAuthMethods}}
|
||||
{{^hasAuthMethods}}
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
{{/hasAuthMethods}}
|
||||
}
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName Authentication name
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName Authentication name
|
||||
* @param apiKey API key
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param clientId Client ID
|
||||
* @param secret Client Secret
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String clientId, String secret, String username, String password) {
|
||||
this(authName);
|
||||
this.getTokenEndPoint()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(secret)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
}
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
{{^java8}}
|
||||
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
|
||||
{{/java8}}
|
||||
{{#java8}}
|
||||
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter())
|
||||
{{/java8}}
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
|
||||
.create();
|
||||
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "{{{basePath}}}";
|
||||
if(!baseUrl.endsWith("/"))
|
||||
baseUrl = baseUrl + "/";
|
||||
|
||||
adapterBuilder = new Retrofit
|
||||
.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
{{#useRxJava}}
|
||||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
||||
{{/useRxJava}}
|
||||
{{#useRxJava2}}
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
{{/useRxJava2}}
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(GsonCustomConverterFactory.create(gson));
|
||||
}
|
||||
|
||||
public <S> S createService(Class<S> serviceClass) {
|
||||
return adapterBuilder
|
||||
.client(okBuilder.build())
|
||||
.build()
|
||||
.create(serviceClass);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
*/
|
||||
private void setApiKey(String apiKey) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName Authentication name
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password oauth
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
private void setCredentials(String username, String password) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName Authentication name
|
||||
* @param apiKey API key
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
/**
|
||||
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Token request builder
|
||||
*/
|
||||
public TokenRequestBuilder getTokenEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getTokenRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
/**
|
||||
* Helper method to configure authorization endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Authentication request builder
|
||||
*/
|
||||
public AuthenticationRequestBuilder getAuthorizationEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getAuthenticationRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param clientId Client ID
|
||||
* @param secret Client Secret
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String clientId, String secret, String username, String password) {
|
||||
this(authName);
|
||||
this.getTokenEndPoint()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(secret)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
/**
|
||||
* Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @param accessToken Access token
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.setAccessToken(accessToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
{{^java8}}
|
||||
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
|
||||
{{/java8}}
|
||||
{{#java8}}
|
||||
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter())
|
||||
{{/java8}}
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
|
||||
.create();
|
||||
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "{{{basePath}}}";
|
||||
if(!baseUrl.endsWith("/"))
|
||||
baseUrl = baseUrl + "/";
|
||||
|
||||
adapterBuilder = new Retrofit
|
||||
.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
{{#useRxJava}}.addCallAdapterFactory(RxJavaCallAdapterFactory.create()){{/useRxJava}}
|
||||
{{#useRxJava2}}.addCallAdapterFactory(RxJava2CallAdapterFactory.create()){{/useRxJava2}}
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(GsonCustomConverterFactory.create(gson));
|
||||
/**
|
||||
* Helper method to configure the oauth accessCode/implicit flow parameters
|
||||
* @param clientId Client ID
|
||||
* @param clientSecret Client secret
|
||||
* @param redirectURI Redirect URI
|
||||
*/
|
||||
public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setRedirectURI(redirectURI);
|
||||
oauth.getAuthenticationRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setRedirectURI(redirectURI);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public <S> S createService(Class<S> serviceClass) {
|
||||
return adapterBuilder
|
||||
.client(okBuilder.build())
|
||||
.build()
|
||||
.create(serviceClass);
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener Access token listener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
*/
|
||||
private void setApiKey(String apiKey) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okBuilder.addInterceptor(authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password oauth
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
private void setCredentials(String username, String password) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, Interceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public Retrofit.Builder getAdapterBuilder() {
|
||||
return adapterBuilder;
|
||||
}
|
||||
|
||||
public void setAdapterBuilder(Retrofit.Builder adapterBuilder) {
|
||||
this.adapterBuilder = adapterBuilder;
|
||||
}
|
||||
|
||||
public OkHttpClient.Builder getOkBuilder() {
|
||||
return okBuilder;
|
||||
}
|
||||
|
||||
public void addAuthsToOkBuilder(OkHttpClient.Builder okBuilder) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
okBuilder.addInterceptor(apiAuthorization);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Token request builder
|
||||
*/
|
||||
public TokenRequestBuilder getTokenEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getTokenRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure authorization endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Authentication request builder
|
||||
*/
|
||||
public AuthenticationRequestBuilder getAuthorizationEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getAuthenticationRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @param accessToken Access token
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.setAccessToken(accessToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the oauth accessCode/implicit flow parameters
|
||||
* @param clientId Client ID
|
||||
* @param clientSecret Client secret
|
||||
* @param redirectURI Redirect URI
|
||||
*/
|
||||
public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setRedirectURI(redirectURI);
|
||||
oauth.getAuthenticationRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setRedirectURI(redirectURI);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener Access token listener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okBuilder.addInterceptor(authorization);
|
||||
}
|
||||
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, Interceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public Retrofit.Builder getAdapterBuilder() {
|
||||
return adapterBuilder;
|
||||
}
|
||||
|
||||
public void setAdapterBuilder(Retrofit.Builder adapterBuilder) {
|
||||
this.adapterBuilder = adapterBuilder;
|
||||
}
|
||||
|
||||
public OkHttpClient.Builder getOkBuilder() {
|
||||
return okBuilder;
|
||||
}
|
||||
|
||||
public void addAuthsToOkBuilder(OkHttpClient.Builder okBuilder) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
okBuilder.addInterceptor(apiAuthorization);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the okBuilder given in parameter, adds the auth interceptors and uses it to configure the Retrofit
|
||||
* @param okClient An instance of OK HTTP client
|
||||
*/
|
||||
public void configureFromOkclient(OkHttpClient okClient) {
|
||||
this.okBuilder = okClient.newBuilder();
|
||||
addAuthsToOkBuilder(this.okBuilder);
|
||||
|
||||
}
|
||||
/**
|
||||
* Clones the okBuilder given in parameter, adds the auth interceptors and uses it to configure the Retrofit
|
||||
* @param okClient An instance of OK HTTP client
|
||||
*/
|
||||
public void configureFromOkclient(OkHttpClient okClient) {
|
||||
this.okBuilder = okClient.newBuilder();
|
||||
addAuthsToOkBuilder(this.okBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,52 +337,54 @@ public class ApiClient {
|
||||
* expected type is String, then just return the body string.
|
||||
*/
|
||||
class GsonResponseBodyConverterToString<T> implements Converter<ResponseBody, T> {
|
||||
private final Gson gson;
|
||||
private final Type type;
|
||||
|
||||
GsonResponseBodyConverterToString(Gson gson, Type type) {
|
||||
this.gson = gson;
|
||||
this.type = type;
|
||||
}
|
||||
private final Gson gson;
|
||||
private final Type type;
|
||||
|
||||
@Override public T convert(ResponseBody value) throws IOException {
|
||||
String returned = value.string();
|
||||
try {
|
||||
return gson.fromJson(returned, type);
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
return (T) returned;
|
||||
}
|
||||
}
|
||||
GsonResponseBodyConverterToString(Gson gson, Type type) {
|
||||
this.gson = gson;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override public T convert(ResponseBody value) throws IOException {
|
||||
String returned = value.string();
|
||||
try {
|
||||
return gson.fromJson(returned, type);
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
return (T) returned;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GsonCustomConverterFactory extends Converter.Factory
|
||||
{
|
||||
public static GsonCustomConverterFactory create(Gson gson) {
|
||||
return new GsonCustomConverterFactory(gson);
|
||||
}
|
||||
class GsonCustomConverterFactory extends Converter.Factory {
|
||||
|
||||
private final Gson gson;
|
||||
private final GsonConverterFactory gsonConverterFactory;
|
||||
private final Gson gson;
|
||||
private final GsonConverterFactory gsonConverterFactory;
|
||||
|
||||
private GsonCustomConverterFactory(Gson gson) {
|
||||
if (gson == null) throw new NullPointerException("gson == null");
|
||||
this.gson = gson;
|
||||
this.gsonConverterFactory = GsonConverterFactory.create(gson);
|
||||
}
|
||||
public static GsonCustomConverterFactory create(Gson gson) {
|
||||
return new GsonCustomConverterFactory(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
if(type.equals(String.class))
|
||||
return new GsonResponseBodyConverterToString<Object>(gson, type);
|
||||
else
|
||||
return gsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
|
||||
}
|
||||
private GsonCustomConverterFactory(Gson gson) {
|
||||
if (gson == null)
|
||||
throw new NullPointerException("gson == null");
|
||||
this.gson = gson;
|
||||
this.gsonConverterFactory = GsonConverterFactory.create(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||
return gsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
|
||||
}
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
if (type.equals(String.class))
|
||||
return new GsonResponseBodyConverterToString<Object>(gson, type);
|
||||
else
|
||||
return gsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||
return gsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
|
||||
}
|
||||
}
|
||||
|
||||
{{^java8}}
|
||||
@@ -386,29 +393,29 @@ class GsonCustomConverterFactory extends Converter.Factory
|
||||
*/
|
||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
|
||||
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(printFormatter.print(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(printFormatter.print(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return parseFormatter.parseDateTime(date);
|
||||
}
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return parseFormatter.parseDateTime(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -416,28 +423,28 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
*/
|
||||
class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter formatter = ISODateTimeFormat.date();
|
||||
private final DateTimeFormatter formatter = ISODateTimeFormat.date();
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.print(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.print(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return formatter.parseLocalDate(date);
|
||||
}
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return formatter.parseLocalDate(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/java8}}
|
||||
{{#java8}}
|
||||
@@ -446,32 +453,31 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
*/
|
||||
class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
|
||||
|
||||
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.format(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.format(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
if (date.endsWith("+0000")) {
|
||||
date = date.substring(0, date.length()-5) + "Z";
|
||||
}
|
||||
|
||||
return OffsetDateTime.parse(date, formatter);
|
||||
@Override
|
||||
public OffsetDateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
if (date.endsWith("+0000")) {
|
||||
date = date.substring(0, date.length()-5) + "Z";
|
||||
}
|
||||
return OffsetDateTime.parse(date, formatter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -479,27 +485,27 @@ class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
|
||||
*/
|
||||
class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
|
||||
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.format(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.format(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return LocalDate.parse(date, formatter);
|
||||
}
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return LocalDate.parse(date, formatter);
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/java8}}
|
||||
|
||||
@@ -35,13 +35,16 @@ public interface {{classname}} {
|
||||
{{/allParams}}
|
||||
* @return Call<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object{{/returnType}}>
|
||||
*/
|
||||
{{#formParams}}{{#-first}}
|
||||
{{#isMultipart}}@retrofit2.http.Multipart{{/isMultipart}}{{^isMultipart}}@retrofit2.http.FormUrlEncoded{{/isMultipart}}{{/-first}}{{/formParams}}
|
||||
{{#formParams}}
|
||||
{{#-first}}
|
||||
{{#isMultipart}}@retrofit2.http.Multipart{{/isMultipart}}{{^isMultipart}}@retrofit2.http.FormUrlEncoded{{/isMultipart}}
|
||||
{{/-first}}
|
||||
{{/formParams}}
|
||||
{{^formParams}}
|
||||
{{#prioritizedContentTypes}}
|
||||
{{#-first}}
|
||||
@Headers({
|
||||
"Content-Type:{{mediaType}}"
|
||||
"Content-Type:{{mediaType}}"
|
||||
})
|
||||
{{/-first}}
|
||||
{{/prioritizedContentTypes}}
|
||||
|
||||
+1
-4
@@ -29,9 +29,8 @@ public interface FakeApi {
|
||||
* @param body client model (required)
|
||||
* @return Call<Client>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@PATCH("fake")
|
||||
F.Promise<Response<Client>> testClientModel(
|
||||
@@ -57,7 +56,6 @@ public interface FakeApi {
|
||||
* @param paramCallback None (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@POST("fake")
|
||||
F.Promise<Response<Void>> testEndpointParameters(
|
||||
@@ -77,7 +75,6 @@ public interface FakeApi {
|
||||
* @param enumQueryDouble Query parameter enum test (double) (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@GET("fake")
|
||||
F.Promise<Response<Void>> testEnumParameters(
|
||||
|
||||
+2
-10
@@ -28,9 +28,8 @@ public interface PetApi {
|
||||
* @param body Pet object that needs to be added to the store (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@POST("pet")
|
||||
F.Promise<Response<Void>> addPet(
|
||||
@@ -44,7 +43,6 @@ public interface PetApi {
|
||||
* @param apiKey (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("pet/{petId}")
|
||||
F.Promise<Response<Void>> deletePet(
|
||||
@retrofit2.http.Path("petId") Long petId, @retrofit2.http.Header("api_key") String apiKey
|
||||
@@ -56,7 +54,6 @@ public interface PetApi {
|
||||
* @param status Status values that need to be considered for filter (required)
|
||||
* @return Call<List<Pet>>
|
||||
*/
|
||||
|
||||
@GET("pet/findByStatus")
|
||||
F.Promise<Response<List<Pet>>> findPetsByStatus(
|
||||
@retrofit2.http.Query("status") CSVParams status
|
||||
@@ -68,7 +65,6 @@ public interface PetApi {
|
||||
* @param tags Tags to filter by (required)
|
||||
* @return Call<List<Pet>>
|
||||
*/
|
||||
|
||||
@GET("pet/findByTags")
|
||||
F.Promise<Response<List<Pet>>> findPetsByTags(
|
||||
@retrofit2.http.Query("tags") CSVParams tags
|
||||
@@ -80,7 +76,6 @@ public interface PetApi {
|
||||
* @param petId ID of pet to return (required)
|
||||
* @return Call<Pet>
|
||||
*/
|
||||
|
||||
@GET("pet/{petId}")
|
||||
F.Promise<Response<Pet>> getPetById(
|
||||
@retrofit2.http.Path("petId") Long petId
|
||||
@@ -92,9 +87,8 @@ public interface PetApi {
|
||||
* @param body Pet object that needs to be added to the store (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@PUT("pet")
|
||||
F.Promise<Response<Void>> updatePet(
|
||||
@@ -109,7 +103,6 @@ public interface PetApi {
|
||||
* @param status Updated status of the pet (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@POST("pet/{petId}")
|
||||
F.Promise<Response<Void>> updatePetWithForm(
|
||||
@@ -124,7 +117,6 @@ public interface PetApi {
|
||||
* @param file file to upload (optional)
|
||||
* @return Call<ModelApiResponse>
|
||||
*/
|
||||
|
||||
@retrofit2.http.Multipart
|
||||
@POST("pet/{petId}/uploadImage")
|
||||
F.Promise<Response<ModelApiResponse>> uploadFile(
|
||||
|
||||
-4
@@ -26,7 +26,6 @@ public interface StoreApi {
|
||||
* @param orderId ID of the order that needs to be deleted (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("store/order/{orderId}")
|
||||
F.Promise<Response<Void>> deleteOrder(
|
||||
@retrofit2.http.Path("orderId") String orderId
|
||||
@@ -37,7 +36,6 @@ public interface StoreApi {
|
||||
* Returns a map of status codes to quantities
|
||||
* @return Call<Map<String, Integer>>
|
||||
*/
|
||||
|
||||
@GET("store/inventory")
|
||||
F.Promise<Response<Map<String, Integer>>> getInventory();
|
||||
|
||||
@@ -48,7 +46,6 @@ public interface StoreApi {
|
||||
* @param orderId ID of pet that needs to be fetched (required)
|
||||
* @return Call<Order>
|
||||
*/
|
||||
|
||||
@GET("store/order/{orderId}")
|
||||
F.Promise<Response<Order>> getOrderById(
|
||||
@retrofit2.http.Path("orderId") Long orderId
|
||||
@@ -60,7 +57,6 @@ public interface StoreApi {
|
||||
* @param body order placed for purchasing the pet (required)
|
||||
* @return Call<Order>
|
||||
*/
|
||||
|
||||
@POST("store/order")
|
||||
F.Promise<Response<Order>> placeOrder(
|
||||
@retrofit2.http.Body Order body
|
||||
|
||||
-8
@@ -26,7 +26,6 @@ public interface UserApi {
|
||||
* @param body Created user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user")
|
||||
F.Promise<Response<Void>> createUser(
|
||||
@retrofit2.http.Body User body
|
||||
@@ -38,7 +37,6 @@ public interface UserApi {
|
||||
* @param body List of user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user/createWithArray")
|
||||
F.Promise<Response<Void>> createUsersWithArrayInput(
|
||||
@retrofit2.http.Body List<User> body
|
||||
@@ -50,7 +48,6 @@ public interface UserApi {
|
||||
* @param body List of user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user/createWithList")
|
||||
F.Promise<Response<Void>> createUsersWithListInput(
|
||||
@retrofit2.http.Body List<User> body
|
||||
@@ -62,7 +59,6 @@ public interface UserApi {
|
||||
* @param username The name that needs to be deleted (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("user/{username}")
|
||||
F.Promise<Response<Void>> deleteUser(
|
||||
@retrofit2.http.Path("username") String username
|
||||
@@ -74,7 +70,6 @@ public interface UserApi {
|
||||
* @param username The name that needs to be fetched. Use user1 for testing. (required)
|
||||
* @return Call<User>
|
||||
*/
|
||||
|
||||
@GET("user/{username}")
|
||||
F.Promise<Response<User>> getUserByName(
|
||||
@retrofit2.http.Path("username") String username
|
||||
@@ -87,7 +82,6 @@ public interface UserApi {
|
||||
* @param password The password for login in clear text (required)
|
||||
* @return Call<String>
|
||||
*/
|
||||
|
||||
@GET("user/login")
|
||||
F.Promise<Response<String>> loginUser(
|
||||
@retrofit2.http.Query("username") String username, @retrofit2.http.Query("password") String password
|
||||
@@ -98,7 +92,6 @@ public interface UserApi {
|
||||
*
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@GET("user/logout")
|
||||
F.Promise<Response<Void>> logoutUser();
|
||||
|
||||
@@ -110,7 +103,6 @@ public interface UserApi {
|
||||
* @param body Updated user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@PUT("user/{username}")
|
||||
F.Promise<Response<Void>> updateUser(
|
||||
@retrofit2.http.Path("username") String username, @retrofit2.http.Body User body
|
||||
|
||||
+315
-320
@@ -15,8 +15,6 @@ import org.joda.time.format.DateTimeFormatter;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||
|
||||
@@ -31,278 +29,273 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
|
||||
import io.swagger.client.auth.HttpBasicAuth;
|
||||
import io.swagger.client.auth.ApiKeyAuth;
|
||||
import io.swagger.client.auth.OAuth;
|
||||
import io.swagger.client.auth.OAuth.AccessTokenListener;
|
||||
import io.swagger.client.auth.OAuthFlow;
|
||||
|
||||
|
||||
public class ApiClient {
|
||||
|
||||
private Map<String, Interceptor> apiAuthorizations;
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
private Retrofit.Builder adapterBuilder;
|
||||
private Map<String, Interceptor> apiAuthorizations;
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
private Retrofit.Builder adapterBuilder;
|
||||
|
||||
public ApiClient() {
|
||||
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
|
||||
createDefaultAdapter();
|
||||
public ApiClient() {
|
||||
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
|
||||
createDefaultAdapter();
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
Interceptor auth;
|
||||
if ("api_key".equals(authName)) {
|
||||
auth = new ApiKeyAuth("header", "api_key");
|
||||
} else if ("http_basic_test".equals(authName)) {
|
||||
auth = new HttpBasicAuth();
|
||||
} else if ("petstore_auth".equals(authName)) {
|
||||
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
|
||||
} else {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
}
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
Interceptor auth;
|
||||
if ("api_key".equals(authName)) {
|
||||
auth = new ApiKeyAuth("header", "api_key");
|
||||
} else if ("http_basic_test".equals(authName)) {
|
||||
auth = new HttpBasicAuth();
|
||||
} else if ("petstore_auth".equals(authName)) {
|
||||
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
|
||||
} else {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
}
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName Authentication name
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName Authentication name
|
||||
* @param apiKey API key
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param clientId Client ID
|
||||
* @param secret Client Secret
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String clientId, String secret, String username, String password) {
|
||||
this(authName);
|
||||
this.getTokenEndPoint()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(secret)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
}
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
|
||||
.create();
|
||||
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io/v2";
|
||||
if(!baseUrl.endsWith("/"))
|
||||
baseUrl = baseUrl + "/";
|
||||
|
||||
adapterBuilder = new Retrofit
|
||||
.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(GsonCustomConverterFactory.create(gson));
|
||||
}
|
||||
|
||||
public <S> S createService(Class<S> serviceClass) {
|
||||
return adapterBuilder
|
||||
.client(okBuilder.build())
|
||||
.build()
|
||||
.create(serviceClass);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
*/
|
||||
private void setApiKey(String apiKey) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName Authentication name
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password oauth
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
private void setCredentials(String username, String password) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName Authentication name
|
||||
* @param apiKey API key
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
/**
|
||||
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Token request builder
|
||||
*/
|
||||
public TokenRequestBuilder getTokenEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getTokenRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
/**
|
||||
* Helper method to configure authorization endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Authentication request builder
|
||||
*/
|
||||
public AuthenticationRequestBuilder getAuthorizationEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getAuthenticationRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param clientId Client ID
|
||||
* @param secret Client Secret
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String clientId, String secret, String username, String password) {
|
||||
this(authName);
|
||||
this.getTokenEndPoint()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(secret)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
/**
|
||||
* Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @param accessToken Access token
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.setAccessToken(accessToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
|
||||
.create();
|
||||
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io/v2";
|
||||
if(!baseUrl.endsWith("/"))
|
||||
baseUrl = baseUrl + "/";
|
||||
|
||||
adapterBuilder = new Retrofit
|
||||
.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
|
||||
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(GsonCustomConverterFactory.create(gson));
|
||||
/**
|
||||
* Helper method to configure the oauth accessCode/implicit flow parameters
|
||||
* @param clientId Client ID
|
||||
* @param clientSecret Client secret
|
||||
* @param redirectURI Redirect URI
|
||||
*/
|
||||
public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setRedirectURI(redirectURI);
|
||||
oauth.getAuthenticationRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setRedirectURI(redirectURI);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public <S> S createService(Class<S> serviceClass) {
|
||||
return adapterBuilder
|
||||
.client(okBuilder.build())
|
||||
.build()
|
||||
.create(serviceClass);
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener Access token listener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
*/
|
||||
private void setApiKey(String apiKey) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okBuilder.addInterceptor(authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password oauth
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
private void setCredentials(String username, String password) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, Interceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public Retrofit.Builder getAdapterBuilder() {
|
||||
return adapterBuilder;
|
||||
}
|
||||
|
||||
public void setAdapterBuilder(Retrofit.Builder adapterBuilder) {
|
||||
this.adapterBuilder = adapterBuilder;
|
||||
}
|
||||
|
||||
public OkHttpClient.Builder getOkBuilder() {
|
||||
return okBuilder;
|
||||
}
|
||||
|
||||
public void addAuthsToOkBuilder(OkHttpClient.Builder okBuilder) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
okBuilder.addInterceptor(apiAuthorization);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Token request builder
|
||||
*/
|
||||
public TokenRequestBuilder getTokenEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getTokenRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure authorization endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Authentication request builder
|
||||
*/
|
||||
public AuthenticationRequestBuilder getAuthorizationEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getAuthenticationRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @param accessToken Access token
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.setAccessToken(accessToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the oauth accessCode/implicit flow parameters
|
||||
* @param clientId Client ID
|
||||
* @param clientSecret Client secret
|
||||
* @param redirectURI Redirect URI
|
||||
*/
|
||||
public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setRedirectURI(redirectURI);
|
||||
oauth.getAuthenticationRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setRedirectURI(redirectURI);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener Access token listener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okBuilder.addInterceptor(authorization);
|
||||
}
|
||||
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, Interceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public Retrofit.Builder getAdapterBuilder() {
|
||||
return adapterBuilder;
|
||||
}
|
||||
|
||||
public void setAdapterBuilder(Retrofit.Builder adapterBuilder) {
|
||||
this.adapterBuilder = adapterBuilder;
|
||||
}
|
||||
|
||||
public OkHttpClient.Builder getOkBuilder() {
|
||||
return okBuilder;
|
||||
}
|
||||
|
||||
public void addAuthsToOkBuilder(OkHttpClient.Builder okBuilder) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
okBuilder.addInterceptor(apiAuthorization);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the okBuilder given in parameter, adds the auth interceptors and uses it to configure the Retrofit
|
||||
* @param okClient An instance of OK HTTP client
|
||||
*/
|
||||
public void configureFromOkclient(OkHttpClient okClient) {
|
||||
this.okBuilder = okClient.newBuilder();
|
||||
addAuthsToOkBuilder(this.okBuilder);
|
||||
|
||||
}
|
||||
/**
|
||||
* Clones the okBuilder given in parameter, adds the auth interceptors and uses it to configure the Retrofit
|
||||
* @param okClient An instance of OK HTTP client
|
||||
*/
|
||||
public void configureFromOkclient(OkHttpClient okClient) {
|
||||
this.okBuilder = okClient.newBuilder();
|
||||
addAuthsToOkBuilder(this.okBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,52 +304,54 @@ public class ApiClient {
|
||||
* expected type is String, then just return the body string.
|
||||
*/
|
||||
class GsonResponseBodyConverterToString<T> implements Converter<ResponseBody, T> {
|
||||
private final Gson gson;
|
||||
private final Type type;
|
||||
|
||||
GsonResponseBodyConverterToString(Gson gson, Type type) {
|
||||
this.gson = gson;
|
||||
this.type = type;
|
||||
}
|
||||
private final Gson gson;
|
||||
private final Type type;
|
||||
|
||||
@Override public T convert(ResponseBody value) throws IOException {
|
||||
String returned = value.string();
|
||||
try {
|
||||
return gson.fromJson(returned, type);
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
return (T) returned;
|
||||
}
|
||||
}
|
||||
GsonResponseBodyConverterToString(Gson gson, Type type) {
|
||||
this.gson = gson;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override public T convert(ResponseBody value) throws IOException {
|
||||
String returned = value.string();
|
||||
try {
|
||||
return gson.fromJson(returned, type);
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
return (T) returned;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GsonCustomConverterFactory extends Converter.Factory
|
||||
{
|
||||
public static GsonCustomConverterFactory create(Gson gson) {
|
||||
return new GsonCustomConverterFactory(gson);
|
||||
}
|
||||
class GsonCustomConverterFactory extends Converter.Factory {
|
||||
|
||||
private final Gson gson;
|
||||
private final GsonConverterFactory gsonConverterFactory;
|
||||
private final Gson gson;
|
||||
private final GsonConverterFactory gsonConverterFactory;
|
||||
|
||||
private GsonCustomConverterFactory(Gson gson) {
|
||||
if (gson == null) throw new NullPointerException("gson == null");
|
||||
this.gson = gson;
|
||||
this.gsonConverterFactory = GsonConverterFactory.create(gson);
|
||||
}
|
||||
public static GsonCustomConverterFactory create(Gson gson) {
|
||||
return new GsonCustomConverterFactory(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
if(type.equals(String.class))
|
||||
return new GsonResponseBodyConverterToString<Object>(gson, type);
|
||||
else
|
||||
return gsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
|
||||
}
|
||||
private GsonCustomConverterFactory(Gson gson) {
|
||||
if (gson == null)
|
||||
throw new NullPointerException("gson == null");
|
||||
this.gson = gson;
|
||||
this.gsonConverterFactory = GsonConverterFactory.create(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||
return gsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
|
||||
}
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
if (type.equals(String.class))
|
||||
return new GsonResponseBodyConverterToString<Object>(gson, type);
|
||||
else
|
||||
return gsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||
return gsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -364,29 +359,29 @@ class GsonCustomConverterFactory extends Converter.Factory
|
||||
*/
|
||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
|
||||
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(printFormatter.print(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(printFormatter.print(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return parseFormatter.parseDateTime(date);
|
||||
}
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return parseFormatter.parseDateTime(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -394,26 +389,26 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
*/
|
||||
class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter formatter = ISODateTimeFormat.date();
|
||||
private final DateTimeFormatter formatter = ISODateTimeFormat.date();
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.print(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.print(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return formatter.parseLocalDate(date);
|
||||
}
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return formatter.parseLocalDate(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-4
@@ -27,9 +27,8 @@ public interface FakeApi {
|
||||
* @param body client model (required)
|
||||
* @return Call<Client>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@PATCH("fake")
|
||||
Call<Client> testClientModel(
|
||||
@@ -55,7 +54,6 @@ public interface FakeApi {
|
||||
* @param paramCallback None (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@POST("fake")
|
||||
Call<Void> testEndpointParameters(
|
||||
@@ -75,7 +73,6 @@ public interface FakeApi {
|
||||
* @param enumQueryDouble Query parameter enum test (double) (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@GET("fake")
|
||||
Call<Void> testEnumParameters(
|
||||
|
||||
+2
-10
@@ -26,9 +26,8 @@ public interface PetApi {
|
||||
* @param body Pet object that needs to be added to the store (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@POST("pet")
|
||||
Call<Void> addPet(
|
||||
@@ -42,7 +41,6 @@ public interface PetApi {
|
||||
* @param apiKey (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("pet/{petId}")
|
||||
Call<Void> deletePet(
|
||||
@retrofit2.http.Path("petId") Long petId, @retrofit2.http.Header("api_key") String apiKey
|
||||
@@ -54,7 +52,6 @@ public interface PetApi {
|
||||
* @param status Status values that need to be considered for filter (required)
|
||||
* @return Call<List<Pet>>
|
||||
*/
|
||||
|
||||
@GET("pet/findByStatus")
|
||||
Call<List<Pet>> findPetsByStatus(
|
||||
@retrofit2.http.Query("status") CSVParams status
|
||||
@@ -66,7 +63,6 @@ public interface PetApi {
|
||||
* @param tags Tags to filter by (required)
|
||||
* @return Call<List<Pet>>
|
||||
*/
|
||||
|
||||
@GET("pet/findByTags")
|
||||
Call<List<Pet>> findPetsByTags(
|
||||
@retrofit2.http.Query("tags") CSVParams tags
|
||||
@@ -78,7 +74,6 @@ public interface PetApi {
|
||||
* @param petId ID of pet to return (required)
|
||||
* @return Call<Pet>
|
||||
*/
|
||||
|
||||
@GET("pet/{petId}")
|
||||
Call<Pet> getPetById(
|
||||
@retrofit2.http.Path("petId") Long petId
|
||||
@@ -90,9 +85,8 @@ public interface PetApi {
|
||||
* @param body Pet object that needs to be added to the store (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@PUT("pet")
|
||||
Call<Void> updatePet(
|
||||
@@ -107,7 +101,6 @@ public interface PetApi {
|
||||
* @param status Updated status of the pet (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@POST("pet/{petId}")
|
||||
Call<Void> updatePetWithForm(
|
||||
@@ -122,7 +115,6 @@ public interface PetApi {
|
||||
* @param file file to upload (optional)
|
||||
* @return Call<ModelApiResponse>
|
||||
*/
|
||||
|
||||
@retrofit2.http.Multipart
|
||||
@POST("pet/{petId}/uploadImage")
|
||||
Call<ModelApiResponse> uploadFile(
|
||||
|
||||
-4
@@ -24,7 +24,6 @@ public interface StoreApi {
|
||||
* @param orderId ID of the order that needs to be deleted (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("store/order/{orderId}")
|
||||
Call<Void> deleteOrder(
|
||||
@retrofit2.http.Path("orderId") String orderId
|
||||
@@ -35,7 +34,6 @@ public interface StoreApi {
|
||||
* Returns a map of status codes to quantities
|
||||
* @return Call<Map<String, Integer>>
|
||||
*/
|
||||
|
||||
@GET("store/inventory")
|
||||
Call<Map<String, Integer>> getInventory();
|
||||
|
||||
@@ -46,7 +44,6 @@ public interface StoreApi {
|
||||
* @param orderId ID of pet that needs to be fetched (required)
|
||||
* @return Call<Order>
|
||||
*/
|
||||
|
||||
@GET("store/order/{orderId}")
|
||||
Call<Order> getOrderById(
|
||||
@retrofit2.http.Path("orderId") Long orderId
|
||||
@@ -58,7 +55,6 @@ public interface StoreApi {
|
||||
* @param body order placed for purchasing the pet (required)
|
||||
* @return Call<Order>
|
||||
*/
|
||||
|
||||
@POST("store/order")
|
||||
Call<Order> placeOrder(
|
||||
@retrofit2.http.Body Order body
|
||||
|
||||
-8
@@ -24,7 +24,6 @@ public interface UserApi {
|
||||
* @param body Created user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user")
|
||||
Call<Void> createUser(
|
||||
@retrofit2.http.Body User body
|
||||
@@ -36,7 +35,6 @@ public interface UserApi {
|
||||
* @param body List of user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user/createWithArray")
|
||||
Call<Void> createUsersWithArrayInput(
|
||||
@retrofit2.http.Body List<User> body
|
||||
@@ -48,7 +46,6 @@ public interface UserApi {
|
||||
* @param body List of user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user/createWithList")
|
||||
Call<Void> createUsersWithListInput(
|
||||
@retrofit2.http.Body List<User> body
|
||||
@@ -60,7 +57,6 @@ public interface UserApi {
|
||||
* @param username The name that needs to be deleted (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("user/{username}")
|
||||
Call<Void> deleteUser(
|
||||
@retrofit2.http.Path("username") String username
|
||||
@@ -72,7 +68,6 @@ public interface UserApi {
|
||||
* @param username The name that needs to be fetched. Use user1 for testing. (required)
|
||||
* @return Call<User>
|
||||
*/
|
||||
|
||||
@GET("user/{username}")
|
||||
Call<User> getUserByName(
|
||||
@retrofit2.http.Path("username") String username
|
||||
@@ -85,7 +80,6 @@ public interface UserApi {
|
||||
* @param password The password for login in clear text (required)
|
||||
* @return Call<String>
|
||||
*/
|
||||
|
||||
@GET("user/login")
|
||||
Call<String> loginUser(
|
||||
@retrofit2.http.Query("username") String username, @retrofit2.http.Query("password") String password
|
||||
@@ -96,7 +90,6 @@ public interface UserApi {
|
||||
*
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@GET("user/logout")
|
||||
Call<Void> logoutUser();
|
||||
|
||||
@@ -108,7 +101,6 @@ public interface UserApi {
|
||||
* @param body Updated user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@PUT("user/{username}")
|
||||
Call<Void> updateUser(
|
||||
@retrofit2.http.Path("username") String username, @retrofit2.http.Body User body
|
||||
|
||||
+316
-319
@@ -16,7 +16,6 @@ import org.joda.time.format.ISODateTimeFormat;
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
||||
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||
|
||||
@@ -31,278 +30,274 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
|
||||
import io.swagger.client.auth.HttpBasicAuth;
|
||||
import io.swagger.client.auth.ApiKeyAuth;
|
||||
import io.swagger.client.auth.OAuth;
|
||||
import io.swagger.client.auth.OAuth.AccessTokenListener;
|
||||
import io.swagger.client.auth.OAuthFlow;
|
||||
|
||||
|
||||
public class ApiClient {
|
||||
|
||||
private Map<String, Interceptor> apiAuthorizations;
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
private Retrofit.Builder adapterBuilder;
|
||||
private Map<String, Interceptor> apiAuthorizations;
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
private Retrofit.Builder adapterBuilder;
|
||||
|
||||
public ApiClient() {
|
||||
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
|
||||
createDefaultAdapter();
|
||||
public ApiClient() {
|
||||
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
|
||||
createDefaultAdapter();
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
Interceptor auth;
|
||||
if ("api_key".equals(authName)) {
|
||||
auth = new ApiKeyAuth("header", "api_key");
|
||||
} else if ("http_basic_test".equals(authName)) {
|
||||
auth = new HttpBasicAuth();
|
||||
} else if ("petstore_auth".equals(authName)) {
|
||||
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
|
||||
} else {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
}
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
Interceptor auth;
|
||||
if ("api_key".equals(authName)) {
|
||||
auth = new ApiKeyAuth("header", "api_key");
|
||||
} else if ("http_basic_test".equals(authName)) {
|
||||
auth = new HttpBasicAuth();
|
||||
} else if ("petstore_auth".equals(authName)) {
|
||||
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
|
||||
} else {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
}
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName Authentication name
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName Authentication name
|
||||
* @param apiKey API key
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param clientId Client ID
|
||||
* @param secret Client Secret
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String clientId, String secret, String username, String password) {
|
||||
this(authName);
|
||||
this.getTokenEndPoint()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(secret)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
}
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
|
||||
.create();
|
||||
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io/v2";
|
||||
if(!baseUrl.endsWith("/"))
|
||||
baseUrl = baseUrl + "/";
|
||||
|
||||
adapterBuilder = new Retrofit
|
||||
.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(GsonCustomConverterFactory.create(gson));
|
||||
}
|
||||
|
||||
public <S> S createService(Class<S> serviceClass) {
|
||||
return adapterBuilder
|
||||
.client(okBuilder.build())
|
||||
.build()
|
||||
.create(serviceClass);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
*/
|
||||
private void setApiKey(String apiKey) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName Authentication name
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password oauth
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
private void setCredentials(String username, String password) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName Authentication name
|
||||
* @param apiKey API key
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
/**
|
||||
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Token request builder
|
||||
*/
|
||||
public TokenRequestBuilder getTokenEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getTokenRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
/**
|
||||
* Helper method to configure authorization endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Authentication request builder
|
||||
*/
|
||||
public AuthenticationRequestBuilder getAuthorizationEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getAuthenticationRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single password oauth2
|
||||
* @param authName Authentication name
|
||||
* @param clientId Client ID
|
||||
* @param secret Client Secret
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
public ApiClient(String authName, String clientId, String secret, String username, String password) {
|
||||
this(authName);
|
||||
this.getTokenEndPoint()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(secret)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
/**
|
||||
* Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @param accessToken Access token
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.setAccessToken(accessToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
|
||||
.create();
|
||||
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io/v2";
|
||||
if(!baseUrl.endsWith("/"))
|
||||
baseUrl = baseUrl + "/";
|
||||
|
||||
adapterBuilder = new Retrofit
|
||||
.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
||||
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(GsonCustomConverterFactory.create(gson));
|
||||
/**
|
||||
* Helper method to configure the oauth accessCode/implicit flow parameters
|
||||
* @param clientId Client ID
|
||||
* @param clientSecret Client secret
|
||||
* @param redirectURI Redirect URI
|
||||
*/
|
||||
public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setRedirectURI(redirectURI);
|
||||
oauth.getAuthenticationRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setRedirectURI(redirectURI);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public <S> S createService(Class<S> serviceClass) {
|
||||
return adapterBuilder
|
||||
.client(okBuilder.build())
|
||||
.build()
|
||||
.create(serviceClass);
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener Access token listener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
*/
|
||||
private void setApiKey(String apiKey) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okBuilder.addInterceptor(authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password oauth
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
*/
|
||||
private void setCredentials(String username, String password) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, Interceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public Retrofit.Builder getAdapterBuilder() {
|
||||
return adapterBuilder;
|
||||
}
|
||||
|
||||
public void setAdapterBuilder(Retrofit.Builder adapterBuilder) {
|
||||
this.adapterBuilder = adapterBuilder;
|
||||
}
|
||||
|
||||
public OkHttpClient.Builder getOkBuilder() {
|
||||
return okBuilder;
|
||||
}
|
||||
|
||||
public void addAuthsToOkBuilder(OkHttpClient.Builder okBuilder) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
okBuilder.addInterceptor(apiAuthorization);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Token request builder
|
||||
*/
|
||||
public TokenRequestBuilder getTokenEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getTokenRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure authorization endpoint of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @return Authentication request builder
|
||||
*/
|
||||
public AuthenticationRequestBuilder getAuthorizationEndPoint() {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
return oauth.getAuthenticationRequestBuilder();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
|
||||
* @param accessToken Access token
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.setAccessToken(accessToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the oauth accessCode/implicit flow parameters
|
||||
* @param clientId Client ID
|
||||
* @param clientSecret Client secret
|
||||
* @param redirectURI Redirect URI
|
||||
*/
|
||||
public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.getTokenRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setRedirectURI(redirectURI);
|
||||
oauth.getAuthenticationRequestBuilder()
|
||||
.setClientId(clientId)
|
||||
.setRedirectURI(redirectURI);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a listener which is notified when a new access token is received.
|
||||
* @param accessTokenListener Access token listener
|
||||
*/
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof OAuth) {
|
||||
OAuth oauth = (OAuth) apiAuthorization;
|
||||
oauth.registerAccessTokenListener(accessTokenListener);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
*/
|
||||
public void addAuthorization(String authName, Interceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
okBuilder.addInterceptor(authorization);
|
||||
}
|
||||
|
||||
public Map<String, Interceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, Interceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public Retrofit.Builder getAdapterBuilder() {
|
||||
return adapterBuilder;
|
||||
}
|
||||
|
||||
public void setAdapterBuilder(Retrofit.Builder adapterBuilder) {
|
||||
this.adapterBuilder = adapterBuilder;
|
||||
}
|
||||
|
||||
public OkHttpClient.Builder getOkBuilder() {
|
||||
return okBuilder;
|
||||
}
|
||||
|
||||
public void addAuthsToOkBuilder(OkHttpClient.Builder okBuilder) {
|
||||
for(Interceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
okBuilder.addInterceptor(apiAuthorization);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the okBuilder given in parameter, adds the auth interceptors and uses it to configure the Retrofit
|
||||
* @param okClient An instance of OK HTTP client
|
||||
*/
|
||||
public void configureFromOkclient(OkHttpClient okClient) {
|
||||
this.okBuilder = okClient.newBuilder();
|
||||
addAuthsToOkBuilder(this.okBuilder);
|
||||
|
||||
}
|
||||
/**
|
||||
* Clones the okBuilder given in parameter, adds the auth interceptors and uses it to configure the Retrofit
|
||||
* @param okClient An instance of OK HTTP client
|
||||
*/
|
||||
public void configureFromOkclient(OkHttpClient okClient) {
|
||||
this.okBuilder = okClient.newBuilder();
|
||||
addAuthsToOkBuilder(this.okBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,52 +306,54 @@ public class ApiClient {
|
||||
* expected type is String, then just return the body string.
|
||||
*/
|
||||
class GsonResponseBodyConverterToString<T> implements Converter<ResponseBody, T> {
|
||||
private final Gson gson;
|
||||
private final Type type;
|
||||
|
||||
GsonResponseBodyConverterToString(Gson gson, Type type) {
|
||||
this.gson = gson;
|
||||
this.type = type;
|
||||
}
|
||||
private final Gson gson;
|
||||
private final Type type;
|
||||
|
||||
@Override public T convert(ResponseBody value) throws IOException {
|
||||
String returned = value.string();
|
||||
try {
|
||||
return gson.fromJson(returned, type);
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
return (T) returned;
|
||||
}
|
||||
}
|
||||
GsonResponseBodyConverterToString(Gson gson, Type type) {
|
||||
this.gson = gson;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override public T convert(ResponseBody value) throws IOException {
|
||||
String returned = value.string();
|
||||
try {
|
||||
return gson.fromJson(returned, type);
|
||||
}
|
||||
catch (JsonParseException e) {
|
||||
return (T) returned;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GsonCustomConverterFactory extends Converter.Factory
|
||||
{
|
||||
public static GsonCustomConverterFactory create(Gson gson) {
|
||||
return new GsonCustomConverterFactory(gson);
|
||||
}
|
||||
class GsonCustomConverterFactory extends Converter.Factory {
|
||||
|
||||
private final Gson gson;
|
||||
private final GsonConverterFactory gsonConverterFactory;
|
||||
private final Gson gson;
|
||||
private final GsonConverterFactory gsonConverterFactory;
|
||||
|
||||
private GsonCustomConverterFactory(Gson gson) {
|
||||
if (gson == null) throw new NullPointerException("gson == null");
|
||||
this.gson = gson;
|
||||
this.gsonConverterFactory = GsonConverterFactory.create(gson);
|
||||
}
|
||||
public static GsonCustomConverterFactory create(Gson gson) {
|
||||
return new GsonCustomConverterFactory(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
if(type.equals(String.class))
|
||||
return new GsonResponseBodyConverterToString<Object>(gson, type);
|
||||
else
|
||||
return gsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
|
||||
}
|
||||
private GsonCustomConverterFactory(Gson gson) {
|
||||
if (gson == null)
|
||||
throw new NullPointerException("gson == null");
|
||||
this.gson = gson;
|
||||
this.gsonConverterFactory = GsonConverterFactory.create(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||
return gsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
|
||||
}
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
if (type.equals(String.class))
|
||||
return new GsonResponseBodyConverterToString<Object>(gson, type);
|
||||
else
|
||||
return gsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||
return gsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -364,29 +361,29 @@ class GsonCustomConverterFactory extends Converter.Factory
|
||||
*/
|
||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
|
||||
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(printFormatter.print(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(printFormatter.print(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return parseFormatter.parseDateTime(date);
|
||||
}
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return parseFormatter.parseDateTime(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -394,26 +391,26 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
*/
|
||||
class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter formatter = ISODateTimeFormat.date();
|
||||
private final DateTimeFormatter formatter = ISODateTimeFormat.date();
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.print(date));
|
||||
}
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.print(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return formatter.parseLocalDate(date);
|
||||
}
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return formatter.parseLocalDate(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-4
@@ -27,9 +27,8 @@ public interface FakeApi {
|
||||
* @param body client model (required)
|
||||
* @return Call<Client>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@PATCH("fake")
|
||||
Observable<Client> testClientModel(
|
||||
@@ -55,7 +54,6 @@ public interface FakeApi {
|
||||
* @param paramCallback None (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@POST("fake")
|
||||
Observable<Void> testEndpointParameters(
|
||||
@@ -75,7 +73,6 @@ public interface FakeApi {
|
||||
* @param enumQueryDouble Query parameter enum test (double) (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@GET("fake")
|
||||
Observable<Void> testEnumParameters(
|
||||
|
||||
+2
-10
@@ -26,9 +26,8 @@ public interface PetApi {
|
||||
* @param body Pet object that needs to be added to the store (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@POST("pet")
|
||||
Observable<Void> addPet(
|
||||
@@ -42,7 +41,6 @@ public interface PetApi {
|
||||
* @param apiKey (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("pet/{petId}")
|
||||
Observable<Void> deletePet(
|
||||
@retrofit2.http.Path("petId") Long petId, @retrofit2.http.Header("api_key") String apiKey
|
||||
@@ -54,7 +52,6 @@ public interface PetApi {
|
||||
* @param status Status values that need to be considered for filter (required)
|
||||
* @return Call<List<Pet>>
|
||||
*/
|
||||
|
||||
@GET("pet/findByStatus")
|
||||
Observable<List<Pet>> findPetsByStatus(
|
||||
@retrofit2.http.Query("status") CSVParams status
|
||||
@@ -66,7 +63,6 @@ public interface PetApi {
|
||||
* @param tags Tags to filter by (required)
|
||||
* @return Call<List<Pet>>
|
||||
*/
|
||||
|
||||
@GET("pet/findByTags")
|
||||
Observable<List<Pet>> findPetsByTags(
|
||||
@retrofit2.http.Query("tags") CSVParams tags
|
||||
@@ -78,7 +74,6 @@ public interface PetApi {
|
||||
* @param petId ID of pet to return (required)
|
||||
* @return Call<Pet>
|
||||
*/
|
||||
|
||||
@GET("pet/{petId}")
|
||||
Observable<Pet> getPetById(
|
||||
@retrofit2.http.Path("petId") Long petId
|
||||
@@ -90,9 +85,8 @@ public interface PetApi {
|
||||
* @param body Pet object that needs to be added to the store (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@Headers({
|
||||
"Content-Type:application/json"
|
||||
"Content-Type:application/json"
|
||||
})
|
||||
@PUT("pet")
|
||||
Observable<Void> updatePet(
|
||||
@@ -107,7 +101,6 @@ public interface PetApi {
|
||||
* @param status Updated status of the pet (optional)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@retrofit2.http.FormUrlEncoded
|
||||
@POST("pet/{petId}")
|
||||
Observable<Void> updatePetWithForm(
|
||||
@@ -122,7 +115,6 @@ public interface PetApi {
|
||||
* @param file file to upload (optional)
|
||||
* @return Call<ModelApiResponse>
|
||||
*/
|
||||
|
||||
@retrofit2.http.Multipart
|
||||
@POST("pet/{petId}/uploadImage")
|
||||
Observable<ModelApiResponse> uploadFile(
|
||||
|
||||
-4
@@ -24,7 +24,6 @@ public interface StoreApi {
|
||||
* @param orderId ID of the order that needs to be deleted (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("store/order/{orderId}")
|
||||
Observable<Void> deleteOrder(
|
||||
@retrofit2.http.Path("orderId") String orderId
|
||||
@@ -35,7 +34,6 @@ public interface StoreApi {
|
||||
* Returns a map of status codes to quantities
|
||||
* @return Call<Map<String, Integer>>
|
||||
*/
|
||||
|
||||
@GET("store/inventory")
|
||||
Observable<Map<String, Integer>> getInventory();
|
||||
|
||||
@@ -46,7 +44,6 @@ public interface StoreApi {
|
||||
* @param orderId ID of pet that needs to be fetched (required)
|
||||
* @return Call<Order>
|
||||
*/
|
||||
|
||||
@GET("store/order/{orderId}")
|
||||
Observable<Order> getOrderById(
|
||||
@retrofit2.http.Path("orderId") Long orderId
|
||||
@@ -58,7 +55,6 @@ public interface StoreApi {
|
||||
* @param body order placed for purchasing the pet (required)
|
||||
* @return Call<Order>
|
||||
*/
|
||||
|
||||
@POST("store/order")
|
||||
Observable<Order> placeOrder(
|
||||
@retrofit2.http.Body Order body
|
||||
|
||||
-8
@@ -24,7 +24,6 @@ public interface UserApi {
|
||||
* @param body Created user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user")
|
||||
Observable<Void> createUser(
|
||||
@retrofit2.http.Body User body
|
||||
@@ -36,7 +35,6 @@ public interface UserApi {
|
||||
* @param body List of user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user/createWithArray")
|
||||
Observable<Void> createUsersWithArrayInput(
|
||||
@retrofit2.http.Body List<User> body
|
||||
@@ -48,7 +46,6 @@ public interface UserApi {
|
||||
* @param body List of user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@POST("user/createWithList")
|
||||
Observable<Void> createUsersWithListInput(
|
||||
@retrofit2.http.Body List<User> body
|
||||
@@ -60,7 +57,6 @@ public interface UserApi {
|
||||
* @param username The name that needs to be deleted (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@DELETE("user/{username}")
|
||||
Observable<Void> deleteUser(
|
||||
@retrofit2.http.Path("username") String username
|
||||
@@ -72,7 +68,6 @@ public interface UserApi {
|
||||
* @param username The name that needs to be fetched. Use user1 for testing. (required)
|
||||
* @return Call<User>
|
||||
*/
|
||||
|
||||
@GET("user/{username}")
|
||||
Observable<User> getUserByName(
|
||||
@retrofit2.http.Path("username") String username
|
||||
@@ -85,7 +80,6 @@ public interface UserApi {
|
||||
* @param password The password for login in clear text (required)
|
||||
* @return Call<String>
|
||||
*/
|
||||
|
||||
@GET("user/login")
|
||||
Observable<String> loginUser(
|
||||
@retrofit2.http.Query("username") String username, @retrofit2.http.Query("password") String password
|
||||
@@ -96,7 +90,6 @@ public interface UserApi {
|
||||
*
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@GET("user/logout")
|
||||
Observable<Void> logoutUser();
|
||||
|
||||
@@ -108,7 +101,6 @@ public interface UserApi {
|
||||
* @param body Updated user object (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
|
||||
@PUT("user/{username}")
|
||||
Observable<Void> updateUser(
|
||||
@retrofit2.http.Path("username") String username, @retrofit2.http.Body User body
|
||||
|
||||
Reference in New Issue
Block a user