Search in sources :

Example 21 with Interceptor

use of okhttp3.Interceptor in project android-oss by kickstarter.

the class ApplicationModule method provideHttpLoggingInterceptor.

@Provides
@Singleton
@NonNull
HttpLoggingInterceptor provideHttpLoggingInterceptor() {
    final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    return interceptor;
}
Also used : HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) NonNull(android.support.annotation.NonNull) Provides(dagger.Provides)

Example 22 with Interceptor

use of okhttp3.Interceptor in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.

@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
    // Make mock response
    Buffer buffer = new Buffer();
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write("content".getBytes());
    gzipOut.close();
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
    // Start mock server
    server.enqueue(mockResponse);
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    final Semaphore done = new Semaphore(0);
    // Add plain interceptor to disable decompress response stream
    client.addExternalInterceptor(new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
            // Make sure the response we get from the interceptor is the raw gzip stream
            byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
            assertArrayEquals(byteOut.toByteArray(), content);
            // We need to set a new stream since we have read it
            return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
        }
    });
    // We do not need to add Accept-Encoding header manually, httpClient library should do that.
    String requestUrl = server.url("/").toString();
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);
    // Make sure the response we get is ungziped by OkHttp library
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals("content".getBytes(), content);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
    server.shutdown();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 23 with Interceptor

use of okhttp3.Interceptor in project BigImageViewer by Piasy.

the class GlideProgressSupport method createInterceptor.

private static Interceptor createInterceptor(final ResponseProgressListener listener) {
    return new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            return response.newBuilder().body(new OkHttpProgressResponseBody(request.url(), response.body(), listener)).build();
        }
    };
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Interceptor(okhttp3.Interceptor)

Example 24 with Interceptor

use of okhttp3.Interceptor in project zipkin by openzipkin.

the class TraceZipkinElasticsearchHttpStorageAutoConfiguration method elasticsearchOkHttpClientBuilder.

@Bean
@Qualifier("zipkinElasticsearchHttp")
@ConditionalOnMissingBean
OkHttpClient.Builder elasticsearchOkHttpClientBuilder() {
    // have to indirect to unwind a circular dependency
    Interceptor tracingInterceptor = new Interceptor() {

        Interceptor delegate = BraveTracingInterceptor.builder(brave).serverName("elasticsearch").build();

        @Override
        public Response intercept(Chain chain) throws IOException {
            // Only join traces, don't start them. This prevents LocalCollector's thread from amplifying.
            if (brave.serverSpanThreadBinder().getCurrentServerSpan() != null && brave.serverSpanThreadBinder().getCurrentServerSpan().getSpan() != null) {
                return delegate.intercept(chain);
            }
            return chain.proceed(chain.request());
        }
    };
    BraveExecutorService tracePropagatingExecutor = BraveExecutorService.wrap(new Dispatcher().executorService(), brave);
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(tracingInterceptor);
    builder.addNetworkInterceptor(tracingInterceptor);
    builder.dispatcher(new Dispatcher(tracePropagatingExecutor));
    return builder;
}
Also used : BraveExecutorService(com.github.kristofa.brave.BraveExecutorService) OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher) Interceptor(okhttp3.Interceptor) BraveTracingInterceptor(com.github.kristofa.brave.okhttp.BraveTracingInterceptor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 25 with Interceptor

use of okhttp3.Interceptor in project Tusky by Vavassor.

the class OkHttpUtils method getUserAgentInterceptor.

/**
     * Add a custom User-Agent that contains Tusky & Android Version to all requests
     * Example:
     * User-Agent: Tusky/1.1.2 Android/5.0.2
     */
@NonNull
private static Interceptor getUserAgentInterceptor() {
    return new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Request requestWithUserAgent = originalRequest.newBuilder().header("User-Agent", "Tusky/" + BuildConfig.VERSION_NAME + " Android/" + Build.VERSION.RELEASE).build();
            return chain.proceed(requestWithUserAgent);
        }
    };
}
Also used : Request(okhttp3.Request) Interceptor(okhttp3.Interceptor) NonNull(android.support.annotation.NonNull)

Aggregations

IOException (java.io.IOException)37 Interceptor (okhttp3.Interceptor)36 Request (okhttp3.Request)29 MockResponse (okhttp3.mockwebserver.MockResponse)29 Response (okhttp3.Response)25 Test (org.junit.Test)25 OkHttpClient (okhttp3.OkHttpClient)24 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)24 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)9 File (java.io.File)8 Retrofit (retrofit2.Retrofit)8 Provides (dagger.Provides)6 Singleton (javax.inject.Singleton)6 Cache (okhttp3.Cache)5 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)3 InterruptedIOException (java.io.InterruptedIOException)3 Call (okhttp3.Call)3 HttpUrl (okhttp3.HttpUrl)3 NonNull (android.support.annotation.NonNull)2 ANResponse (com.androidnetworking.common.ANResponse)2