Elegant Android REST
Dealing with REST API is a major part of most of Android applications. This post aims at providing an EO-way to perform REST requests.

Here is the needed Gradle dependency:
To perform request, let’s define the following interface using okhttp3.Response class:
Here is the implementation written to get repos:
It uses 3 idiomatic EO concepts:
- Seven Virtues of a Good Object: #2 He Works by Contracts
- Constructors Must Be Code-Free
- There Can Be Only One Primary Constructor
Following anti-DTO strategy, here is the code I use to parse data:
and
Here is the code to build a list of repos using JsonRepo:
It uses an idiomatic concept provided by Kotlin:
Finally, to perform the request, just write the following (used in an asynchronous thread):
Following this way of thinking, GetReposRequest could easily be mocked for unit tests. Or its primary constructor can be called directly with a mocked okhttp3.Call. It becomes easy to test, compose/reuse and maintain such elegant object.
For example, it becomes easy to wrap any request in a dedicated decorator with a log strategy such as:
and
Wrapping all together gives me the following code:
resulting in:
Note that I chose to defer validation to avoid calling API multiple times and run into weird behaviors.
To go further
Requests can be used “as is” directly in Activity/Fragment/ViewModel, on condition that call is done in background. It allows to have direct access to the query and cancel it when needed (during component’s lifecycle callback for instance).
But there could be a more sophisticated way to perform queries in an asynchronous way, following the old but still relevant advices from Google I/O 2010: Android REST client applications.
While developing Android applications, I’m used to use Android Priority Jobqueue for a while. But it’s now deprecated in favour of Android’s official WorkManager. Nevertheless, the idea remains the same: wrapping the RestRequest inside a job to perform it asynchronously. But I still haven't found how to write it in an EO-way. If anyone wants to try, I would enjoy seeing the resulting implementation.
Bonus: The Rx-way
In order to perform requests asynchronously, it’s possible the use the ReactiveX approach. First dependencies must be added:
Here is how I declare an asynchronous request as an interface:
It includes a convenient wrapper embedding a standard RestRequest.
The request to get repos for a given user becomes:
So now it becomes easy to call it such as:
For now, I’m not convinced that the Rx-way is a proper EO-way. What’s your mind about it? Please let me know by leaving a comment. It will be appreciated.
Edit: Deal with Android’s WorkManager in an EO-way proposal
First, I follow the official documentation to define a task that can be scheduled with WorkManager:
It’s very Android-specific since it deals with the “inputData” passed to the Worker (a bad EO-name by the way, just like “WorkManager” actually).
Next, I declare an interface that defined “a REST Request that can be managed using WorkManager” such as:
Then I define a convenient object that represents a “simple managed request”:
It’s where I put the logic to “enqueue” a task in the WorkManager and store the resulting operation. It exposes the LiveData provided by the WorkManager using the Worker’s ID (it’s part of the internal state, it doesn’t leak).
Now, let’s use this SimpleOneTimeWorkRequest to build our specific request to get repos:
It provides multiple secondary constructors to ease its usage and hide a part of the logic (constraints and data keys in this case).
We can now use it with an elegant invocation where we can observe the result:
What do you think about this proposal? Does its design fit with EO-principles? Please let me know by leaving a comment. It will be appreciated.