Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Module
open class ApplicationModule(private val application: GDelegate) {
@Provides
@Singleton
fun giveContext(): Context = this.application
@Provides
@Singleton
fun givePackageManager(): PackageManager = application.packageManager
@Provides
@Singleton
open fun giveLoggingInterceptor(): HttpLoggingInterceptor {
val interceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
GLog.v("APP LOGG", message)
})
interceptor.level = if (GLog.DEBUG_BOOL)
HttpLoggingInterceptor.Level.BODY
else
HttpLoggingInterceptor.Level.NONE
return interceptor
}
// okHttpClient
@Provides
open fun giveOkHttpClient(
loggingInterceptor: HttpLoggingInterceptor
): OkHttpClient {
val httpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
// for testing
// .addInterceptor { chain ->
// Response.Builder()
// .code(400)
// .message("{}")
// .request(chain.request())
// .protocol(Protocol.HTTP_1_0)
// .body(ResponseBody.create(MediaType.parse("application/json"), "{}".toByteArray()))
// .build()
// }
// increasing time outs
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
return httpClient.build()
}
@Provides
fun giveRetrofitBuilder(): Retrofit.Builder =
Retrofit.Builder()
.baseUrl(ConstantsUtil.API_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
@Provides
open fun giveRetrofitService(okHttpClient: OkHttpClient): AppAPIs =
Retrofit.Builder()
.baseUrl(ConstantsUtil.API_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
.create(AppAPIs::class.java)
@Provides
@Named("test")
open fun giveAppAPIs(): AppAPIs =
Retrofit.Builder().build().create(AppAPIs::class.java)
@Provides
@Singleton
fun giveGSONInstance(): Gson = Gson()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.