GithubHelp home page GithubHelp logo

auron567 / retrofit2coroutinescalladapter Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 2.0 147 KB

Retrofit CallAdapter for Coroutines to handle response as states

Kotlin 100.00%
android kotlin coroutines repository-pattern mvvm viewmodel livedata retrofit2 retrofit2-coroutines retrofit2-adapter

retrofit2coroutinescalladapter's Introduction

Retrofit2 Coroutines CallAdapter

Sample Android application that show how can we create our own Retrofit CallAdapter for Coroutines to handle response as states.

Implementation

By creating your own CallAdapter, you can do the following:

interface Service {
    @GET("movies/{id}")
    suspend fun getMovie(): NetworkResponse<Movie, MovieError>
}

class Repository {
    val movie = service.getMovie()
    
    when (movie) {
        is NetworkResponse.Success -> // Success response
        is NetworkResponse.ApiError -> // Failure response (non-2xx)
        is NetworkResponse.NetworkError -> // Network failure
        is NetworkResponse.UnknownError -> // Unexpected exceptions
    }
}



The implementation of CallAdapter in the application is defined through the following classes:



Also you need to make Retrofit aware of our custom CallAdapter.Factory:

fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addCallAdapterFactory(NetworkResponseAdapterFactory())
        .addConverterFactory(GsonConverterFactory.create())
        .build()
}

Architecture

This sample application uses Architecture Components to separate the UI code in MainActivity from the application logic in MainViewModel. MovieRepository (and its concrete MovieRepositoryImpl implementation) provides a bridge between the ViewModel and MovieService, which uses Retrofit to return a list of Movie objects.

Libraries

Usage

To use this sample application you need to register your own TMDb API key and add it to the file NetworkModule.kt:

const val API_KEY = "INSERT-API-KEY-HERE"

References

retrofit2coroutinescalladapter's People

Contributors

auron567 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.