Example usage for com.squareup.okhttp OkHttpClient interceptors

List of usage examples for com.squareup.okhttp OkHttpClient interceptors

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient interceptors.

Prototype

List interceptors

To view the source code for com.squareup.okhttp OkHttpClient interceptors.

Click Source Link

Usage

From source file:com.maxleapmobile.gitmaster.api.ApiManager.java

License:Open Source License

private ApiManager() {
    mContext = GithubApplication.getInstance();

    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
    okHttpClient.interceptors().add(new Interceptor() {
        @Override/*from  w  w  w . j a  v  a 2  s.c o  m*/
        public Response intercept(Chain chain) throws IOException {
            mAccessToken = PreferenceUtil.getString(mContext, Const.ACCESS_TOKEN_KEY, null);
            HttpUrl url;
            if (mAccessToken != null) {
                url = chain.request().httpUrl().newBuilder().addQueryParameter("access_token", mAccessToken)
                        .build();
            } else {
                url = chain.request().httpUrl();
            }

            Request request = chain.request();
            Request newRequest;
            newRequest = request.newBuilder().url(url).addHeader("User-Agent", USER_AGENT)
                    .addHeader("Accept", ACCEPT).build();
            return chain.proceed(newRequest);
        }
    });
    mRetrofit = new Retrofit.Builder().baseUrl(API_URL).client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();

    mGithubApi = mRetrofit.create(GithubApi.class);
}

From source file:com.microsoft.rest.credentials.ApplicationTokenCredentials.java

License:Open Source License

@Override
public void applyCredentialsFilter(OkHttpClient client) {
    client.interceptors().add(new ApplicationTokenCredentialsInterceptor(this));
}

From source file:com.microsoft.rest.credentials.BasicAuthenticationCredentials.java

License:Open Source License

@Override
public void applyCredentialsFilter(OkHttpClient client) {
    client.interceptors().add(new BasicAuthenticationCredentialsInterceptor(this));
}

From source file:com.microsoft.rest.credentials.TokenCredentials.java

License:Open Source License

@Override
public void applyCredentialsFilter(OkHttpClient client) {
    client.interceptors().add(new TokenCredentialsInterceptor(this));
}

From source file:com.microsoft.rest.credentials.UserTokenCredentials.java

License:Open Source License

@Override
public void applyCredentialsFilter(OkHttpClient client) {
    client.interceptors().add(new UserTokenCredentialsInterceptor(this));
}

From source file:com.mtnfog.idyl.e3.sdk.IdylE3ClientFactory.java

License:Apache License

private static RestAdapter getAuthenticatedRestAdapter(String endpoint, String apiKey,
        AuthenticationMethod method) {//from   w  w  w  .  j  ava 2s.c om

    final String requestDate = getRequestDate();

    RequestInterceptor requestInterceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {

            request.addHeader("Content-Type", "application/json");
            request.addHeader("Accept", "application/json");
            request.addHeader("x-idyle3-date", requestDate);

        }

    };

    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(new HmacSha512v1SigningInterceptor(apiKey, method, requestDate));

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint)
            .setConverter(new GsonConverter(new Gson())).setLogLevel(RestAdapter.LogLevel.FULL)
            .setRequestInterceptor(requestInterceptor).setClient(new OkClient(okHttpClient)).setLog(new Log() {

                @Override
                public void log(String message) {
                    LOGGER.debug(message);
                }

            }).build();

    return restAdapter;

}

From source file:com.netflix.spinnaker.igor.concourse.client.OkHttpClientBuilder.java

License:Apache License

public static OkHttpClient retryingClient(Supplier<Token> refreshToken) {
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(chain -> OkHttpClientBuilder.createRetryInterceptor(chain, refreshToken));
    okHttpClient.setHostnameVerifier((s, sslSession) -> true);
    okHttpClient.setSslSocketFactory(getSslContext().getSocketFactory());
    okHttpClient.setConnectTimeout(15, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
    return okHttpClient;
}

From source file:com.rowland.movies.ApplicationController.java

License:Apache License

public static Retrofit getRetrofit() {
    // Set custom date format for Gson
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    SessionRequestInterceptor sessionRequestInterceptor = new SessionRequestInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    // Our client
    OkHttpClient client = new OkHttpClient();
    // Set other interceptors
    client.networkInterceptors().add(new StethoInterceptor());
    // Set SessionRequestInterceptor instance as interceptor
    client.interceptors().add(sessionRequestInterceptor);
    // Set HttpLoggingInterceptor instance as last interceptor
    client.interceptors().add(logging);//from   ww  w  . j  ava2s  . co  m
    //To send out network requests to an API_MOVIE_URL, we need to use the Retrofit builder class
    Retrofit retrofit = new Retrofit.Builder().baseUrl(EBaseURlTypes.MOVIE_API_BASE_URL.getUrlType())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).build();
    // Wollah! Retrofit instance is served hot.
    return retrofit;
}

From source file:com.rowland.moviesquire.ApplicationController.java

License:Apache License

public static Retrofit getRetrofit() {
    // Set custom date format for Gson
    Gson gson = new GsonBuilder()
            .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
            .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    SessionRequestInterceptor sessionRequestInterceptor = new SessionRequestInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    // Our client
    OkHttpClient client = new OkHttpClient();
    // Set other interceptors
    client.networkInterceptors().add(new StethoInterceptor());
    // Set SessionRequestInterceptor instance as interceptor
    client.interceptors().add(sessionRequestInterceptor);
    // Set HttpLoggingInterceptor instance as last interceptor
    client.interceptors().add(logging);//from www .j  a v a  2  s. co  m
    //To send out network requests to an API_MOVIE_URL, we need to use the Retrofit builder class
    Retrofit retrofit = new Retrofit.Builder().baseUrl(EBaseURlTypes.MOVIE_API_BASE_URL.getUrlType())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).build();
    // Wollah! Retrofit instance is served hot.
    return retrofit;
}

From source file:com.sam_chordas.android.stockhawk.app.retofit.RestClientPublic.java

License:Apache License

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.interceptors().add(interceptor);

    client.networkInterceptors().add(new Interceptor() {
        @Override// w  ww .  ja v  a2 s  . c  o m
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.httpUrl().newBuilder()
                    .addQueryParameter(App.getGlobalContext().getString(R.string.url_query_format),
                            App.getGlobalContext().getString(R.string.url_query_json))
                    .addQueryParameter(App.getGlobalContext().getString(R.string.url_query_diagnostics),
                            App.getGlobalContext().getString(R.string.url_query_diagnostics_value))
                    .addQueryParameter(App.getGlobalContext().getString(R.string.url_query_evn),
                            App.getGlobalContext().getString(R.string.url_query_evn_value))
                    .build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);

        }
    });
    return client;
}