Retrofit Co Routines Railway Adapter

An Idiomatic way to parse different success and failure response bodies on Retrofit 2

Лицензия

Лицензия

Категории

Категории

Retrofit Сеть HTTP Clients
Группа

Группа

xyz.teja.retrofit2
Идентификатор

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

coroutines-railway-adapter
Последняя версия

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

1.0.0
Дата

Дата

Тип

Тип

pom.sha512
Описание

Описание

Retrofit Co Routines Railway Adapter
An Idiomatic way to parse different success and failure response bodies on Retrofit 2
Ссылка на сайт

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

https://github.com/Teja-Konjeti/Retrofit-Railway-Adapter/
Система контроля версий

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

https://github.com/Teja-Konjeti/Retrofit-Railway-Adapter/

Скачать coroutines-railway-adapter

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
xyz.teja.retrofit2 : base-railway-adapter jar 1.0.0

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

Данный проект не имеет модулей.

Retrofit Railway Adapter (Co-Routines & RxJava 2)

build success Maven-Central code coverage

An Idiomatic way to parse different success and failure responses for implementing Railway Pattern on Retrofit 2

Usage

// Can be any class
data class SuccessBody(val success: String)
// Can be any class
data class ErrorBody(val error: String)

interface Service {
    @GET("/") fun getBody(): Call<NetworkResponse<SuccessBody, ErrorBody>>
    @GET("/") suspend fun getBody(): NetworkResponse<SuccessBody, ErrorBody>
    @GET("/") fun getBodyObservable(): Observable<NetworkResponse<SuccessBody, ErrorBody>>
}

This will parse to one of the following responses

// A request that resulted in a successful response of type T (SuccessBody in this example).
NetworkResponse.Success(val body: SuccessBody)
// A request that resulted in a response of type  (ErrorBody in this example).
NetworkResponse.ServerError(val body: ErrorBody?, val response: okhttp3.Response, val rawBody: String)
// A request that didn't result in a response or some protocol error.
NetworkResponse.NetworkError(val error: Throwable)

The complete use cases and parsing behaviour can be found here

Note: DO NOT use Gson as your Retrofit converter. Gson uses reflection and will ALWAYS parse into SuccessBody even when a property is non-nullable which causes run-time exceptions.

This library has been tested with Jackson and Kotlinx.serialization libraries.

Setup (Co-Routines or Generic Call)

Gradle

dependencies {
    // Appropriate Kotlin-stdlib, Retrofit and OkHttp3 (latest version) need to be implemented with the adpater
    implementation "xyz.teja.retrofit2:coroutines-railway-adapter:1.0.0"
}

Retrofit

    val retrofit = Retrofit.Builder()
        .baseUrl(server.url("/"))
        .addCallAdapterFactory(CoRoutinesRailwayAdapterFactory)
        .addConverterFactory(
            JacksonConverterFactory.create(
                JsonMapper
                    .builder()
                    .addModules(KotlinModule(nullIsSameAsDefault = false))
                    .build()
            )
        )
        .build()
    val testService = retrofit.create(Service::class.java)

Setup (RxJava)

Gradle

dependencies {
    // Appropriate Kotlin-stdlib, Retrofit and OkHttp3 (latest version) need to be implemented with the adpater
    implementation "xyz.teja.retrofit2:rxjava2-railway-adapter:1.0.0"
}

Retrofit

    val retrofit = Retrofit.Builder()
        .baseUrl(server.url("/"))
        .addCallAdapterFactory(RxJava2RailwayAdapterFactory)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addCallAdapterFactory(CoRoutinesRailwayAdapterFactory)
        .addConverterFactory(
            JacksonConverterFactory.create(
                JsonMapper
                    .builder()
                    .addModules(KotlinModule(nullIsSameAsDefault = false))
                    .build()
            )
        )
        .build()
    val testService = retrofit.create(Service::class.java)

Kudos to naturalwarren for the idea. This implementation is completely different from his implementation. This library supports Co-Routines and also try to convert response body to error body when the response is successful among many other use cases handled. You can find all of these use cases in the test suite of the coroutines module.

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

Версия
1.0.0