Skip to content

Instantly share code, notes, and snippets.

@Audhil
Last active August 1, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Audhil/761df96041c5cb0446b9ed171257f709 to your computer and use it in GitHub Desktop.
Save Audhil/761df96041c5cb0446b9ed171257f709 to your computer and use it in GitHub Desktop.
@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