Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static com.sap.cloud.sdk.services.openapi.apache.apiclient.DefaultApiResponseHandler.isJsonMime;
import static lombok.AccessLevel.PRIVATE;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
Expand All @@ -28,6 +29,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.GZIPOutputStream;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -68,6 +70,7 @@
import lombok.Getter;
import lombok.ToString;
import lombok.With;
import lombok.val;

/**
* API client for executing HTTP requests using Apache HttpClient 5.
Expand Down Expand Up @@ -560,7 +563,11 @@ public <T> T invokeAPI(
if( body != null || !formParams.isEmpty() ) {
if( isBodyAllowed(Method.valueOf(method)) ) {
// Add entity if we have content and a valid method
builder.setEntity(serialize(body, formParams, contentTypeObj));
builder
.setEntity(
"gzip".equals(headerParams.get("Content-Encoding"))
? serializeGzip(body, contentTypeObj)
: serialize(body, formParams, contentTypeObj));
Comment on lines +568 to +570
Copy link

@ZhongpinWang ZhongpinWang Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pp] Maybe this part can be extracted into a separate function and do "switch case" for different encodings? Just for better code structure since gzip is only one of the many possible values of content encoding.

} else {
throw new OpenApiRequestException("method " + method + " does not support a request body");
}
Expand All @@ -578,4 +585,16 @@ public <T> T invokeAPI(
throw new OpenApiRequestException(e);
}
}

private HttpEntity serializeGzip( final Object body, final ContentType contentType )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pp] Some personal preference (we use pp in js team) 🙂 It could be better if this method can be merged with the already existing serialize() method by passing an additional param contentEncoding since it is the responsibility of the serialize() method to consider if data needs to be encoded / compressed.

Then maybe refactor the serialize() method as it now has 55 lines with a big if-else.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pp/req] (req for request changes, but up to you to decide) Also the current serializeGzip() cannot handle form data?

What about using the current serialize() method first, after getting the HttpEntity, check the Content-Encoding and call HttpEntity::writeTo() to write the content to OutputStream and then compress? Makes code much cleaner IMO.

{
val outputStream = new ByteArrayOutputStream();
try( val gzip = new GZIPOutputStream(outputStream) ) {
gzip.write(objectMapper.writeValueAsBytes(body));
}
catch( IOException e ) {
throw new OpenApiRequestException(e);
}
return new ByteArrayEntity(outputStream.toByteArray(), contentType.withCharset(StandardCharsets.UTF_8));
}
}