forked from loafle/openapi-generator-original
@@ -1,33 +1,107 @@
|
||||
package io.swagger.client;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
import feign.Feign;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.jackson.JacksonDecoder;
|
||||
import feign.jackson.JacksonEncoder;
|
||||
import feign.slf4j.Slf4jLogger;
|
||||
import io.swagger.client.auth.*;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class ApiClient {
|
||||
public interface Api {}
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
private String basePath = "http://petstore.swagger.io/v2";
|
||||
private Map<String, RequestInterceptor> apiAuthorizations;
|
||||
private Feign.Builder feignBuilder;
|
||||
|
||||
public ApiClient() {
|
||||
objectMapper = createObjectMapper();
|
||||
apiAuthorizations = new LinkedHashMap<String, RequestInterceptor>();
|
||||
feignBuilder = Feign.builder()
|
||||
.encoder(new FormAwareEncoder(new JacksonEncoder(objectMapper)))
|
||||
.decoder(new JacksonDecoder(objectMapper))
|
||||
.logger(new Slf4jLogger());
|
||||
}
|
||||
|
||||
public ApiClient(String[] authNames) {
|
||||
this();
|
||||
for(String authName : authNames) {
|
||||
RequestInterceptor auth;
|
||||
if (authName == "petstore_auth") {
|
||||
auth = new OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
|
||||
} else if (authName == "api_key") {
|
||||
auth = new ApiKeyAuth("header", "api_key");
|
||||
} else {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
|
||||
}
|
||||
addAuthorization(authName, auth);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constructor for single auth name
|
||||
* @param authName
|
||||
*/
|
||||
public ApiClient(String authName) {
|
||||
this(new String[]{authName});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single api key
|
||||
* @param authName
|
||||
* @param apiKey
|
||||
*/
|
||||
public ApiClient(String authName, String apiKey) {
|
||||
this(authName);
|
||||
this.setApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper constructor for single basic auth or password oauth2
|
||||
* @param authName
|
||||
* @param username
|
||||
* @param password
|
||||
*/
|
||||
public ApiClient(String authName, String username, String password) {
|
||||
this(authName);
|
||||
this.setCredentials(username, password);
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
|
||||
public Map<String, RequestInterceptor> getApiAuthorizations() {
|
||||
return apiAuthorizations;
|
||||
}
|
||||
|
||||
public void setApiAuthorizations(Map<String, RequestInterceptor> apiAuthorizations) {
|
||||
this.apiAuthorizations = apiAuthorizations;
|
||||
}
|
||||
|
||||
public ApiClient setBasePath(String basePath) {
|
||||
this.basePath = basePath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Feign.Builder getFeignBuilder() {
|
||||
return feignBuilder;
|
||||
}
|
||||
|
||||
public ApiClient setFeignBuilder(Feign.Builder feignBuilder) {
|
||||
this.feignBuilder = feignBuilder;
|
||||
return this;
|
||||
}
|
||||
|
||||
private ObjectMapper createObjectMapper() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
||||
@@ -45,13 +119,7 @@ public class ApiClient {
|
||||
* XYZResponse response = api.someMethod(...);
|
||||
*/
|
||||
public <T extends Api> T buildClient(Class<T> clientClass) {
|
||||
return Feign.builder()
|
||||
.encoder(new FormAwareEncoder(new JacksonEncoder(objectMapper)))
|
||||
.decoder(new JacksonDecoder(objectMapper))
|
||||
// enable for basic auth:
|
||||
// .requestInterceptor(new feign.auth.BasicAuthRequestInterceptor(username, password))
|
||||
.logger(new Slf4jLogger())
|
||||
.target(clientClass, basePath);
|
||||
return feignBuilder.target(clientClass, basePath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,4 +151,53 @@ public class ApiClient {
|
||||
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json";
|
||||
return contentTypes[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey
|
||||
*/
|
||||
public void setApiKey(String apiKey) {
|
||||
for(RequestInterceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof ApiKeyAuth) {
|
||||
ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
|
||||
keyAuth.setApiKey(apiKey);
|
||||
return ;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("No API key authentication configured!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the username/password for basic auth or password OAuth
|
||||
* @param username
|
||||
* @param password
|
||||
*/
|
||||
public void setCredentials(String username, String password) {
|
||||
for(RequestInterceptor apiAuthorization : apiAuthorizations.values()) {
|
||||
if (apiAuthorization instanceof HttpBasicAuth) {
|
||||
HttpBasicAuth basicAuth = (HttpBasicAuth) apiAuthorization;
|
||||
basicAuth.setCredentials(username, password);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("No Basic authentication configured!");
|
||||
}
|
||||
|
||||
public RequestInterceptor getAuthorization(String authName) {
|
||||
return apiAuthorizations.get(authName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName
|
||||
* @param authorization
|
||||
*/
|
||||
public void addAuthorization(String authName, RequestInterceptor authorization) {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
feignBuilder.requestInterceptor(authorization);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import feign.codec.EncodeException;
|
||||
import feign.codec.Encoder;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class FormAwareEncoder implements Encoder {
|
||||
public static final String UTF_8 = "utf-8";
|
||||
private static final String LINE_FEED = "\r\n";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.swagger.client;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class StringUtil {
|
||||
/**
|
||||
* Check if the given array contains the given value (with case-insensitive comparison).
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.io.File;
|
||||
import java.util.*;
|
||||
import feign.*;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public interface PetApi extends ApiClient.Api {
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import io.swagger.client.model.Order;
|
||||
import java.util.*;
|
||||
import feign.*;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public interface StoreApi extends ApiClient.Api {
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.*;
|
||||
import java.util.*;
|
||||
import feign.*;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public interface UserApi extends ApiClient.Api {
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.swagger.client.auth;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
public class ApiKeyAuth implements RequestInterceptor {
|
||||
private final String location;
|
||||
private final String paramName;
|
||||
|
||||
private String apiKey;
|
||||
|
||||
public ApiKeyAuth(String location, String paramName) {
|
||||
this.location = location;
|
||||
this.paramName = paramName;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public String getParamName() {
|
||||
return paramName;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
if (location == "query") {
|
||||
template.query(paramName, apiKey);
|
||||
} else if (location == "header") {
|
||||
template.header(paramName, apiKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.swagger.client.auth;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import feign.auth.BasicAuthRequestInterceptor;
|
||||
|
||||
/**
|
||||
* An interceptor that adds the request header needed to use HTTP basic authentication.
|
||||
*/
|
||||
public class HttpBasicAuth implements RequestInterceptor {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setCredentials(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
RequestInterceptor requestInterceptor = new BasicAuthRequestInterceptor(username, password);
|
||||
requestInterceptor.apply(template);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.swagger.client.auth;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
public class OAuth implements RequestInterceptor {
|
||||
|
||||
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) {
|
||||
// TODO Not implemented yet
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
// TODO Not implemented yet
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.swagger.client.auth;
|
||||
|
||||
public enum OAuthFlow {
|
||||
accessCode, implicit, password, application
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class Category {
|
||||
|
||||
private Long id = null;
|
||||
@@ -53,8 +53,10 @@ public class Category {
|
||||
return false;
|
||||
}
|
||||
Category category = (Category) o;
|
||||
return Objects.equals(id, category.id) &&
|
||||
Objects.equals(name, category.name);
|
||||
|
||||
return true && Objects.equals(id, category.id) &&
|
||||
Objects.equals(name, category.name)
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.Date;
|
||||
|
||||
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class Order {
|
||||
|
||||
private Long id = null;
|
||||
@@ -131,12 +131,14 @@ public class Order {
|
||||
return false;
|
||||
}
|
||||
Order order = (Order) o;
|
||||
return Objects.equals(id, order.id) &&
|
||||
|
||||
return true && Objects.equals(id, order.id) &&
|
||||
Objects.equals(petId, order.petId) &&
|
||||
Objects.equals(quantity, order.quantity) &&
|
||||
Objects.equals(shipDate, order.shipDate) &&
|
||||
Objects.equals(status, order.status) &&
|
||||
Objects.equals(complete, order.complete);
|
||||
Objects.equals(complete, order.complete)
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.*;
|
||||
|
||||
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class Pet {
|
||||
|
||||
private Long id = null;
|
||||
@@ -133,12 +133,14 @@ public class Pet {
|
||||
return false;
|
||||
}
|
||||
Pet pet = (Pet) o;
|
||||
return Objects.equals(id, pet.id) &&
|
||||
|
||||
return true && Objects.equals(id, pet.id) &&
|
||||
Objects.equals(category, pet.category) &&
|
||||
Objects.equals(name, pet.name) &&
|
||||
Objects.equals(photoUrls, pet.photoUrls) &&
|
||||
Objects.equals(tags, pet.tags) &&
|
||||
Objects.equals(status, pet.status);
|
||||
Objects.equals(status, pet.status)
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,7 +10,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class Tag {
|
||||
|
||||
private Long id = null;
|
||||
@@ -53,8 +53,10 @@ public class Tag {
|
||||
return false;
|
||||
}
|
||||
Tag tag = (Tag) o;
|
||||
return Objects.equals(id, tag.id) &&
|
||||
Objects.equals(name, tag.name);
|
||||
|
||||
return true && Objects.equals(id, tag.id) &&
|
||||
Objects.equals(name, tag.name)
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,7 +10,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:18.888+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-09T15:18:34.525Z")
|
||||
public class User {
|
||||
|
||||
private Long id = null;
|
||||
@@ -138,14 +138,16 @@ public class User {
|
||||
return false;
|
||||
}
|
||||
User user = (User) o;
|
||||
return Objects.equals(id, user.id) &&
|
||||
|
||||
return true && Objects.equals(id, user.id) &&
|
||||
Objects.equals(username, user.username) &&
|
||||
Objects.equals(firstName, user.firstName) &&
|
||||
Objects.equals(lastName, user.lastName) &&
|
||||
Objects.equals(email, user.email) &&
|
||||
Objects.equals(password, user.password) &&
|
||||
Objects.equals(phone, user.phone) &&
|
||||
Objects.equals(userStatus, user.userStatus);
|
||||
Objects.equals(userStatus, user.userStatus)
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user