NetworkResponse

NetworkResponse multiplatform

Лицензия

Лицензия

Категории

Категории

Данные Сеть
Группа

Группа

com.javiersc.network-response
Идентификатор

Идентификатор

network-response-metadata
Последняя версия

Последняя версия

0.8.1
Дата

Дата

Тип

Тип

pom.sha512
Описание

Описание

NetworkResponse
NetworkResponse multiplatform
Ссылка на сайт

Ссылка на сайт

http://github.com/JavierSegoviaCordoba/NetworkResponse
Система контроля версий

Система контроля версий

https://github.com/JavierSegoviaCordoba/NetworkResponse

Скачать network-response-metadata

Зависимости

compile (5)

Идентификатор библиотеки Тип Версия
org.jetbrains.kotlinx : kotlinx-coroutines-core-metadata jar 1.4.2
io.ktor : ktor-client-core jar 1.4.3
io.ktor : ktor-client-json jar 1.4.3
io.ktor : ktor-client-serialization jar 1.4.3
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.21

Модули Проекта

Данный проект не имеет модулей.
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

Версии библиотеки

Версия
0.8.1
0.8.0
0.7.3
0.7.2