https://kotlinlang.org logo
#ktor
Title
# ktor
v

Vivek Modi

03/23/2022, 5:41 PM
Hey everyone, I have pre existing Android application using
OKHTTP
client with some`interceptors` and
authenticator
and iOS application using
alamofire
with
interceptors
.Both projects are huge in size. Now I want to use KMM for new api client. So How can I use these 2 http client through Ktor. I don't want to write from scratch in KMM module. Any suggestion?
s

Sebastian Owodzin

03/24/2022, 12:44 AM
You can’t use them inside Ktor, since the client logic will need to be written in common code.
v

Vivek Modi

03/24/2022, 8:24 AM
Do i need to use write from scratch?
d

David Nedrow

03/24/2022, 1:45 PM
Why do you want three API "clients"? OKHTTP, alamofire, AND Ktor?
You can't use the native routines through Ktor unless you wrap them, but even then I'm not sure how that would work.
The best thing to do would be to not worry about the existing stuff and write your new API handler via KMM/Ktor. You could slowly move common routines from your "legacy" API code and add them to the Ktor code. Obviously, that means re-writing them. But if you do it incrementally, the burden shouldn't be too bad.
v

Vivek Modi

03/24/2022, 1:53 PM
I don't want to change my previous retrofit and alamofire code because my project is huge in size. Now I want to use KMM, and Ktor is way to connect both platform. But in my retrofit and alomofire has interceptor and authenticator. So I want same behaviour in ktor client. That's why I asked is there a way to use existing in ktor or not
yeah I didn't find the way to connect intercepter
d

David Nedrow

03/24/2022, 1:54 PM
Are you asking if you can use your existing implementation in KMM/Ktor, or are you asking if there are interceptors and authenticators in Ktor?
I may have misunderstood the question.
v

Vivek Modi

03/24/2022, 1:56 PM
I am asking can I use my existing implementation in kmm/ktor or not ?
I know that interceptor and authenticator are available in ktor
s

Sebastian Owodzin

03/24/2022, 9:13 PM
You’d need to reimplement everything in KTOR. You can try to do this in phases if your project allows you to do that, for example first do all unauthenticated calls then add the one that require authentication. Also remember that some of the custom interceptors/authenticators might not be needed in KTOR, as it has lots of features/plugins.
v

Vivek Modi

03/24/2022, 9:29 PM
Ok that's great
a

Aleksei Tirman [JB]

03/25/2022, 5:19 PM
You can configure the OkHttp engine to use your existing interceptors (see an example here). Unfortunately, for
alamofire
this not gonna work.
v

Vivek Modi

03/25/2022, 5:38 PM
so I need to write logic in ios module?
a

Aleksei Tirman [JB]

03/25/2022, 5:41 PM
Yes.