Adapter for Retrofit 2 that allows using LiveData objects as return a return type for API definitions
LiveData is part of the Android Jetpack 🚀 Architecture components, and using it with Retrofit is not that hard (but google doesn't bothers about explaining how to 😒), so you can use add this library to your project to define an adapter. (like that RxJava adapter, yay!).
The source code is inside the library folder / module.
Simple. First, replace all your Call<T>
references with LiveData<ApiResult<T>>
references in your API definitions interface class.
This means, from:
MyAwesomeAPIService.kt
interface MyAwesomeAPIService {
@GET("/mtndew/flavors")
fun fetchMtnDewFlavors():Call<List<MtnDewCan>>
}
To:
MyAwesomeAPIService.kt
interface MyAwesomeAPIService {
@GET("/mtndew/flavors")
fun fetchMtnDewFlavors():LiveData<ApiResult<List<MtnDewCan>>>
}
Then, add the adapter to your retrofit instance.
YourRetrofitDependencyInjectorOfSomeSort.kt
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(LiveDataAdapter.Factory()) // <-- Define the LiveData adapter Factory here!
.build()
.create(MyAwesomeAPIService::class.java)
The ApiResult.kt class is a model class provided in the library too, it contains your response object inside its body property, a code is also contained and an error string is also there somewhere in the class if you want some of that spicy error handling (that you should definitely have), it comes as a String.
What to do with that error string?, You can print it as it is, it can be a JSON/XML string too! (if the server is responding with custom JSON errors, that it should be) so you can use GSON, Jackson or whatever parsing tool you want to handle the custom error response error or something like that.
You can also use it to poke your eyes out but, why would you? (actually you can't, it is a String, is not a physical object you dummy!)
You can set the return type of your call as a LiveData containing your response object type, the value will be set as null if any exception happens during the call.
You know how dangerous that technique can be, right?
Handling the error of your API call only because you got a null result is a really bad idea! as you don't have any information of what went wrong, (and the customer care deparment is going to love you when a user calls with an oopsie, something wrong happened! message and you don't know what happened!)
Anyway, if you really want to (or you are lazy enough to), just set the return type to LiveData.
Yeah sure, add it to your project like this:
implementation 'me.alfredobejarano.retrofitadapters:livedata:0.0.2'
Enjoy! :happy:
Made with ❤️ by @AlfredoBejarano