[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:
Hugo Barrigas
2018-08-25 14:22:36 +01:00
committed by Jérémie Bresson
parent 39e9206976
commit 37fd4264ac
2 changed files with 26 additions and 20 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Flux;
import java.util.Optional;
import java.io.BufferedReader;
import java.io.IOException;
@@ -76,7 +77,7 @@ public class ApiClient {
}
private HttpHeaders defaultHeaders = new HttpHeaders();
private String basePath = "http://petstore.swagger.io:80/v2";
private final WebClient webClient;
@@ -87,20 +88,22 @@ public class ApiClient {
public ApiClient() {
this.dateFormat = createDefaultDateFormat();
this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat);
init();
this.webClient = buildWebClient(new ObjectMapper(), dateFormat);
this.init();
}
public ApiClient(ObjectMapper mapper, DateFormat 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) {
this.webClient = webClient;
this.dateFormat = format;
init();
this.init();
}
public DateFormat createDefaultDateFormat() {
@@ -108,7 +111,7 @@ public class ApiClient {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
}
protected void init() {
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
@@ -137,7 +140,7 @@ public class ApiClient {
return webClient.build();
}
/**
* Get the current base path
* @return String the base path
@@ -590,7 +593,7 @@ public class ApiClient {
auth.applyToParams(queryParams, headerParams);
}
}
private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
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
return builder.toString();
}
private String bodyToString(InputStream body) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8));