Skip to content

Commit

Permalink
Update Feign code
Browse files Browse the repository at this point in the history
  • Loading branch information
agoncal committed Oct 25, 2017
1 parent 0182537 commit 8400766
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,71 +1,27 @@
package org.bakingpie.book.client;

import feign.Feign;
import feign.Logger;
import feign.slf4j.Slf4jLogger;
import org.bakingpie.book.client.api.NumbersApi;

import javax.annotation.Generated;

@Generated(value = "io.swagger.codegen.languages.JavaClientCodegen")
// tag::adocSnippet[]
public class ApiClient {
// tag::adocSkip[]
public interface Api {
}
// end::adocSkip[]

private String basePath = "http://localhost:8084/number-api/api";
private Feign.Builder feignBuilder;

public ApiClient() {
feignBuilder = Feign.builder()
.logger(new Slf4jLogger());
}

public ApiClient(String[] authNames) {
this();
for (String authName : authNames) {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
}

/**
* Basic constructor for single auth name
*
* @param authName
*/
public ApiClient(String authName) {
this(new String[]{authName});
}

public String getBasePath() {
return basePath;
}

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;
}

/**
* Creates a feign client for given API interface.
* <p>
* Usage:
* ApiClient apiClient = new ApiClient();
* apiClient.setBasePath("http://localhost:8080");
* XYZApi api = apiClient.buildClient(XYZApi.class);
* XYZResponse response = api.someMethod(...);
*
* @param <T> Type
* @param clientClass Client class
* @return The Client
*/
public <T extends Api> T buildClient(Class<T> clientClass) {
return feignBuilder.target(clientClass, basePath);
public NumbersApi buildNumberApiClient() {
return Feign.builder()
.logger(new Slf4jLogger(NumbersApi.class))
.logLevel(Logger.Level.FULL)
.target(NumbersApi.class, basePath);
}
}
// end::adocSnippet[]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Generated(value = "io.swagger.codegen.languages.JavaClientCodegen")
public interface NumbersApi extends ApiClient.Api {

@RequestLine("GET http://localhost:8084/number-api/api/numbers/book")
@RequestLine("GET /numbers/book")
@Headers({"Content-Type: text/plain", "Accept: text/plain"})
String generateBookNumber();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ public Response findAll() {
})
// tag::adocSnippet[]
public Response create(@ApiParam(value = "Book to be created", required = true) Book book, @Context UriInfo uriInfo) {
// tag::adocSkip[]
log.info("Creating the book " + book);
NumbersApi numberApi = new ApiClient().buildClient(NumbersApi.class);

log.info("Invoking the number-api");
// end::adocSkip[]

NumbersApi numberApi = new ApiClient().buildNumberApiClient();
String isbn = numberApi.generateBookNumber();
book.setIsbn(isbn);

final Book created = bookRepository.create(book);
URI createdURI = uriInfo.getBaseUriBuilder().path(String.valueOf(created.getId())).build();
return Response.created(createdURI).build();
Expand Down

0 comments on commit 8400766

Please sign in to comment.