Hey I am new in the world of Kotlin Multiplatform ...
# multiplatform
v
Hey I am new in the world of Kotlin Multiplatform mobile. I have experience in development of android field. I am trying to connect Android application and Ios application. I searched and see Ktor is the way to fetch data from server. In android we use Kotlin Sealed Classes for Handling API Responses example in your application and in ios side team use Understanding Swift closures and asynchronous request functions similar to these. I am little bit confused, How can I use this way in common module to use both platform because both platform have Interceptor and many more things. Do I need to write everything from scratch? or Is there any way we can use existing way. My both platforms application is huge project. I don't won't to replace the code. I want to start new api call throught KMM. Any suggestion would be helpful for me. Thanks
g
"Ktor is the way": if you've a REST api it's a good choice, if you want to share more (with backend) or in a more type-safe way, then you may want to have a look at GraphQL (here is a KMP client). Once the choice is done, you can describe your API calls in Kotlin and generate a library for each platform you want. And since it's Kotlin and Ktor (for example), you'll probably not be able to re-use your old code for doing the network call. At this point you'll probably have to find a solution for asynchronous code, maybe coroutines + Result<T> is enough for you, it'll depend of your architecture and needs.
v
@Grégory Lureau so I need to write everything from scratch?
g
If "everything" means the network layer, then probably yes, since I presume you're using a network library for each platform. Your question is very generic so I've little information about what you really want to achieve... Note that logic like retry or timeouts don't have to be in the network layer and can be re-used. As an alternative, you can share only the DTOs, not sure it will bring a lot of value for you but at least you can keep your current platform network libraries.
v
yes I used retrofit library.
@Grégory Lureau One thing I used HttpLoggingInterceptor with AuthorizationInterceptor, HeadersInterceptor in my OkHttpClient in my retrofit client. When I am using Ktor do I need to reuse it or I need to write from scratch using ktor Http?
g
Same name, but it's not the same interface, so you'll not be able to magically matches (relatively) similar interfaces.
v
got it thanks @Grégory Lureau