Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Kotlin's Result #3558

Open
GSala opened this issue May 10, 2021 · 5 comments · May be fixed by #4018
Open

Add support for Kotlin's Result #3558

GSala opened this issue May 10, 2021 · 5 comments · May be fixed by #4018
Labels

Comments

@GSala
Copy link

GSala commented May 10, 2021

Now that inline classes are stable in Kotlin 1.5.0, it would be nice to have API endpoints return Result to wrap the outcome (success/failure) of a retrofit call, instead of returning a value or throwing an exception.

For example:

@GET("me")
suspend fun getUser(): Result<User>
@kobito-kaba
Copy link

I doubt if Retrofit should support kotlin Result at this moment, because returning kotlin Result from functions is not the default behavior.

Anyway, this call adapter will work for your use case.
note:
That call adapter will work for suspend fun foo() : Result<Foo> , but won't work for fun foo(): Result<Foo> in some reasons.

@tejas-n
Copy link

tejas-n commented Jun 26, 2021

Initially I thought that might be a good addition, but I guess you can always use runCatching if you want the type wrapped in Result

val user: Result<User> = runCatching {
    api.getUser()
 }

@youarenot667543
Copy link

val user: Result\u003CUser> = runCatching {\n api.getUser()\n }

@agamula90
Copy link

You can try my solution - https://github.com/agamula90/RetrofitKx, hosted on mavenCentral, based on retrofit + moshi + ksp if you are are looking for better kotlin api

@Goooler
Copy link
Contributor

Goooler commented May 29, 2024

As a workaround, you can try out https://github.com/connyduck/networkresult-calladapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants