Search in sources :

Example 11 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project android-diplicity by zond.

the class RetrofitActivity method recreateServices.

protected void recreateServices() {
    AuthenticatingCallAdapterFactory adapterFactory = new AuthenticatingCallAdapterFactory();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request toIssue = chain.request().newBuilder().addHeader("Accept", "application/json; charset=UTF-8").addHeader("X-Diplicity-API-Level", "" + DIPLICITY_API_LEVEL).build();
            if (getLocalDevelopmentMode() && !getLocalDevelopmentModeFakeID().equals("")) {
                HttpUrl url = toIssue.url().newBuilder().addQueryParameter("fake-id", getLocalDevelopmentModeFakeID()).build();
                toIssue = toIssue.newBuilder().url(url).build();
            } else if (!getAuthToken().equals("")) {
                toIssue = toIssue.newBuilder().addHeader("Authorization", "bearer " + getAuthToken()).build();
            }
            Log.d("Diplicity", "" + toIssue.method() + "ing " + toIssue.url());
            return chain.proceed(toIssue);
        }
    });
    builder.connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS);
    Gson gson = new GsonBuilder().registerTypeAdapter(Ticker.class, new TickerUnserializer()).registerTypeAdapter(Game.class, new GameUnserializer(this)).create();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseURL()).addConverterFactory(GsonConverterFactory.create(gson)).addCallAdapterFactory(adapterFactory).client(builder.build()).build();
    gameService = retrofit.create(GameService.class);
    userStatsService = retrofit.create(UserStatsService.class);
    memberService = retrofit.create(MemberService.class);
    rootService = retrofit.create(RootService.class);
    variantService = retrofit.create(VariantService.class);
    optionsService = retrofit.create(OptionsService.class);
    orderService = retrofit.create(OrderService.class);
    phaseService = retrofit.create(PhaseService.class);
    channelService = retrofit.create(ChannelService.class);
    messageService = retrofit.create(MessageService.class);
    phaseResultService = retrofit.create(PhaseResultService.class);
    gameResultService = retrofit.create(GameResultService.class);
    phaseStateService = retrofit.create(PhaseStateService.class);
    gameStateService = retrofit.create(GameStateService.class);
    userConfigService = retrofit.create(UserConfigService.class);
    banService = retrofit.create(BanService.class);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) PhaseService(se.oort.diplicity.apigen.PhaseService) GameStateService(se.oort.diplicity.apigen.GameStateService) PhaseResultService(se.oort.diplicity.apigen.PhaseResultService) BanService(se.oort.diplicity.apigen.BanService) Interceptor(okhttp3.Interceptor) GsonBuilder(com.google.gson.GsonBuilder) MemberService(se.oort.diplicity.apigen.MemberService) Request(okhttp3.Request) UserConfigService(se.oort.diplicity.apigen.UserConfigService) IOException(java.io.IOException) TickerUnserializer(se.oort.diplicity.apigen.TickerUnserializer) GameResultService(se.oort.diplicity.apigen.GameResultService) UserStatsService(se.oort.diplicity.apigen.UserStatsService) HttpUrl(okhttp3.HttpUrl) MessageService(se.oort.diplicity.apigen.MessageService) PhaseStateService(se.oort.diplicity.apigen.PhaseStateService) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Game(se.oort.diplicity.apigen.Game) GameService(se.oort.diplicity.apigen.GameService) OrderService(se.oort.diplicity.apigen.OrderService)

Example 12 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project Collar by CodeZsx.

the class HttpUtils method getRetrofitWithAppKey.

private Retrofit getRetrofitWithAppKey(String baseUrl) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request r = addParam(chain.request());
            Log.i(TAG, r.method() + " " + r.url().toString());
            return chain.proceed(r);
        }

        private Request addParam(Request oldRequest) {
            if (oldRequest.method().equals("POST")) {
                return oldRequest;
            }
            HttpUrl.Builder builder = oldRequest.url().newBuilder().setEncodedQueryParameter("source", Config.APP_KEY);
            return oldRequest.newBuilder().method(oldRequest.method(), oldRequest.body()).url(builder.build()).build();
        }
    }).build();
    return new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(baseUrl).build();
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Interceptor(okhttp3.Interceptor) HttpUrl(okhttp3.HttpUrl)

Example 13 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project Collar by CodeZsx.

the class HttpUtils method getRetrofit.

private Retrofit getRetrofit(String baseUrl) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request r = addParam(chain.request());
            Log.i(TAG, r.method() + " " + r.url().toString());
            return chain.proceed(r);
        }

        private Request addParam(Request oldRequest) {
            if (oldRequest.method().equals("POST")) {
                return oldRequest;
            }
            HttpUrl.Builder builder = oldRequest.url().newBuilder().setEncodedQueryParameter("access_token", AccessTokenKeeper.getInstance().getAccessToken());
            return oldRequest.newBuilder().method(oldRequest.method(), oldRequest.body()).url(builder.build()).build();
        }
    }).build();
    return new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(baseUrl).build();
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Interceptor(okhttp3.Interceptor) HttpUrl(okhttp3.HttpUrl)

Example 14 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project BestPracticeApp by pop1234o.

the class MainActivity method requestOkHttp.

/**
 * 缓存
 */
private void requestOkHttp() {
    new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            okhttp3.Request request = chain.request();
            // chain.request().newBuilder().addHeader().build()
            okhttp3.Response response = chain.proceed(request);
            return response;
        }
    }).cache(new okhttp3.Cache(getCacheDir(), 5 * 1024 * 1024)).build();
    okhttp3.Request request_forceNocache = new okhttp3.Request.Builder().cacheControl(new CacheControl.Builder().noCache().build()).url("").build();
    okhttp3.Request request_forceCache = new okhttp3.Request.Builder().cacheControl(new CacheControl.Builder().maxAge(0, TimeUnit.SECONDS).build()).url("").build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) StringRequest(com.android.volley.toolbox.StringRequest) Request(com.android.volley.Request) IOException(java.io.IOException) Response(com.android.volley.Response) CacheControl(okhttp3.CacheControl) Interceptor(okhttp3.Interceptor) Cache(com.android.volley.Cache) DiskBasedCache(com.android.volley.toolbox.DiskBasedCache)

Example 15 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project crnk-framework by crnk-project.

the class OkHttpBraveIntegration method onBuild.

@Override
public void onBuild(OkHttpClient.Builder builder) {
    Interceptor interceptor = TracingInterceptor.create(httpTracing);
    builder.addNetworkInterceptor(interceptor);
    builder.dispatcher(new Dispatcher(httpTracing.tracing().currentTraceContext().executorService(new Dispatcher().executorService())));
}
Also used : Dispatcher(okhttp3.Dispatcher) Interceptor(okhttp3.Interceptor) TracingInterceptor(brave.okhttp3.TracingInterceptor)

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