Hovhannes
10/01/2021, 5:59 AM@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {
@Provides
fun provideBuildlogger(): HttpLoggingInterceptor {
return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}
@Provides
@Singleton
fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
} else {
OkHttpClient
.Builder()
.build()
}
@Provides
@Singleton
@Named("base")
fun provideRetrofitBuilder(
alwaysonUrl: String, okHttpClient: OkHttpClient
): Retrofit {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create().asLenient())
.baseUrl(alwaysonUrl)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
@Named("vbtop")
fun provideRetrofitBuilderVbttop(
@Named("vbttop") vbttopUrl: String, okHttpClient: OkHttpClient
): Retrofit {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(vbttopUrl)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
fun provideApi(@Named("base") retrofit: Retrofit): UrlApi {
return retrofit.create(UrlApi::class.java)
}
@Provides
@Singleton
fun provideApiVbtop(@Named("vbttop") retrofit: Retrofit): VbtopApi {
return retrofit.create(VbtopApi::class.java)
}
@Provides
fun provideBase() = BuildConfig.BASE_URL
@Provides
@Named("vbttop")
fun vbttopUrl(): String = ""
}
interface UrlApi {
@GET
suspend fun getUrls(@Url path: String): Response<JsonData>
}
interface VbtopApi {
@GET
suspend fun getVbtop(@Url url:String): Response<VbtopUrl>
}
class MainRepository @Inject constructor(private val urlApi: UrlApi, private val vbtopApi: VbtopApi) {
suspend fun getUrls(path:String) = urlApi.getUrls(path)
suspend fun getVbtop(url:String)=vbtopApi.getVbtop(url)
}
ephemient
10/01/2021, 6:10 AMclass MainRepository @Inject constructor(
private val urlApi: UrlApi,
@Named("vbtop")
private val vbtopApi: VbtopApi
)
Hovhannes
10/01/2021, 6:14 AMephemient
10/01/2021, 6:46 AMHovhannes
10/01/2021, 7:42 AMRafal
10/01/2021, 11:11 AMsuspend fun getVbtop(@Url url:String)
(you don’t add any other methods that will require a base url), you can simply provide anything like <https://whatever.com/>
and the @Url
param will override it@Provides
@Named("vbttop")
fun vbttopUrl(): String = "<https://whatever.com/>"
should be enough