|
| 1 | +# NetworkResponseAdapter |
| 2 | + |
| 3 | +[](https://jitpack.io/#haroldadmin/NetworkResponseAdapter) |
| 4 | +[](https://github.com/haroldadmin/networkresponseadapter/actions) |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +This library provides a Retrofit call adapter to handle errors as a part of state. It helps you write cleaner code for network requests by treating errors as values, instead of exceptions. |
| 9 | + |
| 10 | +## Network Response |
| 11 | + |
| 12 | +`NetworkResponse<S, E>` is a Kotlin sealed class with the following states: |
| 13 | + |
| 14 | +1. Success: Represents successful responses (2xx response codes) |
| 15 | +2. ServerError: Represents Server errors |
| 16 | +3. NetworkError: Represents connectivity errors |
| 17 | +4. UnknownError: Represents every other kind of error which can not be classified as an API error or a network problem (eg JSON deserialization exceptions) |
| 18 | + |
| 19 | +It is generic on two types: a response (`S`), and an error (`E`). The response type is your Java/Kotlin representation of the API response, while the error type represents the error response sent by the API. |
| 20 | + |
| 21 | +## Example |
| 22 | + |
| 23 | +```kotlin |
| 24 | +data class DetailsResponse( |
| 25 | + val details: String |
| 26 | +) |
| 27 | + |
| 28 | +data class DetailsError( |
| 29 | + val errorMessage: String |
| 30 | +) |
| 31 | + |
| 32 | +interface Api { |
| 33 | + @Get("/details) |
| 34 | + suspend fun details(): NetworkResponse<DetailsResponse, DetailsError> |
| 35 | +} |
| 36 | +
|
| 37 | +class ViewModel { |
| 38 | + suspend fun fetchDetails() { |
| 39 | + when (val response = api.details()) { |
| 40 | + is NetworkResponse.Success -> handleSuccess(response.body) |
| 41 | + is NetworkResponse.ServerError -> handleServerError(response.code) |
| 42 | + is NetworkResponse.NetworkError -> handleNetworkError(response.error) |
| 43 | + is NetworkResponse.UnknownError -> handleUnknownError(response.error) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | +
|
| 49 | +## Installation |
| 50 | +
|
| 51 | +Add the Jitpack repository to your list of repositories: |
| 52 | +
|
| 53 | +```groovy |
| 54 | +allprojects { |
| 55 | + repositories { |
| 56 | + maven { url 'https://jitpack.io' } |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +And then add the dependency in your gradle file: |
| 62 | +
|
| 63 | +```groovy |
| 64 | +dependencies { |
| 65 | + implementation "com.github.haroldadmin:NetworkResponseAdapter:(latest-version)" |
| 66 | +} |
| 67 | +``` |
| 68 | +
|
| 69 | +<!-- prettier-ignore-start --> |
| 70 | +!!! note |
| 71 | + This library uses OkHttp 4, which requires Android API version 21+ and Java 8+. |
| 72 | +<!-- prettier-ignore-end --> |
| 73 | +
|
| 74 | +## License |
| 75 | +
|
| 76 | +```text |
| 77 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 78 | +you may not use this file except in compliance with the License. |
| 79 | +You may obtain a copy of the License at |
| 80 | +
|
| 81 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 82 | +
|
| 83 | +Unless required by applicable law or agreed to in writing, software |
| 84 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 85 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 86 | +See the License for the specific language governing permissions and |
| 87 | +limitations under the License. |
| 88 | +``` |
0 commit comments