NetworkResponse

NetworkResponse multiplatform

License

License

Categories

Categories

Data Net
GroupId

GroupId

com.javiersc.network-response
ArtifactId

ArtifactId

logger-extensions-metadata
Last Version

Last Version

0.8.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

NetworkResponse
NetworkResponse multiplatform
Project URL

Project URL

http://github.com/JavierSegoviaCordoba/NetworkResponse
Source Code Management

Source Code Management

https://github.com/JavierSegoviaCordoba/NetworkResponse

Download logger-extensions-metadata

Dependencies

compile (3)

Group / Artifact Type Version
com.javiersc.network-response : network-response-metadata jar 0.8.1
com.javiersc.logger : logger-serialization jar 0.10.0
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.21

Project Modules

There are no modules declared in this project.
Master Download Coverage Master Master Build Quality Master
Develop Download Coverage Develop Develop Build Quality Develop

NetworkResponse

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

Versions

Version
0.8.1
0.8.0
0.7.3