forked from loafle/openapi-generator-original
[JAVA][Webclient] Add constructor to allow custom WebClient to be passed into ApiClient (#882)
* add constructor to allow custom webclient to be passed into ApiClient
This commit is contained in:
parent
39e9206976
commit
37fd4264ac
@ -33,6 +33,7 @@ import org.springframework.web.reactive.function.BodyInserters;
|
|||||||
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -76,7 +77,7 @@ public class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private HttpHeaders defaultHeaders = new HttpHeaders();
|
private HttpHeaders defaultHeaders = new HttpHeaders();
|
||||||
|
|
||||||
private String basePath = "{{basePath}}";
|
private String basePath = "{{basePath}}";
|
||||||
|
|
||||||
private final WebClient webClient;
|
private final WebClient webClient;
|
||||||
@ -87,20 +88,22 @@ public class ApiClient {
|
|||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
this.dateFormat = createDefaultDateFormat();
|
this.dateFormat = createDefaultDateFormat();
|
||||||
this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat);
|
this.webClient = buildWebClient(new ObjectMapper(), dateFormat);
|
||||||
|
this.init();
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApiClient(ObjectMapper mapper, DateFormat format) {
|
public ApiClient(ObjectMapper mapper, DateFormat format) {
|
||||||
this(buildWebClient(mapper.copy(), format), format);
|
this(buildWebClient(mapper.copy(), format), format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) {
|
||||||
|
this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy(), format)), format);
|
||||||
|
}
|
||||||
|
|
||||||
private ApiClient(WebClient webClient, DateFormat format) {
|
private ApiClient(WebClient webClient, DateFormat format) {
|
||||||
this.webClient = webClient;
|
this.webClient = webClient;
|
||||||
this.dateFormat = format;
|
this.dateFormat = format;
|
||||||
|
this.init();
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DateFormat createDefaultDateFormat() {
|
public DateFormat createDefaultDateFormat() {
|
||||||
@ -108,7 +111,7 @@ public class ApiClient {
|
|||||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
return dateFormat;
|
return dateFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void init() {
|
protected void init() {
|
||||||
// Setup authentications (key: authentication name, value: authentication).
|
// Setup authentications (key: authentication name, value: authentication).
|
||||||
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}
|
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}
|
||||||
@ -136,7 +139,7 @@ public class ApiClient {
|
|||||||
return webClient.build();
|
return webClient.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current base path
|
* Get the current base path
|
||||||
* @return String the base path
|
* @return String the base path
|
||||||
@ -589,7 +592,7 @@ public class ApiClient {
|
|||||||
auth.applyToParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
|
private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
|
||||||
private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class);
|
private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class);
|
||||||
|
|
||||||
@ -628,7 +631,7 @@ public class ApiClient {
|
|||||||
builder.setLength(builder.length() - 1); // Get rid of trailing comma
|
builder.setLength(builder.length() - 1); // Get rid of trailing comma
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String bodyToString(InputStream body) throws IOException {
|
private String bodyToString(InputStream body) throws IOException {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8));
|
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8));
|
||||||
|
@ -33,6 +33,7 @@ import org.springframework.web.reactive.function.BodyInserters;
|
|||||||
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -76,7 +77,7 @@ public class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private HttpHeaders defaultHeaders = new HttpHeaders();
|
private HttpHeaders defaultHeaders = new HttpHeaders();
|
||||||
|
|
||||||
private String basePath = "http://petstore.swagger.io:80/v2";
|
private String basePath = "http://petstore.swagger.io:80/v2";
|
||||||
|
|
||||||
private final WebClient webClient;
|
private final WebClient webClient;
|
||||||
@ -87,20 +88,22 @@ public class ApiClient {
|
|||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
this.dateFormat = createDefaultDateFormat();
|
this.dateFormat = createDefaultDateFormat();
|
||||||
this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat);
|
this.webClient = buildWebClient(new ObjectMapper(), dateFormat);
|
||||||
|
this.init();
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApiClient(ObjectMapper mapper, DateFormat format) {
|
public ApiClient(ObjectMapper mapper, DateFormat format) {
|
||||||
this(buildWebClient(mapper.copy(), format), format);
|
this(buildWebClient(mapper.copy(), format), format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) {
|
||||||
|
this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy(), format)), format);
|
||||||
|
}
|
||||||
|
|
||||||
private ApiClient(WebClient webClient, DateFormat format) {
|
private ApiClient(WebClient webClient, DateFormat format) {
|
||||||
this.webClient = webClient;
|
this.webClient = webClient;
|
||||||
this.dateFormat = format;
|
this.dateFormat = format;
|
||||||
|
this.init();
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DateFormat createDefaultDateFormat() {
|
public DateFormat createDefaultDateFormat() {
|
||||||
@ -108,7 +111,7 @@ public class ApiClient {
|
|||||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
return dateFormat;
|
return dateFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void init() {
|
protected void init() {
|
||||||
// Setup authentications (key: authentication name, value: authentication).
|
// Setup authentications (key: authentication name, value: authentication).
|
||||||
authentications = new HashMap<String, Authentication>();
|
authentications = new HashMap<String, Authentication>();
|
||||||
@ -137,7 +140,7 @@ public class ApiClient {
|
|||||||
return webClient.build();
|
return webClient.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current base path
|
* Get the current base path
|
||||||
* @return String the base path
|
* @return String the base path
|
||||||
@ -590,7 +593,7 @@ public class ApiClient {
|
|||||||
auth.applyToParams(queryParams, headerParams);
|
auth.applyToParams(queryParams, headerParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
|
private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
|
||||||
private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class);
|
private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class);
|
||||||
|
|
||||||
@ -629,7 +632,7 @@ public class ApiClient {
|
|||||||
builder.setLength(builder.length() - 1); // Get rid of trailing comma
|
builder.setLength(builder.length() - 1); // Get rid of trailing comma
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String bodyToString(InputStream body) throws IOException {
|
private String bodyToString(InputStream body) throws IOException {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8));
|
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user