Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 407 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to create different instances of the same object and access them by passing parameters to get() function in Koin?

#1
I am using Koin as a DI for my app. I created a module:
```
object NetworkModule {
fun get() = module {
single {
val authenticationInterceptor = Interceptor { chain ->
// Request customization goes here
}

OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(authenticationInterceptor) //Not all clients might have this interceptor
.build()
}

single {
Retrofit.Builder()
.baseUrl("example.com")
.client(get(/* I would like to send some paramter here */))
.addConverterFactory(GsonConverterFactory.create(get()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(Api::class.java)
}
}
}

```



How can I create different `HttpClient` or `Retrofit` instances which have different parameters set or has different instantiation? For instance, in some cases, I might need `OkHttpClient` with `AutheniticationInterceptor` and in some other cases my client might not need to use it.

Can I pass some parameters when calling `get()` so that I can get different instances? Any suggestions would be apprieciated.
Reply

#2
> You can do like below (Use koin latest version for named property).Also why I use single and factory because
>
> single— declare a singleton definition of given type. Koin keeps only one instance of this definition
>
> factory — declare a factory definition of given type. Koin gives a new
> instance each time

const val WITH_AUTH: String = "WITH_AUTH"
const val WITH_OUT_AUTH: String = "WITH_OUT_AUTH"

val remoteModule = module {
factory(named("HEADERS")) {
val map = it.get<MutableMap<String, String>>(0)
Interceptor { chain ->
val original = chain.request()
val request = original.newBuilder()
map.forEach { entry ->
request.addHeader(entry.key, entry.value)
}
chain.proceed(request.build())
}
}

factory(named("auth")) {
OkHttpClient.Builder().apply {
map["AUTHORIZATION"] = "token"

readTimeout(1, TimeUnit.MINUTES)
connectTimeout(2, TimeUnit.MINUTES)
writeTimeout(1, TimeUnit.MINUTES)
addInterceptor(get(named("HEADERS"), parameters = {
parametersOf(map)
}))
}.build()
}

factory(named("auth")) {
Retrofit.Builder()
.baseUrl("base_url")
.client(get(named("auth")))
//.addCallAdapterFactory()
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiService::class.java)
}

single(named("noAuth")) {
val map = mutableMapOf(ACCEPT to CONTENT_TYPE)
OkHttpClient.Builder().apply {
readTimeout(1, TimeUnit.MINUTES)
connectTimeout(2, TimeUnit.MINUTES)
writeTimeout(1, TimeUnit.MINUTES)
addInterceptor(get(named("HEADERS"), parameters = {
parametersOf(map)
}))

}.build()
}

single(named("noAuth")) {
Retrofit.Builder()
.baseUrl("base_url")
.client(get(named("noAuth")))
//.addCallAdapterFactory()
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiService::class.java)
}
}

> Now in your activity or viewModel

protected val apiServiceWithoutHeader: ApiService by inject(named(WITH_OUT_HEADER))
protected val apiServiceWithHeader: ApiService by inject(named(WITH_HEADER))

**with above object call appropriate API**
Reply

#3
You can use named properties - e.g.

```
single<OkHttpClient>(named("auth")){
// here you pass the version with authinterceptor
}
```

```
single<OkHttpClient>(named("noAuth")){
// here you pass the version without authinterceptor
}
```

Then in your get() method you pass the name, e.g.
```
.client(get(named("auth")))
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through