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.

License

License

Categories

Categories

Retrofit Net HTTP Clients
GroupId

GroupId

com.github.nikolaymenzhulin
ArtifactId

ArtifactId

flow-response-retrofit-adapter
Last Version

Last Version

1.2.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

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.
Project URL

Project URL

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

Source Code Management

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

Download flow-response-retrofit-adapter

Dependencies

compile (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.32

runtime (3)

Group / Artifact Type Version
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

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0