-
Notifications
You must be signed in to change notification settings - Fork 6
/
RetrofitModule.kt
69 lines (59 loc) · 1.92 KB
/
RetrofitModule.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ir.heydarii.musicmanager.base.di
import dagger.Module
import dagger.Provides
import ir.heydarii.musicmanager.retrofit.RetrofitMainInterface
import ir.heydarii.musicmanager.retrofit.RetrofitServiceGenerator
import ir.heydarii.musicmanager.utils.Consts
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton
@Module
class RetrofitModule {
@Singleton
@Provides
fun provideRetrofit(
converter: GsonConverterFactory,
httpClient: OkHttpClient.Builder,
@Named("baseURL") baseURL: String
): Retrofit {
val retrofitClass = RetrofitServiceGenerator(converter, httpClient, baseURL)
return retrofitClass.getClient()
}
@Singleton
@Provides
fun provideOkHttp(interceptor: HttpLoggingInterceptor): OkHttpClient.Builder {
val httpClient = OkHttpClient().newBuilder()
httpClient.connectTimeout(15, TimeUnit.SECONDS)
httpClient.readTimeout(15, TimeUnit.SECONDS)
httpClient.callTimeout(15, TimeUnit.SECONDS)
httpClient.addInterceptor(interceptor)
return httpClient
}
@Singleton
@Provides
fun provedHttpLoggingInterceptor(): HttpLoggingInterceptor {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
}
@Singleton
@Provides
fun provideGsonConverterFactory(): GsonConverterFactory {
return GsonConverterFactory.create()
}
@Singleton
@Provides
@Named("baseURL")
fun provideBaseURL(): String {
return Consts.BASE_URL
}
@Singleton
@Provides
fun getMainInterface(retrofit: Retrofit): RetrofitMainInterface {
return retrofit.create(RetrofitMainInterface::class.java)
}
}