-
Notifications
You must be signed in to change notification settings - Fork 25
feat: [OpenAPI] GZIP encoding #1110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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. | ||
|
|
@@ -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)); | ||
| } else { | ||
| throw new OpenApiRequestException("method " + method + " does not support a request body"); | ||
| } | ||
|
|
@@ -578,4 +585,16 @@ public <T> T invokeAPI( | |
| throw new OpenApiRequestException(e); | ||
| } | ||
| } | ||
|
|
||
| private HttpEntity serializeGzip( final Object body, final ContentType contentType ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Then maybe refactor the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 What about using the current |
||
| { | ||
| 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)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
gzipis only one of the many possible values of content encoding.