-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
19 additions
and
57 deletions.
There are no files selected for viewing
66 changes: 11 additions & 55 deletions
66
demo04-invoking/services/book-api/src/main/java/org/bakingpie/book/client/ApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters