Add petstore client sample for Java-feign

This commit is contained in:
xhh
2015-12-01 16:13:31 +08:00
parent 21e8822343
commit eb0e47461c
18 changed files with 1437 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
<manifest package="io.swagger.client" xmlns:android="http://schemas.android.com/apk/res/android">
<application />
</manifest>

View File

@@ -0,0 +1,86 @@
package io.swagger.client;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.slf4j.Slf4jLogger;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class ApiClient {
public interface Api {}
private ObjectMapper objectMapper;
private String basePath = "http://petstore.swagger.io/v2";
public ApiClient() {
objectMapper = createObjectMapper();
}
public String getBasePath() {
return basePath;
}
public ApiClient setBasePath(String basePath) {
this.basePath = basePath;
return this;
}
private ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
return objectMapper;
}
/**
* Creates a feign client for given API interface.
*
* Usage:
* ApiClient apiClient = new ApiClient();
* apiClient.setBasePath("http://localhost:8080");
* XYZApi api = apiClient.buildClient(XYZApi.class);
* XYZResponse response = api.someMethod(...);
*/
public <T extends Api> T buildClient(Class<T> clientClass) {
return Feign.builder()
.encoder(new JacksonEncoder(objectMapper))
.decoder(new JacksonDecoder(objectMapper))
// enable for basic auth:
// .requestInterceptor(new feign.auth.BasicAuthRequestInterceptor(username, password))
.logger(new Slf4jLogger())
.target(clientClass, basePath);
}
/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
* otherwise use all of them (joining into a string)
*
* @param accepts The accepts array to select from
* @return The Accept header to use. If the given array is empty,
* null will be returned (not to set the Accept header explicitly).
*/
public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) return null;
if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json";
return StringUtil.join(accepts, ",");
}
/**
* Select the Content-Type header's value from the given array:
* if JSON exists in the given array, use it;
* otherwise use the first one of the array.
*
* @param contentTypes The Content-Type array to select from
* @return The Content-Type header to use. If the given array is empty,
* JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) return "application/json";
if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json";
return contentTypes[0];
}
}

View File

@@ -0,0 +1,51 @@
package io.swagger.client;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class StringUtil {
/**
* Check if the given array contains the given value (with case-insensitive comparison).
*
* @param array The array
* @param value The value to search
* @return true if the array contains the value
*/
public static boolean containsIgnoreCase(String[] array, String value) {
for (String str : array) {
if (value == null && str == null) return true;
if (value != null && value.equalsIgnoreCase(str)) return true;
}
return false;
}
/**
* Join an array of strings with the given separator.
* <p>
* Note: This might be replaced by utility method from commons-lang or guava someday
* if one of those libraries is added as dependency.
* </p>
*
* @param array The array of strings
* @param separator The separator
* @return the resulting string
*/
public static String join(String[] array, String separator) {
int len = array.length;
if (len == 0) return "";
StringBuilder out = new StringBuilder();
out.append(array[0]);
for (int i = 1; i < len; i++) {
out.append(separator).append(array[i]);
}
return out.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
public static String toIndentedString(Object o) {
if (o == null) return "null";
return o.toString().replace("\n", "\n ");
}
}

View File

