A plug-and-play Retrofit CallAdapter for type-safe network response handling in Android. Clean error handling with sealed classes — no more messy try-catch blocks.
Add JitPack to your settings.gradle.kts:
dependencyResolutionManagement {
repositories {
maven { url = uri("https://jitpack.io") }
}
}Add the dependency:
dependencies {
implementation("com.github.navgurukul:NetworkResponseAdapter:1.0.0")
}Add the adapter factory to Retrofit:
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addCallAdapterFactory(NetworkResponseAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()Define your API:
interface ApiService {
@GET("users")
suspend fun getUsers(): NetworkResponse<List<User>, ErrorResponse>
}Handle responses:
when (val response = apiService.getUsers()) {
is NetworkResponse.Success -> response.body // Your data
is NetworkResponse.ServerError -> response.code // 4xx, 5xx
is NetworkResponse.NetworkError -> response.error // No internet, timeout
is NetworkResponse.UnknownError -> response.error // Parsing errors, etc.
}Or simplified:
when (val response = apiService.getUsers()) {
is NetworkResponse.Success -> handleData(response.body)
is NetworkResponse.Error -> showError(response.error.message)
}- 🎯 Type-safe error handling with sealed classes
- 💾 Built-in Room caching (Cache-First, Network-First, etc.)
- 🔄 Retry with exponential backoff
- ⚡ Kotlin Coroutines support
- 📦 Header preservation
- 🌐 Offline support
Full docs, advanced usage, caching strategies, and API reference: