Giter Site home page Giter Site logo

elip-kio / retrofit-suspend-result-adapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yujinyan/retrofit-suspend-result-adapter

0.0 0.0 0.0 81 KB

Retrofit 2 call adapter for Kotlin suspend functions and stdlib Result return type.

Kotlin 100.00%

retrofit-suspend-result-adapter's Introduction

Retrofit Suspend Kotlin Result Adapter

Release

A Retrofit 2 CallAdapter.Factory for Kotlin suspend functions that use the standard library kotlin.Result as return value. The call adapter catches network failures / deserialization errors so that there is no need to try catch suspend functions at call sites.

Many useful methods are available on the Result type. Once Kotlin allows null-safety operators ?., ?: and !! on Result, the ergonomics of using this type will be greatly improved.

Note that using kotlin.Result as function return type requires Kotlin 1.5.

Usage

val retrofit = Retrofit.Builder()
  .baseUrl("https://example.com")
  .addCallAdapterFactory(SuspendResultCallAdapterFactory())
  .build()

Your retrofit service can now use suspend functions with Result<T> return type.

interface MyService {
  @GET("/user")
  suspend fun getUser(): Result<User>
}

On Android, you can call suspend functions on the main thread without try catch.

lifecycleScope.launch {
  retrofit.create<MyService>()
    .getUser()
    .getOrNull()?.let {
      // update UI
      binding.nameLabel.value = it
    }
}

Failure handler

You can register a default failure handler through the constructor. This is a good place for configuring global error handling logic.

val retrofit = Retrofit.Builder()
  .baseUrl("https://example.com")
  .addCallAdapterFactory(SuspendResultCallAdapterFactory {
    report(it)
  })
  .addConverterFactory(MoshiConverterFactory.create(moshi))
  .build()

Custom converter for Result<T>

If your API returns data wrapped in an "envelop", it may be a good idea to unwrap the data and place it in Result directly.

Suppose the API response looks like this. You can still use Result<User> as return type.

{
  "errcode": 0,
  "data": {
    "id": 1,
    "name": "Peter"
  }
}

To achieve this, write a custom converter. Checkout CustomConverterTest for an example.

retrofit-suspend-result-adapter's People

Contributors

yujinyan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.