forked from loafle/openapi-generator-original
[JAVA-VERTX] make Vertx client support running on different contexts (#6283)
* [java-vertx] make vertx client support running on different contexts * [java-vertx] make vertx client support running on different contexts (samples)
This commit is contained in:
parent
bc633766f8
commit
2ebb1bcaf0
@ -40,10 +40,11 @@ public class ApiClient {
|
||||
private static final OpenOptions FILE_DOWNLOAD_OPTIONS = new OpenOptions().setCreate(true).setTruncateExisting(true);
|
||||
|
||||
private final Vertx vertx;
|
||||
private final JsonObject config;
|
||||
private final String identifier;
|
||||
|
||||
private MultiMap defaultHeaders = MultiMap.caseInsensitiveMultiMap();
|
||||
private Map<String, Authentication> authentications;
|
||||
private WebClient webClient;
|
||||
private String basePath = "{{{basePath}}}";
|
||||
private DateFormat dateFormat;
|
||||
private ObjectMapper objectMapper;
|
||||
@ -84,9 +85,8 @@ public class ApiClient {
|
||||
// Configurations
|
||||
this.basePath = config.getString("basePath", this.basePath);
|
||||
this.downloadsDir = config.getString("downloadsDir", this.downloadsDir);
|
||||
|
||||
// Build WebClient
|
||||
this.webClient = buildWebClient(vertx, config);
|
||||
this.config = config;
|
||||
this.identifier = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public Vertx getVertx() {
|
||||
@ -102,15 +102,16 @@ public class ApiClient {
|
||||
return this;
|
||||
}
|
||||
|
||||
public WebClient getWebClient() {
|
||||
public synchronized WebClient getWebClient() {
|
||||
String webClientIdentifier = "web-client-" + identifier;
|
||||
WebClient webClient = Vertx.currentContext().get(webClientIdentifier);
|
||||
if (webClient == null) {
|
||||
webClient = buildWebClient(vertx, config);
|
||||
Vertx.currentContext().put(webClientIdentifier, webClient);
|
||||
}
|
||||
return webClient;
|
||||
}
|
||||
|
||||
public ApiClient setWebClient(WebClient webClient) {
|
||||
this.webClient = webClient;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
@ -428,7 +429,7 @@ public class ApiClient {
|
||||
}
|
||||
|
||||
HttpMethod httpMethod = HttpMethod.valueOf(method);
|
||||
HttpRequest<Buffer> request = webClient.requestAbs(httpMethod, basePath + path);
|
||||
HttpRequest<Buffer> request = getWebClient().requestAbs(httpMethod, basePath + path);
|
||||
|
||||
if (httpMethod == HttpMethod.PATCH) {
|
||||
request.putHeader("X-HTTP-Method-Override", "PATCH");
|
||||
|
@ -94,7 +94,7 @@ This endpoint does not need any parameter.
|
||||
|
||||
### Return type
|
||||
|
||||
[**Map<String, Integer>**](Map.md)
|
||||
**Map<String, Integer>**
|
||||
|
||||
### Authorization
|
||||
|
||||
|
@ -40,10 +40,11 @@ public class ApiClient {
|
||||
private static final OpenOptions FILE_DOWNLOAD_OPTIONS = new OpenOptions().setCreate(true).setTruncateExisting(true);
|
||||
|
||||
private final Vertx vertx;
|
||||
private final JsonObject config;
|
||||
private final String identifier;
|
||||
|
||||
private MultiMap defaultHeaders = MultiMap.caseInsensitiveMultiMap();
|
||||
private Map<String, Authentication> authentications;
|
||||
private WebClient webClient;
|
||||
private String basePath = "http://petstore.swagger.io:80/v2";
|
||||
private DateFormat dateFormat;
|
||||
private ObjectMapper objectMapper;
|
||||
@ -85,9 +86,8 @@ public class ApiClient {
|
||||
// Configurations
|
||||
this.basePath = config.getString("basePath", this.basePath);
|
||||
this.downloadsDir = config.getString("downloadsDir", this.downloadsDir);
|
||||
|
||||
// Build WebClient
|
||||
this.webClient = buildWebClient(vertx, config);
|
||||
this.config = config;
|
||||
this.identifier = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public Vertx getVertx() {
|
||||
@ -103,15 +103,16 @@ public class ApiClient {
|
||||
return this;
|
||||
}
|
||||
|
||||
public WebClient getWebClient() {
|
||||
public synchronized WebClient getWebClient() {
|
||||
String webClientIdentifier = "web-client-" + identifier;
|
||||
WebClient webClient = Vertx.currentContext().get(webClientIdentifier);
|
||||
if (webClient == null) {
|
||||
webClient = buildWebClient(vertx, config);
|
||||
Vertx.currentContext().put(webClientIdentifier, webClient);
|
||||
}
|
||||
return webClient;
|
||||
}
|
||||
|
||||
public ApiClient setWebClient(WebClient webClient) {
|
||||
this.webClient = webClient;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
@ -429,7 +430,7 @@ public class ApiClient {
|
||||
}
|
||||
|
||||
HttpMethod httpMethod = HttpMethod.valueOf(method);
|
||||
HttpRequest<Buffer> request = webClient.requestAbs(httpMethod, basePath + path);
|
||||
HttpRequest<Buffer> request = getWebClient().requestAbs(httpMethod, basePath + path);
|
||||
|
||||
if (httpMethod == HttpMethod.PATCH) {
|
||||
request.putHeader("X-HTTP-Method-Override", "PATCH");
|
||||
|
@ -127,6 +127,6 @@ public class AdditionalPropertiesClass {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -115,6 +115,6 @@ public class Animal {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,6 @@ public class AnimalFarm extends ArrayList<Animal> {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -96,6 +96,6 @@ public class ArrayOfArrayOfNumberOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -96,6 +96,6 @@ public class ArrayOfNumberOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -158,6 +158,6 @@ public class ArrayTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -200,6 +200,6 @@ public class Capitalization {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -87,6 +87,6 @@ public class Cat extends Animal {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -108,6 +108,6 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -86,6 +86,6 @@ public class ClassModel {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -85,6 +85,6 @@ public class Client {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -87,6 +87,6 @@ public class Dog extends Animal {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -188,6 +188,6 @@ public class EnumArrays {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -262,6 +262,6 @@ public class EnumTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -375,6 +375,6 @@ public class FormatTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -90,6 +90,6 @@ public class HasOnlyReadOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -162,6 +162,6 @@ public class MapTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -145,6 +145,6 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -109,6 +109,6 @@ public class Model200Response {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -131,6 +131,6 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -86,6 +86,6 @@ public class ModelReturn {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -137,6 +137,6 @@ public class Name {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -86,6 +86,6 @@ public class NumberOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -238,6 +238,6 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -132,6 +132,6 @@ public class OuterComposite {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -254,6 +254,6 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -99,6 +99,6 @@ public class ReadOnlyFirst {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -85,6 +85,6 @@ public class SpecialModelName {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -108,6 +108,6 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -246,6 +246,6 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user