@@ -0,0 +1,120 @@
package io.swagger.client.api;
import io.swagger.client.ApiException;
import io.swagger.client.ApiClient;
import io.swagger.client.Configuration;
import io.swagger.client.Pair;
import io.swagger.client.TypeRef;
import io.swagger.client.model.Pet;
import java.io.File;
import java.util.*;
import feign.*;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public interface PetApi extends io.swagger.client.ApiClient.Api {
/**
* Update an existing pet
*
* @param body Pet object that needs to be added to the store
* @return void
*/
@RequestLine("PUT /pet")
@Headers({
})
void updatePet (@Param("body") Pet body) throws ApiException;
/**
* Add a new pet to the store
*
* @param body Pet object that needs to be added to the store
* @return void
*/
@RequestLine("POST /pet")
@Headers({
})
void addPet (@Param("body") Pet body) throws ApiException;
/**
* Finds Pets by status
* Multiple status values can be provided with comma seperated strings
* @param status Status values that need to be considered for filter
* @return List<Pet>
*/
@RequestLine("GET /pet/findByStatus?status={status}")
@Headers({
})
List<Pet> findPetsByStatus (@Param("status") List<String> status) throws ApiException;
/**
* Finds Pets by tags
* Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
* @param tags Tags to filter by
* @return List<Pet>
*/
@RequestLine("GET /pet/findByTags?tags={tags}")
@Headers({
})
List<Pet> findPetsByTags (@Param("tags") List<String> tags) throws ApiException;
/**
* Find pet by ID
* Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
* @param petId ID of pet that needs to be fetched
* @return Pet
*/
@RequestLine("GET /pet/{petId}")
@Headers({
})
Pet getPetById (@Param("petId") Long petId) throws ApiException;
/**
* Updates a pet in the store with form data
*
* @param petId ID of pet that needs to be updated
* @param name Updated name of the pet
* @param status Updated status of the pet
* @return void
*/
@RequestLine("POST /pet/{petId}")
@Headers({
})
void updatePetWithForm (@Param("petId") String petId, @Param("name") String name, @Param("status") String status) throws ApiException;
/**
* Deletes a pet
*
* @param petId Pet id to delete
* @param apiKey
* @return void
*/
@RequestLine("DELETE /pet/{petId}")
@Headers({
"apiKey: {apiKey}"
})
void deletePet (@Param("petId") Long petId, @Param("apiKey") String apiKey) throws ApiException;
/**
* uploads an image
*
* @param petId ID of pet to update
* @param additionalMetadata Additional data to pass to server
* @param file file to upload
* @return void
*/
@RequestLine("POST /pet/{petId}/uploadImage")
@Headers({
})
void uploadFile (@Param("petId") Long petId, @Param("additionalMetadata") String additionalMetadata, @Param("file") File file) throws ApiException;
}

View File

@@ -0,0 +1,66 @@
package io.swagger.client.api;
import io.swagger.client.ApiException;
import io.swagger.client.ApiClient;
import io.swagger.client.Configuration;
import io.swagger.client.Pair;
import io.swagger.client.TypeRef;
import java.util.Map;
import io.swagger.client.model.Order;
import java.util.*;
import feign.*;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public interface StoreApi extends io.swagger.client.ApiClient.Api {
/**
* Returns pet inventories by status
* Returns a map of status codes to quantities
* @return Map<String, Integer>
*/
@RequestLine("GET /store/inventory")
@Headers({
})
Map<String, Integer> getInventory () throws ApiException;
/**
* Place an order for a pet
*
* @param body order placed for purchasing the pet
* @return Order
*/
@RequestLine("POST /store/order")
@Headers({
})
Order placeOrder (@Param("body") Order body) throws ApiException;
/**
* Find purchase order by ID
* For valid response try integer IDs with value &lt;= 5 or &gt; 10. Other values will generated exceptions
* @param orderId ID of pet that needs to be fetched
* @return Order
*/
@RequestLine("GET /store/order/{orderId}")
@Headers({
})
Order getOrderById (@Param("orderId") String orderId) throws ApiException;
/**
* Delete purchase order by ID
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
* @param orderId ID of the order that needs to be deleted
* @return void
*/
@RequestLine("DELETE /store/order/{orderId}")
@Headers({
})
void deleteOrder (@Param("orderId") String orderId) throws ApiException;
}

View File

