I'm trying to create a RetrofitModule
. I have an interceptor
that adds dynamic headers using a AuthHeader
object. I'm unsure how I can pass this object to the module class.
@Module
public class RetrofitModule {
private static final String TAG = "RetrofitModule";
//Do I create a constructor here for the RetrofitModule that accepts an AuthHeader object?
@Provides
@Singleton
RequestInterceptor providesRequestInterceptor(@NonNull AuthHeader authHeader) {
return new RequestInterceptor(authHeader);
}
@Provides
@Singleton
OkHttpClient.Builder provideOkHttp() {
return new OkHttpClient.Builder();
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient.Builder okHttpClient, RequestInterceptor requestInterceptor) {
okHttpClient.addInterceptor(requestInterceptor);
HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(
HttpLoggingInterceptor.Level.BASIC
);
okHttpClient.addInterceptor(logging);
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Constants.ENDPOINT)
.client(okHttpClient.build())
.build();
}
@Provides
@Singleton
LoginService provideLoginServer(Retrofit retrofit) {
return retrofit.create(LoginService.class);
}
}
This is the AuthHeader
object I want to pass to the module so that I can add dynamic headers to the network call:
public class AuthHeader {
private String idToken;
private String idClient;
private String idEmail;
public AuthHeader(String idToken, String idClient, String idEmail) {
this.idToken = idToken;
this.idClient = idClient;
this.idEmail = idEmail;
}
public String getIdToken() {
return idToken;
}
public String getIdClient() {
return idClient;
}
public String getIdEmail() {
return idEmail;
}
}