33
33
import org .springframework .web .reactive .function .client .ExchangeStrategies ;
34
34
import reactor .core .publisher .Mono ;
35
35
import reactor .core .publisher .Flux ;
36
+ import java .util .Optional ;
36
37
37
38
import java .io .BufferedReader ;
38
39
import java .io .IOException ;
@@ -76,7 +77,7 @@ private String collectionToString(Collection<? extends CharSequence> collection)
76
77
}
77
78
78
79
private HttpHeaders defaultHeaders = new HttpHeaders ();
79
-
80
+
80
81
private String basePath = "http://localhost:8090" ;
81
82
82
83
private final WebClient webClient ;
@@ -87,24 +88,30 @@ private String collectionToString(Collection<? extends CharSequence> collection)
87
88
88
89
public ApiClient () {
89
90
this .dateFormat = createDefaultDateFormat ();
90
- this .webClient = buildWebClient (new ObjectMapper (), this .dateFormat );
91
+ this .webClient = buildWebClient (new ObjectMapper (), dateFormat );
92
+ this .init ();
91
93
}
92
94
93
95
public ApiClient (ObjectMapper mapper , DateFormat format ) {
94
96
this (buildWebClient (mapper .copy (), format ), format );
95
97
}
96
98
99
+ public ApiClient (WebClient webClient , ObjectMapper mapper , DateFormat format ) {
100
+ this (Optional .ofNullable (webClient ).orElseGet (() ->buildWebClient (mapper .copy (), format )), format );
101
+ }
102
+
97
103
private ApiClient (WebClient webClient , DateFormat format ) {
98
104
this .webClient = webClient ;
99
105
this .dateFormat = format ;
106
+ this .init ();
100
107
}
101
108
102
109
public DateFormat createDefaultDateFormat () {
103
110
DateFormat dateFormat = new RFC3339DateFormat ();
104
111
dateFormat .setTimeZone (TimeZone .getTimeZone ("UTC" ));
105
112
return dateFormat ;
106
113
}
107
-
114
+
108
115
protected void init () {
109
116
// Setup authentications (key: authentication name, value: authentication).
110
117
authentications = new HashMap <String , Authentication >();
@@ -129,7 +136,7 @@ public static WebClient buildWebClient(ObjectMapper mapper, DateFormat dateForma
129
136
return webClient .build ();
130
137
}
131
138
132
-
139
+
133
140
/**
134
141
* Get the current base path
135
142
* @return String the base path
@@ -582,7 +589,7 @@ private void updateParamsForAuth(String[] authNames, MultiValueMap<String, Strin
582
589
auth .applyToParams (queryParams , headerParams );
583
590
}
584
591
}
585
-
592
+
586
593
private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
587
594
private final Log log = LogFactory .getLog (ApiClientHttpRequestInterceptor .class );
588
595
@@ -621,7 +628,7 @@ private String headersToString(HttpHeaders headers) {
621
628
builder .setLength (builder .length () - 1 ); // Get rid of trailing comma
622
629
return builder .toString ();
623
630
}
624
-
631
+
625
632
private String bodyToString (InputStream body ) throws IOException {
626
633
StringBuilder builder = new StringBuilder ();
627
634
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (body , StandardCharsets .UTF_8 ));
0 commit comments