@@ -0,0 +1,116 @@
package io.swagger.client.api;
import io.swagger.client.ApiException;
import io.swagger.client.ApiClient;
import io.swagger.client.Configuration;
import io.swagger.client.Pair;
import io.swagger.client.TypeRef;
import io.swagger.client.model.User;
import java.util.*;
import java.util.*;
import feign.*;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public interface UserApi extends io.swagger.client.ApiClient.Api {
/**
* Create user
* This can only be done by the logged in user.
* @param body Created user object
* @return void
*/
@RequestLine("POST /user")
@Headers({
})
void createUser (@Param("body") User body) throws ApiException;
/**
* Creates list of users with given input array
*
* @param body List of user object
* @return void
*/
@RequestLine("POST /user/createWithArray")
@Headers({
})
void createUsersWithArrayInput (@Param("body") List<User> body) throws ApiException;
/**
* Creates list of users with given input array
*
* @param body List of user object
* @return void
*/
@RequestLine("POST /user/createWithList")
@Headers({
})
void createUsersWithListInput (@Param("body") List<User> body) throws ApiException;
/**
* Logs user into the system
*
* @param username The user name for login
* @param password The password for login in clear text
* @return String
*/
@RequestLine("GET /user/login?username={username}&password={password}")
@Headers({
})
String loginUser (@Param("username") String username, @Param("password") String password) throws ApiException;
/**
* Logs out current logged in user session
*
* @return void
*/
@RequestLine("GET /user/logout")
@Headers({
})
void logoutUser () throws ApiException;
/**
* Get user by user name
*
* @param username The name that needs to be fetched. Use user1 for testing.
* @return User
*/
@RequestLine("GET /user/{username}")
@Headers({
})
User getUserByName (@Param("username") String username) throws ApiException;
/**
* Updated user
* This can only be done by the logged in user.
* @param username name that need to be deleted
* @param body Updated user object
* @return void
*/
@RequestLine("PUT /user/{username}")
@Headers({
})
void updateUser (@Param("username") String username, @Param("body") User body) throws ApiException;
/**
* Delete user
* This can only be done by the logged in user.
* @param username The name that needs to be deleted
* @return void
*/
@RequestLine("DELETE /user/{username}")
@Headers({
})
void deleteUser (@Param("username") String username) throws ApiException;
}

View File

