-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add getUTxOInfo Tests * Add API Token example to Readme * New Feature #149: Allow HTTP requests with data compression * Fix AddressInfo Endpoint * Change Default Timeout Val to 300 Seconds --------- Co-authored-by: Dudi Edri <[email protected]>
- Loading branch information
Showing
9 changed files
with
151 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/rest/koios/client/backend/api/base/interceptor/GzipInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package rest.koios.client.backend.api.base.interceptor; | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import okhttp3.Headers; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import okio.GzipSource; | ||
import okio.Okio; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class GzipInterceptor implements Interceptor { | ||
@NotNull | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request.Builder newRequest = chain.request().newBuilder(); | ||
newRequest.addHeader("Accept-Encoding", "deflate, gzip"); | ||
Response response = chain.proceed(newRequest.build()); | ||
|
||
if (isGzipped(response)) { | ||
return unzip(response); | ||
} else { | ||
return response; | ||
} | ||
} | ||
|
||
private Response unzip(final Response response) throws IOException { | ||
|
||
if (response.body() == null) { | ||
return response; | ||
} | ||
|
||
GzipSource gzipSource = new GzipSource(response.body().source()); | ||
String bodyString = Okio.buffer(gzipSource).readUtf8(); | ||
|
||
ResponseBody responseBody = ResponseBody.create(bodyString, response.body().contentType()); | ||
|
||
Headers strippedHeaders = response.headers().newBuilder() | ||
.removeAll("Content-Encoding") | ||
.removeAll("Content-Length") | ||
.build(); | ||
return response.newBuilder() | ||
.headers(strippedHeaders) | ||
.body(responseBody) | ||
.message(response.message()) | ||
.build(); | ||
|
||
} | ||
|
||
private Boolean isGzipped(Response response) { | ||
return response.header("Content-Encoding") != null && Objects.equals(response.header("Content-Encoding"), "gzip"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters