Supercharging Retrofit with Kotlin


Constructing a Kotlin-esque API for Retrofit

At Coinbase we use Retrofit and Sq.’s RxJava 2 Adapter as our API to the wire. Retrofit makes networking a breeze, however all through our app we discovered ourselves writing code like this:

https://medium.com/media/903b7744b3ff6a3afea6f2af43d9d363/href

This works, however there’s a couple of tough edges:

  • Our API isn’t declarative. To find out what state we’re in now we have to null test a bunch of issues and it’s straightforward to overlook a case
  • We’ve inadvertently leaked our community serializer (moshi) into our software layer to deserialize error our bodies
  • Streams get torn down when a community error happens. This isn’t a giant deal right here, but when we begin combining community streams with different Observables we doubtless don’t need community errors to terminate the ensuing stream

Let’s have a look at how we are able to use Retrofit’s CallAdapter API to nerf down these edges. We’ll use Sealed Classes to signify the results of community calls and construct error physique deserialization into Retrofit.

https://medium.com/media/a9c5e48937a814bdf845ab0f5e23c129/href

To have Retrofit return an occasion of NetworkResponse when the getTokens() API is invoked, now we have to put in writing a customized CallAdapter.Factory. The CallAdapter.Manufacturing facility beneath says, “I understand how to create situations of NetworkResponse which are emitted to RxJava streams."

https://medium.com/media/1f1d92f0536c3e3a664a1932dd682b73/href

When the getTokens() API is invoked, this CallAdapter.Manufacturing facility:

  • Delegates to an adapter that is aware of learn how to make an occasion of the kind Observable<AccessToken> (line 46)
  • Asks for a converter able to serializing the kind Error (line 54)
  • Creates a KotlinRxJava2CallAdapter (line 60). This adapter deserializes error our bodies and can adorn the stream of Observable<AccessToken> right into a Single<NetworkResponse<AccessToken,Error>>

To make use of our customized CallAdapter.Manufacturing facility now we have to plug it into our Retrofit occasion. Adapter registration order is vital since we’ve written a delegating CallAdapter; we should register our adapter earlier than every other adapters it could delegate to. Within the snippet beneath we delegate the creation of Observable<AccessToken> to Sq.’s RxJava Adapter.

val retrofit = Retrofit.Builder()
.baseUrl("https://api.coinbase.com/v2/")
.addCallAdapterFactory(KotlinRxJava2CallAdapterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.construct()

When you’re curious about having a Kotlin-esque API to the wire that gives error physique deserialization, you'll find the complete code for our Adapter here.

P.S. We’re hiring!

Particular because of Jesse Wilson for catching a bug within the full code for the adapter; all error codes aren’t assured to return JSON!


Supercharging Retrofit with Kotlin was initially printed in The Coinbase Blog on Medium, the place persons are persevering with the dialog by highlighting and responding to this story.

Leave a Reply

Your email address will not be published. Required fields are marked *