@@ -0,0 +1,73 @@
package io.swagger.client.model;
import io.swagger.client.StringUtil;
import java.util.Objects;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class Category {
private Long id = null;
private String name = null;
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Category category = (Category) o;
return Objects.equals(id, category.id) &&
Objects.equals(name, category.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Category {\n");
sb.append(" id: ").append(StringUtil.toIndentedString(id)).append("\n");
sb.append(" name: ").append(StringUtil.toIndentedString(name)).append("\n");
sb.append("}");
return sb.toString();
}
}

View File

@@ -0,0 +1,153 @@
package io.swagger.client.model;
import io.swagger.client.StringUtil;
import java.util.Date;
import java.util.Objects;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class Order {
private Long id = null;
private Long petId = null;
private Integer quantity = null;
private Date shipDate = null;
public enum StatusEnum {
PLACED("placed"),
APPROVED("approved"),
DELIVERED("delivered");
private String value;
StatusEnum(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
private StatusEnum status = null;
private Boolean complete = null;
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("petId")
public Long getPetId() {
return petId;
}
public void setPetId(Long petId) {
this.petId = petId;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("quantity")
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("shipDate")
public Date getShipDate() {
return shipDate;
}
public void setShipDate(Date shipDate) {
this.shipDate = shipDate;
}
/**
* Order Status
**/
@ApiModelProperty(value = "Order Status")
@JsonProperty("status")
public StatusEnum getStatus() {
return status;
}
public void setStatus(StatusEnum status) {
this.status = status;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("complete")
public Boolean getComplete() {
return complete;
}
public void setComplete(Boolean complete) {
this.complete = complete;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Order order = (Order) o;
return 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);
}
@Override
public int hashCode() {
return Objects.hash(id, petId, quantity, shipDate, status, complete);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Order {\n");
sb.append(" id: ").append(StringUtil.toIndentedString(id)).append("\n");
sb.append(" petId: ").append(StringUtil.toIndentedString(petId)).append("\n");
sb.append(" quantity: ").append(StringUtil.toIndentedString(quantity)).append("\n");
sb.append(" shipDate: ").append(StringUtil.toIndentedString(shipDate)).append("\n");
sb.append(" status: ").append(StringUtil.toIndentedString(status)).append("\n");
sb.append(" complete: ").append(StringUtil.toIndentedString(complete)).append("\n");
sb.append("}");
return sb.toString();
}
}

View File

@@ -0,0 +1,155 @@
package io.swagger.client.model;
import io.swagger.client.StringUtil;
import io.swagger.client.model.Category;
import java.util.*;
import io.swagger.client.model.Tag;
import java.util.Objects;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class Pet {
private Long id = null;
private Category category = null;
private String name = null;
private List<String> photoUrls = new ArrayList<String>();
private List<Tag> tags = new ArrayList<Tag>();
public enum StatusEnum {
AVAILABLE("available"),
PENDING("pending"),
SOLD("sold");
private String value;
StatusEnum(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
private StatusEnum status = null;
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("category")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
/**
**/
@ApiModelProperty(required = true, value = "")
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
**/
@ApiModelProperty(required = true, value = "")
@JsonProperty("photoUrls")
public List<String> getPhotoUrls() {
return photoUrls;
}
public void setPhotoUrls(List<String> photoUrls) {
this.photoUrls = photoUrls;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("tags")
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
/**
* pet status in the store
**/
@ApiModelProperty(value = "pet status in the store")
@JsonProperty("status")
public StatusEnum getStatus() {
return status;
}
public void setStatus(StatusEnum status) {
this.status = status;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pet pet = (Pet) o;
return 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);
}
@Override
public int hashCode() {
return Objects.hash(id, category, name, photoUrls, tags, status);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Pet {\n");
sb.append(" id: ").append(StringUtil.toIndentedString(id)).append("\n");
sb.append(" category: ").append(StringUtil.toIndentedString(category)).append("\n");
sb.append(" name: ").append(StringUtil.toIndentedString(name)).append("\n");
sb.append(" photoUrls: ").append(StringUtil.toIndentedString(photoUrls)).append("\n");
sb.append(" tags: ").append(StringUtil.toIndentedString(tags)).append("\n");
sb.append(" status: ").append(StringUtil.toIndentedString(status)).append("\n");
sb.append("}");
return sb.toString();
}
}

View File

@@ -0,0 +1,73 @@
package io.swagger.client.model;
import io.swagger.client.StringUtil;
import java.util.Objects;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class Tag {
private Long id = null;
private String name = null;
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Tag tag = (Tag) o;
return Objects.equals(id, tag.id) &&
Objects.equals(name, tag.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Tag {\n");
sb.append(" id: ").append(StringUtil.toIndentedString(id)).append("\n");
sb.append(" name: ").append(StringUtil.toIndentedString(name)).append("\n");
sb.append("}");
return sb.toString();
}
}

View File

@@ -0,0 +1,164 @@
package io.swagger.client.model;
import io.swagger.client.StringUtil;
import java.util.Objects;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-12-01T16:10:23.565+08:00")
public class User {
private Long id = null;
private String username = null;
private String firstName = null;
private String lastName = null;
private String email = null;
private String password = null;
private String phone = null;
private Integer userStatus = null;
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("firstName")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("lastName")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("phone")
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
/**
* User Status
**/
@ApiModelProperty(value = "User Status")
@JsonProperty("userStatus")
public Integer getUserStatus() {
return userStatus;
}
public void setUserStatus(Integer userStatus) {
this.userStatus = userStatus;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return 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);
}
@Override
public int hashCode() {
return Objects.hash(id, username, firstName, lastName, email, password, phone, userStatus);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class User {\n");
sb.append(" id: ").append(StringUtil.toIndentedString(id)).append("\n");
sb.append(" username: ").append(StringUtil.toIndentedString(username)).append("\n");
sb.append(" firstName: ").append(StringUtil.toIndentedString(firstName)).append("\n");
sb.append(" lastName: ").append(StringUtil.toIndentedString(lastName)).append("\n");
sb.append(" email: ").append(StringUtil.toIndentedString(email)).append("\n");
sb.append(" password: ").append(StringUtil.toIndentedString(password)).append("\n");
sb.append(" phone: ").append(StringUtil.toIndentedString(phone)).append("\n");
sb.append(" userStatus: ").append(StringUtil.toIndentedString(userStatus)).append("\n");
sb.append("}");
return sb.toString();
}
}