Flow Response Retrofit Adapter

The small library containing the wrapper over network requests implemented using Kotlin Flow and the adapter that allows this wrapper to be used along with Retrofit 2.

Лицензия

Лицензия

Категории

Категории

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

Группа

com.github.nikolaymenzhulin
Идентификатор

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

flow-response-retrofit-adapter
Последняя версия

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

1.2.1
Дата

Дата

Тип

Тип

pom.sha512
Описание

Описание

Flow Response Retrofit Adapter
The small library containing the wrapper over network requests implemented using Kotlin Flow and the adapter that allows this wrapper to be used along with Retrofit 2.
Ссылка на сайт

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

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

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

https://github.com/NikolayMenzhulin/Flow-Response-Retrofit-Adapter

Скачать flow-response-retrofit-adapter

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.32

runtime (3)

Идентификатор библиотеки Тип Версия
com.squareup.retrofit2 : retrofit jar 2.9.0
com.squareup.okhttp3 : okhttp jar 4.9.1
org.jetbrains.kotlinx : kotlinx-coroutines-core-jvm jar 1.4.3

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

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

Flow-Response-Retrofit-Adapter

The small library containing the wrapper over network requests implemented using Kotlin Flow
and the adapter that allows this wrapper to be used along with Retrofit 2.

build Maven Central License

Usage

Add FlowResponseCallAdapterFactory to your Retrofit configuration:

Retrofit.Builder()
    // Some another configuration.
    .addCallAdapterFactory(FlowResponseCallAdapterFactory())
    .build()

In your Retrofit interfaces wrap results of your requests in FlowResponse:

interface SomeRetrofitService {
    
    @GET("/")
    fun someNetworkRequest(): FlowResponse<SomeNetworkRequestResult>
    
    // Some other methods.
}

Use Kotlin Coroutines for work with Flows and receive Response states of your requests:

someRetrofitService.someNetworkRequest()
    .onEach { state ->
        when {
            state.isLoading -> {
                // Logic of changing the loading state or something else.
            }
            state.isSuccess -> {
                if (!state.isEmpty) {
                    val data: SomeNetworkRequestResult = state.getData()
                    // Logic of reaction to the success response and using the data from it...
                } else {
                    // or reaction on the empty data.
                }
            }
            state.isError -> {
                val error: Throwable = state.getError()
                // Logic of errors handling.
            }
        }
    }.launchIn(coroutineScope)

More information of usage into the sample.

Download

Step 1. Add the Maven Central repository to your build file:

allprojects {
    repositories {
        mavenCentral()
    }
}

Step 2. Add the dependency:

dependencies {
    implementation 'com.github.nikolaymenzhulin:flow-response-retrofit-adapter:1.2.1'
}

License

Copyright © 2021 Nikolay Menzhulin.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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

Версия
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0