last day (882 days later) » 

12:50 PM
room topic changed to Android Help: Helping Random users with Android Stuff ;) [android]
 
1:03 PM
Hello @MartinMarconcini, I am the one who messaged on twitter
 
Hey
Thanks for joining here, I think it's a little bit easier.
 
okay, thanks
 
Ok, so to check on what you're doing. You want to have two separate instances of an OkHttp client, where you have a different set of interceptor(s) for each one
Is that correct?
(can you post the code you sent me on Twitter here?) Like, not the image, the actual copy/paste so it's easier to read?
 
yes, one would have a logging interceptor alone while the other would have both the logging interceptor and interceptor for passing headers
 
so if you do in your module:
wait, this interface... doesn't support the same Markdown :D
single { named("clientA") { OkHttpClient.Builder().addInterceptor(...).build() }
and
single { named("clientB") { OkHttpClient.Builder().addInterceptor(...).addInterceptor(...).build() }
and then you get these as:
single<YourRetrofitForExample> { val okhttpClient = get<OkHttpClient>(named("clientB")) val retrofit = Retrofit.builder().client(okHttpClient)....build() }
(sorry, it won't let me multi-line format)
and then say you had another place where you want to use the other client...
single<SomethingElseThatUsesTheOtherClient> { val okhttpClient = get<OkHttpClient>(named("clientA")) val something = ... }
notice one calls get<TYPE>(named("clientA")) and the other get<TYPE>(named("clientB"))
 
1:22 PM
Okay, thank you, i am implementing this
 
1:36 PM
I am getting an error of `definition [Apiservice] trying to override another [Apiservice]
I added (override=true) to each instance of apiservice but did not fix it
single {
val okHttpClientWithAuth = get<OkHttpClient>(named("auth"))
provideApiServiceWithAuth(okHttpClientWithAuth)
}
single{
val okHttpClientWithoutAuth = get<OkHttpClient>(named("noAuth"))
provideApiServiceWithoutAuth(okHttpClientWithoutAuth)
}
 
the thing is those two are returning the same type (OkHttpClient)
so you need to name them too I believe.
change it to something like single(named("api1")) {... } and single(named"api2")) {...} I think.
Both return an apiClient (if I am not mistaken from your image) so Koin can't tell the difference between the two
like so:
single(named("AuthApi")) {
val okHttpClientWithAuth = get<OkHttpClient>(named("auth"))
provideApiServiceWithAuth(okHttpClientWithAuth)
}

single(named("noAuthApi")) {
val okHttpClientWithoutAuth = get<OkHttpClient>(named("noAuth"))
provideApiServiceWithoutAuth(okHttpClientWithoutAuth)
}
then you have to decide which one you want.
now, if what you want is the same "api service" with a different OkHttp client depending on the existence of the token...
you're going to need to abstract that away. That's not "koin's" responsibility... you could do something like
single<ApiService> {
val hasToken = get<YourAuthTokenRepo>().hasToken()

if (hasToken) {
val ... = get<OkHttpClient>(named("auth"))
provide...
} else {
val ... = get<OkHttpClient>(named("noAuth"))
provide...
}
}
something like that.
 
adding a name to both provideApiServiceWithAuth and provideApiServiceWithoutAuth breaks the app. I think I have to say also that I am passing both instances in my view model (SharedViewModel) thats why I have SharedViewModel(get(), get())
 
Yeah, I saw that, why do you pass both though?
are you going to use both at the same time?
 
yeah in two different functions declared in the view model, one to call the apiservice that needs an auth header and another to call one that does not need an auth headrer
 
Hi, how are you everyone?
 
1:50 PM
I don't think that this particular ViewModel should be in charge of making that decision.
Hello Rakesh ;)
 
How you guys doing?
You guys started work on flutter?
 
Whether your API service needs an specific header or not, shouldn't be the ViewModel's concern at all. That's your Repository's problem.
 
API you should be on Repository, while making network request enable the header and past the response on Respository to ViewModel
 
@RakeshKumar I created the room to help @letmebe with a problem, you're more than welcome to stay and participate, but if you want to talk about general Android stuff, there's an Android room with more people in it ;)
 
@MartinMarconcini, I am already in that room :)
 
1:53 PM
Great ;)
 
I thought you guys work on flutter. I suppose to ask about it
 
It's all right.
 
So you are saying I should abstract it into a repository and pass it in the constructor of the viewModel instead
 
Here's what I think I'd do:
1. Have a custom HeaderInterceptor(private val somethingHere) : Interceptor {}
What is soemthingHere ? whatever you have that knows if you need a token or not.
Have that as your interceptor (always)
and in it, override the intercept(...) method from the Intercept interface.
and decide whether you want to add the session headers or not.
Then neither your HttpClient, nor your ViewModel, nor anybody really knows much about this. It will be added when the logic in your custom interceptor decides.
Again, i don't have your problem/code/domain in front of me, so I'm not sure if this is what you should do, but at first glance, I would absolutely abstract this away into a repo or whatever use-case you have for this API service.
this is how it would look (pseudo-code)
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val requestBuilder = originalRequest.newBuilder()


// Add headers if needed
val headers = ...
for (header in headers) {
requestBuilder.header(header.key, header.value)
}

// build the new request
val currentRequest = requestBuilder.build()
return chain.proceed(currentRequest)
}
so now your interceptor will either add headers (if needed) or simply let the chain proceed. (you can tweak the method at your own convenience, but you should get the idea)
Now you've kicked the problem elsewhere. The question you now have to solve is: how do I know when somethingHere returns headers or not... that's tied to your own solution/domain.
In the last app I worked for we had a SessionInfo object with a lot of stuff that belonged on the session. Among those things was a var headers: MutableMap<String, String> = mutableMapOf() and we used that in the interceptor to add the headers. We maintained that map inside SessionIfnro when the user logged in, etc.
So our HeaderInterceptor looked like: class HeaderInterceptor(private val sessionInfo: SessionInfo) : Interceptor {
and this is how we added "custom" headers (all inside the intercept(...) fun...)
sessionInfo.config?.clientKey?.let { requestBuilder.header(HEADER_APP_KEY, it) }
sessionInfo.csrfToken?.let { requestBuilder.header(HEADER_CSRF_KEY, it) }
two different "dynamic" headers that may or may not be present.
does it make sense to you? :)
 
2:08 PM
i understand you, It is quite a lot to take in. The app I am working on need a user to be authenticated before they can make the call to this second api, thus the need for the authtoken.
I probably would be able to go around this using your method, but I am time constrained. But would try
 
Here's my question. Who in your app knows whether they are authenticated or not.
What object can you use to say: am I authenticated? Yes? Here's the token. No? I need to get one.
^ who answers this
(rather, "what", for we're talking about objects) :D
 
So, when I run the app, on the splash screen, I make a call to authenticate the user using my details, after I save the value to the shared preferences that would be available throughout the app
I think the logic for checking whether or not to add headers when there is a token is what is missing 🤔
 
yes :)
pass an object that can store the token (to sharedpref) but keeps a copy in memory (your TokenRepository) (?) and then pass that to your header interceptor so it can as tokenrepo.hasToken??
that's your sessionInfo
 
 
2 hours later…
3:54 PM
Well, I gotta get down, good luck with that and stay inside! :)
I haven't heard back from you in a couple of hours, so I assume you're good to go. Post another SO question if you need further help. Be safe.
o/
 

  last day (882 days later) »