Skip to content

ebi-igweze/simple-call-adapter

Repository files navigation

Simple Call Adapter

A simple adapter for retrofit 2

Introduction

This is a simple adapter for retrofit responses. It is made to compose both error and success response that will be passed into one callback and also provide cancellable subscriptions for asynchronous calls. You can read this post explaining the setup.

Key Classes

Class Description
Simple This is a wrapper class that wraps the resulting response for the passed callback
Subscription This is the class that wraps disposable results for asynchronous calls
SimpleCallAdapterFactory This is a factory class for creating the call adapter for retrofit
SimpleCallAdapter This is the retrofit adapter responsible for transforming the retrofit's Call interface into a Simple response

Setup

First, add the following to your repositories block

    maven { url "http://dl.bintray.com/ebi-igweze/maven" }

then secondly, add this to your module level dependencies block

    implementation "com.igweze.ebi:simple-call-adapter:1.0.1"

Usage

You can use it as follows,

First you have to add the SimpleCallAdapterFactory to your retrofit container like so:

    Retrofit.Builder()
        .baseUrl(your_base_url)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(SimpleCallAdapterFactory.create())
        .build()

then you declare a Simple<T> as your return type in your HttpService interface, where T is the response value your are expecting from your api endpoint, for example:

Kotlin

interface IHttpService {
    @GET("users")
    fun getUsers(@Query("count") count: Int): Simple<List<User>>
}

Java

interface IHttpService {
    @GET("users")
    Simple<List<User>> getUsers(@Query("count") int count)
}

finally you can extract your response using a java functional interface or kotlin lambda expression, for example:

Kotlin

    // run function asynchronously and get a disposable subscription
    val subscription = httpService.getUsers(5).process { users, throwable ->
        // check if error occurred
        if (throwable != null) {
            if (throwable is HttpException) {
                // handle http exception
            } else {
                // handle other errors
            }
        }

        // handle success response
        users?.also { usersList.add(it) }
    }
    // ...

    // dispose subscription manually
    if (!subscription.isDisposed())
        subscription.dispose()

    //...

    // dispose subscription by binding to a lifecycle component
    subscription.bind(this@MainActivity)

    httpService.getUsers(5).run { users, t ->
        // handle error
        if (t != null) { }

        // handle success
        users?.also { userList.add(it) }
    }

    //...

    // or you could do multiple calls to get the raw response
    try {
        // get the Response<UserInfo>
        val userInfoResponse = httpService.getUserInfo().run()
        if (userInfoResponse.isSuccessful) {
            val userInfo = useInfoResponse.body()
            val accountInfoResponse = httpService.getUserAccountInfo().run()
            val user = if (accountInfoResponse.isSuccessful) {
                val accountInfo = accountInfoResponse.body()
                User(userInfo, accountInfo)
            } else {
                null
            }

            // do what you want with user
        }

    } catch(ex: IOException) {
        // handle exception
    }

Java The call back also applies to java but you do it using the SimpleHandler functional interface, like so:

    // asynchronous call
    // for java 8
    httpService.getUsers(5).process((users, throwable) -> {
        // handle error or success response
    }).bind(MainActivity.this);

    // synchronous call
    // or for java <= 7
    httpService.getUsers(4).run(new SimpleHandler<List<User>>() {
        @Override
        public void accept(List<User> users, Throwable throwable) {
            // handle error or success response
        }
    });

And that is it.

About

A call adapter for retrofit 2

Resources

Stars

Watchers

Forks

Packages

No packages published