https://kotlinlang.org logo
Title
a

andylamax

07/30/2022, 7:53 PM
Welcome to the kotlin slack @Yesayaathuman Questions like these (General android questions) are most suited for stack overflow or the android slack. Not the kotlin slack. That being said IIRC, starting from version 2.6.0, retrofit did start supporting coroutines and suspend functions. Meaning
@POST("login")
suspend fun login(
    email: String,
    password: String
) : Call<AccessToken>
can just be written as
suspend fun login(
    email: String,
    password: String
) : AccessToken          // Notice that there is no need to Wrap this inside a Call object
Keep in mind that before embracing coroutines, your code could have looked like this (without the suspend keyword)
@POST("login")
fun login(                // Notice that there is no suspend modifier here
    email: String,
    password: String
) : Call<AccessToken>
After you change your return type, you can just wrap your service in a try catch block like this
try {
    val token = service.login("<mailto:yesaya@athumani.com|yesaya@athumani.com>","secure-password")
} catch(exception: Throwble){
   // handle exceptions here
}
Your code becomes simple and almost similar to a concurrent one. ADVICE: Because you have chosen to embrace coroutines, why not look into ktor-client instead of retrofit?? These annotation magic tends to hide some implementations that lead to scenarios like these. You can ask ktor specific questions in the #ktor channel. But if you still want to keep dancing with retrofit, try reading this article, It may give you more guidance.