Skip to content
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

[Java][okhttp4-gson] Fix adding/removing interceptors to OkHttpClient in OkHttp3 #1336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -80,7 +80,7 @@ public class ApiClient {

{{#useGzipFeature}}
// Enable gzip request compression
httpClient.interceptors().add(new GzipRequestInterceptor());
setHttpClient(getHttpClient.newBuilder().interceptors().add(new GzipRequestInterceptor()).build());
{{/useGzipFeature}}

json = new JSON();
Expand Down Expand Up @@ -338,9 +338,9 @@ public class ApiClient {
if (debugging) {
loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(Level.BODY);
httpClient.interceptors().add(loggingInterceptor);
setHttpClient(getHttpClient.newBuilder().interceptors().add(logginInterceptor).build());
} else {
httpClient.interceptors().remove(loggingInterceptor);
setHttpClient(getHttpClient.newBuilder().interceptors().remove(logginInterceptor).build());
loggingInterceptor = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ public class {{classname}} {
{{localVariablePrefix}}localVarHeaderParams.put("Content-Type", {{localVariablePrefix}}localVarContentType);

if(progressListener != null) {
{{localVariablePrefix}}apiClient.getHttpClient().networkInterceptors().add(new okhttp3.Interceptor() {
okhttp3.Interceptor interceptor = new okhttp3.Interceptor() {
@Override
public okhttp3.Response intercept(okhttp3.Interceptor.Chain chain) throws IOException {
okhttp3.Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), progressListener))
.build();
}
});
};
okhttp3.OkHttpClient newClient = {{localVariablePrefix}}apiClient.getHttpClient().newBuilder().addNetworkInterceptor(interceptor).build();
{{localVariablePrefix}}apiClient.setHttpClient(newClient);
}

String[] {{localVariablePrefix}}localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
Expand Down