Giter VIP home page Giter VIP logo

networkresponse's Introduction

Master Download Coverage Master Master Build Quality Master
Develop Download Coverage Develop Develop Build Quality Develop

NetworkResponse is a sealed class to wrap responses from network requests:

  • Success [data, code and headers]
  • Error [error, code and headers]
  • UnknownError [throwable]
  • RemoteNotAvailable
  • InternetNotAvailable

This library works very well when used in conjunction with Resource which is very similar to NetworkResponse but thought to use with another architecture layer, for example domain objects. NetworkResponse has mappers to transform it to a Resource.

Features

  • Multiplatform (NetworkResponse and Resource support)
  • Retrofit support (jvm)
  • Ktor support

Download

This library is Kotlin Multiplatform but at this moment jvm is the only artifact generated. It is available at Maven Central.

implementation("com.javiersc.network-response:network-response:$version")

Retrofit

This adapter for Retrofit wraps automatically the Retrofit responses with a NetworkResponse:

@GET("users")
suspend fun getUsers(): NetworkResponse<List<UserDTO>, ErrorDTO>
// UserDTO and ErrorDTO should be your data classes

If the server doesn't return an error body, or it is irrelevant you can mark it as Unit:

@GET("users")
suspend fun getUsers(): NetworkResponse<List<UserDTO>, Unit>

Add the NetworkResponseCallAdapterFactory to the Retrofit builder:

private val retrofit = Retrofit.Builder().apply {
    //...
    addCallAdapterFactory(NetworkResponseCallAdapterFactory())
    //...
}.build()

It is possible to use Deferred too:

@GET("users")
fun getUsers(): Deferred<NetworkResponse<List<UserDTO>, ErrorDTO>>

Ktor

You only need to wrap the request and do not indicate the type in the client method because it is inferred automatically

val usersNetworkResponse = NetworkResponse<List<UsersDTO>, ErrorDTO> { client.get("https://example.com/users") }

More artifacts

Credits

Based on NetworkResponseAdapter by Kshitij Chauhan

networkresponse's People

Contributors

javiersegoviacordoba avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

lexuanthanh273

networkresponse's Issues

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.