Skip to content

Commit d434b5c

Browse files
committed
regenerated samples
1 parent 4d20a06 commit d434b5c

File tree

27 files changed

+2289
-1210
lines changed

27 files changed

+2289
-1210
lines changed

samples/client/echo_api/java/native/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openapitools.jackson.nullable.JsonNullableModule;
2121

2222
import java.io.InputStream;
23+
import java.io.IOException;
2324
import java.net.URI;
2425
import java.net.URLEncoder;
2526
import java.net.http.HttpClient;
@@ -34,6 +35,8 @@
3435
import java.util.List;
3536
import java.util.StringJoiner;
3637
import java.util.function.Consumer;
38+
import java.util.Optional;
39+
import java.util.zip.GZIPInputStream;
3740
import java.util.stream.Collectors;
3841

3942
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -62,7 +65,7 @@ public class ApiClient {
6265
protected String basePath;
6366
protected Consumer<HttpRequest.Builder> interceptor;
6467
protected Consumer<HttpResponse<InputStream>> responseInterceptor;
65-
protected Consumer<HttpResponse<String>> asyncResponseInterceptor;
68+
protected Consumer<HttpResponse<InputStream>> asyncResponseInterceptor;
6669
protected Duration readTimeout;
6770
protected Duration connectTimeout;
6871

@@ -384,7 +387,7 @@ public Consumer<HttpResponse<InputStream>> getResponseInterceptor() {
384387
* of null resets the interceptor to a no-op.
385388
* @return This object.
386389
*/
387-
public ApiClient setAsyncResponseInterceptor(Consumer<HttpResponse<String>> interceptor) {
390+
public ApiClient setAsyncResponseInterceptor(Consumer<HttpResponse<InputStream>> interceptor) {
388391
this.asyncResponseInterceptor = interceptor;
389392
return this;
390393
}
@@ -394,7 +397,7 @@ public ApiClient setAsyncResponseInterceptor(Consumer<HttpResponse<String>> inte
394397
*
395398
* @return The custom interceptor that was set, or null if there isn't any.
396399
*/
397-
public Consumer<HttpResponse<String>> getAsyncResponseInterceptor() {
400+
public Consumer<HttpResponse<InputStream>> getAsyncResponseInterceptor() {
398401
return asyncResponseInterceptor;
399402
}
400403

@@ -454,4 +457,32 @@ public ApiClient setConnectTimeout(Duration connectTimeout) {
454457
public Duration getConnectTimeout() {
455458
return connectTimeout;
456459
}
460+
461+
/**
462+
* Returns the response body InputStream, transparently decoding gzip-compressed
463+
* payloads when the server sets {@code Content-Encoding: gzip}.
464+
*
465+
* @param response HTTP response whose body should be consumed
466+
* @return Original or decompressed InputStream for the response body
467+
* @throws IOException if the response body cannot be accessed or wrapping fails
468+
*/
469+
public static InputStream getResponseBody(HttpResponse<InputStream> response) throws IOException {
470+
if (response == null) {
471+
return null;
472+
}
473+
InputStream body = response.body();
474+
if (body == null) {
475+
return null;
476+
}
477+
Optional<String> encoding = response.headers().firstValue("Content-Encoding");
478+
if (encoding.isPresent()) {
479+
for (String token : encoding.get().split(",")) {
480+
if ("gzip".equalsIgnoreCase(token.trim())) {
481+
return new GZIPInputStream(body);
482+
}
483+
}
484+
}
485+
return body;
486+
}
487+
457488
}

samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/AuthApi.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static HttpRequest.Builder withAdditionalHeaders(HttpRequest.Builder builder, Ma
7373
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
7474
private final Duration memberVarReadTimeout;
7575
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
76-
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
76+
private final Consumer<HttpResponse<InputStream>> memberVarAsyncResponseInterceptor;
7777

7878
public AuthApi() {
7979
this(Configuration.getDefaultApiClient());
@@ -91,7 +91,15 @@ public AuthApi(ApiClient apiClient) {
9191

9292

9393
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
94-
String body = response.body() == null ? null : new String(response.body().readAllBytes());
94+
InputStream responseBody = ApiClient.getResponseBody(response);
95+
String body = null;
96+
try {
97+
body = responseBody == null ? null : new String(responseBody.readAllBytes());
98+
} finally {
99+
if (responseBody != null) {
100+
responseBody.close();
101+
}
102+
}
95103
String message = formatExceptionMessage(operationId, response.statusCode(), body);
96104
return new ApiException(response.statusCode(), message, response.headers(), body);
97105
}
@@ -110,10 +118,13 @@ private String formatExceptionMessage(String operationId, int statusCode, String
110118
* @return File
111119
* @throws ApiException If fail to read file content from response and write to disk
112120
*/
113-
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
121+
public File downloadFileFromResponse(HttpResponse<InputStream> response, InputStream responseBody) throws ApiException {
122+
if (responseBody == null) {
123+
throw new ApiException(new IOException("Response body is empty"));
124+
}
114125
try {
115126
File file = prepareDownloadFile(response);
116-
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
127+
java.nio.file.Files.copy(responseBody, file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
117128
return file;
118129
} catch (IOException e) {
119130
throw new ApiException(e);
@@ -199,14 +210,16 @@ public ApiResponse<String> testAuthHttpBasicWithHttpInfo(Map<String, String> hea
199210
if (memberVarResponseInterceptor != null) {
200211
memberVarResponseInterceptor.accept(localVarResponse);
201212
}
213+
InputStream localVarResponseBody = null;
202214
try {
203215
if (localVarResponse.statusCode()/ 100 != 2) {
204216
throw getApiException("testAuthHttpBasic", localVarResponse);
205217
}
206218
// for plain text response
207219
if (localVarResponse.headers().map().containsKey("Content-Type") &&
208220
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0).split(";")[0].trim())) {
209-
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
221+
localVarResponseBody = ApiClient.getResponseBody(localVarResponse);
222+
java.util.Scanner s = new java.util.Scanner(localVarResponseBody == null ? InputStream.nullInputStream() : localVarResponseBody).useDelimiter("\\A");
210223
String responseBodyText = s.hasNext() ? s.next() : "";
211224
return new ApiResponse<String>(
212225
localVarResponse.statusCode(),
@@ -217,6 +230,9 @@ public ApiResponse<String> testAuthHttpBasicWithHttpInfo(Map<String, String> hea
217230
throw new RuntimeException("Error! The response Content-Type is supposed to be `text/plain` but it's not: " + localVarResponse);
218231
}
219232
} finally {
233+
if (localVarResponseBody != null) {
234+
localVarResponseBody.close();
235+
}
220236
}
221237
} catch (IOException e) {
222238
throw new ApiException(e);
@@ -297,14 +313,16 @@ public ApiResponse<String> testAuthHttpBearerWithHttpInfo(Map<String, String> he
297313
if (memberVarResponseInterceptor != null) {
298314
memberVarResponseInterceptor.accept(localVarResponse);
299315
}
316+
InputStream localVarResponseBody = null;
300317
try {
301318
if (localVarResponse.statusCode()/ 100 != 2) {
302319
throw getApiException("testAuthHttpBearer", localVarResponse);
303320
}
304321
// for plain text response
305322
if (localVarResponse.headers().map().containsKey("Content-Type") &&
306323
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0).split(";")[0].trim())) {
307-
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
324+
localVarResponseBody = ApiClient.getResponseBody(localVarResponse);
325+
java.util.Scanner s = new java.util.Scanner(localVarResponseBody == null ? InputStream.nullInputStream() : localVarResponseBody).useDelimiter("\\A");
308326
String responseBodyText = s.hasNext() ? s.next() : "";
309327
return new ApiResponse<String>(
310328
localVarResponse.statusCode(),
@@ -315,6 +333,9 @@ public ApiResponse<String> testAuthHttpBearerWithHttpInfo(Map<String, String> he
315333
throw new RuntimeException("Error! The response Content-Type is supposed to be `text/plain` but it's not: " + localVarResponse);
316334
}
317335
} finally {
336+
if (localVarResponseBody != null) {
337+
localVarResponseBody.close();
338+
}
318339
}
319340
} catch (IOException e) {
320341
throw new ApiException(e);

0 commit comments

Comments
 (0)