This works, however there’s just a few tough edges:
- Our API isn’t declarative. To find out what state we’re in we now have to null examine a bunch of issues and it’s simple 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 possible don’t need community errors to terminate the ensuing stream
Let’s take a look at how we will use Retrofit’s CallAdapter API to nerf down these edges. We’ll use Sealed Classes to symbolize the results of community calls and construct error physique deserialization into Retrofit.
To have Retrofit return an occasion of
NetworkResponse when the
getTokens() API is invoked, we now have to jot down a customized
CallAdapter.Manufacturing facility under says, “I understand how to create cases of
NetworkResponse which might be emitted to RxJava streams.”
getTokens() API is invoked, this
- Delegates to an adapter that is aware of how you can make an occasion of the kind
- Asks for a converter able to serializing the kind
- Creates a
KotlinRxJava2CallAdapter(line 60). This adapter deserializes error our bodies and can adorn the stream of
Observable<AccessToken>right into a
To make use of our customized
CallAdapter.Manufacturing facility we now have to plug it into our
Retrofit occasion. Adapter registration order is essential since we’ve written a delegating
CallAdapter; we should register our adapter earlier than every other adapters it could delegate to. Within the snippet under we delegate the creation of
Observable<AccessToken> to Sq.’s RxJava Adapter.
val retrofit = Retrofit.Builder()
When you’re focused on having a Kotlin-esque API to the wire that gives error physique deserialization, you could find the complete code for our Adapter here.
P.S. We’re hiring!