Search in sources :

Example 91 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project apollo-android by apollographql.

the class SendOperationIdentifiersTest method operationIdHttpRequestHeader.

@Test
public void operationIdHttpRequestHeader() throws Exception {
    final HeroAndFriendsNamesQuery heroAndFriendsNamesQuery = new HeroAndFriendsNamesQuery(Input.fromNullable(EMPIRE));
    final AtomicBoolean applicationInterceptorHeader = new AtomicBoolean();
    final AtomicBoolean networkInterceptorHeader = new AtomicBoolean();
    OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (request.header("X-APOLLO-OPERATION-ID").equals(heroAndFriendsNamesQuery.operationId())) {
                applicationInterceptorHeader.set(true);
            }
            return chain.proceed(chain.request());
        }
    }).addNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (request.header("X-APOLLO-OPERATION-ID").equals(heroAndFriendsNamesQuery.operationId())) {
                networkInterceptorHeader.set(true);
            }
            return chain.proceed(chain.request());
        }
    }).build();
    ApolloClient apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).build();
    enqueueAndAssertResponse(server, "HeroAndFriendsNameResponse.json", apolloClient.query(heroAndFriendsNamesQuery), new Predicate<com.apollographql.apollo.api.Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(com.apollographql.apollo.api.Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertThat(applicationInterceptorHeader.get()).isTrue();
    assertThat(networkInterceptorHeader.get()).isTrue();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) IOException(java.io.IOException) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Response(okhttp3.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 92 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project apollo-android by apollographql.

the class ApolloCallTrackerTest method setUp.

@Before
public void setUp() throws Exception {
    activeCallCounts = new ArrayList<>();
    Interceptor interceptor = new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            activeCallCounts.add(apolloClient.activeCallsCount());
            return chain.proceed(chain.request());
        }
    };
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    apolloClient = ApolloClient.builder().serverUrl(SERVER_URL).okHttpClient(okHttpClient).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Interceptor(okhttp3.Interceptor) Before(org.junit.Before)

Example 93 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project Ency by xiarunhao123.

the class HttpModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(OkHttpClient.Builder builder, final Context context) {
    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        builder.addInterceptor(loggingInterceptor);
    }
    File cacheFile = new File(Constants.PATH_CACHE);
    Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
    Interceptor cacheInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!AppNetWorkUtil.isNetworkConnected(context)) {
                request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
            }
            Response response = chain.proceed(request);
            if (AppNetWorkUtil.isNetworkConnected(context)) {
                int maxAge = 0;
                // 有网络时, 不缓存, 最大保存时长为0
                response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
            } else {
                // 无网络时,设置超时为4周
                int maxStale = 60 * 60 * 24 * 28;
                response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
            }
            return response;
        }
    };
    // 设置缓存
    builder.addNetworkInterceptor(cacheInterceptor);
    builder.addInterceptor(cacheInterceptor);
    builder.cache(cache);
    // 设置超时
    builder.connectTimeout(10, TimeUnit.SECONDS);
    builder.readTimeout(20, TimeUnit.SECONDS);
    builder.writeTimeout(20, TimeUnit.SECONDS);
    // 错误重连
    builder.retryOnConnectionFailure(true);
    return builder.build();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) File(java.io.File) Interceptor(okhttp3.Interceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 94 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project osm-contributor by jawg.

the class FlickrPhotoUtils method getAdapter.

public static Retrofit getAdapter(final Map<String, String> oAuthParams) {
    Retrofit adapterOauth = null;
    try {
        SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(new URL(FLICKR_API_URL));
        adapterOauth = new Retrofit.Builder().addConverterFactory(ScalarsConverterFactory.create()).baseUrl(FLICKR_API_URL).client(new OkHttpClient().newBuilder().sslSocketFactory(NoSSLv3Factory).addInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request newRequest = request.newBuilder().addHeader("Authorization", FlickrSecurityUtils.getAuthorizationHeader(oAuthParams)).build();
                return chain.proceed(newRequest);
            }
        }).build()).build();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return adapterOauth;
}
Also used : MalformedURLException(java.net.MalformedURLException) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) URL(java.net.URL) NoSSLv3SocketFactory(io.jawg.osmcontributor.flickr.oauth.NoSSLv3SocketFactory) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Interceptor(okhttp3.Interceptor)

Example 95 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project osm-contributor by jawg.

the class FlickrUploadUtils method getRestAdapter.

public static Retrofit getRestAdapter(final Map<String, String> oAuthParams) {
    if (adapter == null) {
        try {
            SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(new URL("https://up.flickr.com/services"));
            adapter = new Retrofit.Builder().addConverterFactory(ScalarsConverterFactory.create()).baseUrl("https://up.flickr.com/services/").client(new okhttp3.OkHttpClient().newBuilder().sslSocketFactory(NoSSLv3Factory).addInterceptor(new Interceptor() {

                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    Request newRequest = request.newBuilder().addHeader("Authorization", FlickrSecurityUtils.getAuthorizationHeader(oAuthParams)).build();
                    return chain.proceed(newRequest);
                }
            }).build()).build();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
    }
    return adapter;
}
Also used : MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) IOException(java.io.IOException) URL(java.net.URL) NoSSLv3SocketFactory(io.jawg.osmcontributor.flickr.oauth.NoSSLv3SocketFactory) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Interceptor(okhttp3.Interceptor)

Aggregations

Interceptor (okhttp3.Interceptor)138 Request (okhttp3.Request)61 OkHttpClient (okhttp3.OkHttpClient)54 Response (okhttp3.Response)51 IOException (java.io.IOException)45 Test (org.junit.Test)29 Retrofit (retrofit2.Retrofit)27 File (java.io.File)15 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)13 Cache (okhttp3.Cache)9 HttpUrl (okhttp3.HttpUrl)8 Interceptor (com.pushtorefresh.storio3.Interceptor)7 Dispatcher (okhttp3.Dispatcher)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 X509TrustManager (javax.net.ssl.X509TrustManager)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)5 Provides (dagger.Provides)5 Singleton (javax.inject.Singleton)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 OAuth (com.arm.mbed.cloud.sdk.internal.devicedirectory.auth.OAuth)4