https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

divyanshunegi

06/12/2021, 10:14 AM
This might sound a noob question but just starting on KMM, I want to create an APIClient as shared module for Android and iOS I need the following features • It should have local SSL data encryption • InternetConnectionCheck Interceptor • ErrorHandlerInterceptor • RetryRequest After RefreshToken Interceptor • Few other custom interceptors Earlier I was writing my APiClient in the
commonMain
but as I can see it does not support addInterceptor property by default, which is available in OkHttp for Android. So the new plan I had is create an expect class APiClient in common main and implement APIClient for Android on AndroidMain with OkHttp engine and APiClient on iOSMain with IOS Engine. Is this approach right ? or am I missing some part ?
👍 1
c

CLOVIS

06/12/2021, 11:01 AM
Well the idea is ok, but you could use Ktor (#ktor) that does all of this for you. It's a library that has all the
expect-actual
stuff you need to write Multiplatform clients from the get go.
On each platform, it delegates to the platform's implementation
d

divyanshunegi

06/12/2021, 3:37 PM
@CLOVIS I am using ktor right now, and I tried using
Copy code
engine {
  addInterceptor()
}
This only works if I use
OkHttp
engine on ktor which will only work on the Android side, I want to do this on iOS side as well bit
IOS
ktor engine does not support this.
currently I checked that I can write my custom plugin, but getting confused on adding the SSL part, others can be done as I checked the documentation
c

CLOVIS

06/12/2021, 4:44 PM
Ask around in #ktor, I'm not an expert, but it should handle SSL by default, as well as most of what you're describing
Not through
engine {}
, of course, as that can't be Multiplatform
d

divyanshunegi

06/12/2021, 5:32 PM
got it. thanks for help @CLOVIS. really appreciated
r

Rak

06/14/2021, 8:04 AM
With KTor i can recommend this video

https://www.youtube.com/watch?v=_Q62iJoNOfg&list=PLlFc5cFwUnmy_oVc9YQzjasSNoAk4hk_C&index=5&ab_channel=KotlinbyJetBrains