Java tutorial
/* The MIT License (MIT) * Copyright (c) 2016 BELATRIX * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.belatrixsf.allstars.utils.di.modules; import com.belatrixsf.allstars.BuildConfig; import com.belatrixsf.allstars.managers.PreferencesManager; import com.belatrixsf.allstars.networking.retrofit.api.CategoryAPI; import com.belatrixsf.allstars.networking.retrofit.api.EmployeeAPI; import com.belatrixsf.allstars.networking.retrofit.api.StarAPI; import com.google.gson.Gson; import com.squareup.okhttp.Interceptor; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import com.squareup.okhttp.logging.HttpLoggingInterceptor; import java.io.IOException; import javax.inject.Singleton; import dagger.Module; import dagger.Provides; import retrofit.GsonConverterFactory; import retrofit.Retrofit; /** * Created by gyosida on 4/12/16. */ @Module public class RetrofitModule { @Singleton @Provides public Retrofit provideRetrofit() { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient(); client.interceptors().add(loggingInterceptor); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String token = PreferencesManager.get().getToken(); if (token != null) { Request request = chain.request().newBuilder().addHeader("Authorization", "Token " + token) .build(); return chain.proceed(request); } return chain.proceed(chain.request()); } }); return new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()) .baseUrl(BuildConfig.BASE_URL).client(client).build(); } @Provides public EmployeeAPI provideUserAPI(Retrofit retrofit) { return retrofit.create(EmployeeAPI.class); } @Provides public StarAPI providesStarAPI(Retrofit retrofit) { return retrofit.create(StarAPI.class); } @Provides public CategoryAPI provideCategoryAPI(Retrofit retrofit) { return retrofit.create(CategoryAPI.class); } }