A new stable release for your favourite Retrofit call adapter. v5 is a culmination of a lot of community contributions and an overhaul of the internals of the library. It's completely backward compatible for existing users of the library, but the changes are significant enough to warrant a major version update.
Upgrade Guide: https://haroldadmin.github.io/NetworkResponseAdapter/upgrade-guides/v5/
Changes since v4:
Sealed Interfaces
The NetworkResponse
class is now based on sealed interfaces. This allows for more concise when
expressions when you don't care about the specific type of the error:
when (networkResponse) {
is NetworkResponse.Success -> ...
is NetworkResponse.Error -> ...
}
Raw Retrofit Responses
NetworkResponse.Success
, NetworkResponse.ServerError
and NetworkResponse.UnknownError
now bundle the raw retrofit Response<S>
object to allow for greater access to the response of the network request.
when (networkResponse) {
is NetworkResponse.Success -> {
val statusCode = networkResponse.response.code()
}
}
Handling Empty Response Bodies
In the current version of the library you have to use the Unit
response type if you expected your server to respond without a body. This is fine when the API never returns a body, but causes problems if it sometimes returns a body and sometimes doesn't (200 vs 204 status code).
The bundled raw Retrofit responses provide a better way to handle this situation.
interface PostsService {
@GET("/")
suspend fun getPost(): NetworkResponse<Unit, ErrorResponse>
}
when (val postResponse = service.getPost()) {
is NetworkResponse.Success -> {
if (postResponse.code != 204) {
val rawBody = postResponse.response.rawBody()
// Manually parse the raw body to access the response
}
}
is NetworkResponse.Error -> { ... }
}
Tests Overhaul & Migration to Kotest
This PR gets refactors the library's test suite to get rid of redundant and obscure tests, and replaces them with a streamlined test suite focused on publicly exposed functionality.
We've also finally moved away from the deprecated kotlintest
artifacts to the new kotest
libraries.
Remove Deprecated Classes
We've removed deprecated ways to instantiate adapter factory. The existing classes had been marked as deprecated for a long period, and I hope everyone has moved away from them.
Kotlin 1.6.0
Updated the language level to 1.6.0
Sample App
Add a module showing sample usage of the library using the kotlinx.serialization
library.
Huge thanks to @argenkiwi and @gilgoldzweig for their contributions to this release.