mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-16 08:31:26 +00:00
Java jersey2: use a shared Client instance
This commit is contained in:
@@ -45,6 +45,7 @@ import {{invokerPackage}}.auth.OAuth;
|
||||
|
||||
{{>generatedAnnotation}}
|
||||
public class ApiClient {
|
||||
private Client client;
|
||||
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
private boolean debugging = false;
|
||||
@@ -69,6 +70,8 @@ public class ApiClient {
|
||||
// Set default User-Agent.
|
||||
setUserAgent("Java-Swagger");
|
||||
|
||||
buildClient();
|
||||
|
||||
// Setup authentications (key: authentication name, value: authentication).
|
||||
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}
|
||||
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
|
||||
@@ -203,6 +206,7 @@ public class ApiClient {
|
||||
*/
|
||||
public ApiClient setDebugging(boolean debugging) {
|
||||
this.debugging = debugging;
|
||||
buildClient();
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -419,13 +423,6 @@ public class ApiClient {
|
||||
public <T> T invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, TypeRef returnType) throws ApiException {
|
||||
updateParamsForAuth(authNames, queryParams, headerParams);
|
||||
|
||||
final ClientConfig clientConfig = new ClientConfig();
|
||||
clientConfig.register(MultiPartFeature.class);
|
||||
if (debugging) {
|
||||
clientConfig.register(LoggingFilter.class);
|
||||
}
|
||||
Client client = ClientBuilder.newClient(clientConfig);
|
||||
|
||||
WebTarget target = client.target(this.basePath).path(path);
|
||||
|
||||
if (queryParams != null) {
|
||||
@@ -537,6 +534,15 @@ public class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
private void buildClient() {
|
||||
final ClientConfig clientConfig = new ClientConfig();
|
||||
clientConfig.register(MultiPartFeature.class);
|
||||
if (debugging) {
|
||||
clientConfig.register(LoggingFilter.class);
|
||||
}
|
||||
this.client = ClientBuilder.newClient(clientConfig);
|
||||
}
|
||||
|
||||
private Map<String, List<String>> buildResponseHeaders(Response response) {
|
||||
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
||||
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
||||
|
||||
Reference in New Issue
Block a user