|
5 | 5 |
|
6 | 6 | ## Introduction |
7 | 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. |
| 8 | +This library provides a Kotlin Coroutines based Retrofit call adapter for wrapping your API responses in |
| 9 | +a `NetworkResponse` type. |
| 10 | + |
| 11 | +See [Installation](#installation) for setup instructions. |
9 | 12 |
|
10 | 13 | ## Network Response |
11 | 14 |
|
12 | | -`NetworkResponse<S, E>` is a Kotlin sealed class with the following states: |
| 15 | +`NetworkResponse<S, E>` is a Kotlin sealed interface with the following states: |
13 | 16 |
|
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) |
| 17 | +- `Success`: Represents successful network calls (2xx response codes) |
| 18 | +- `Error`: Represents unsuccessful network calls |
| 19 | + - `ServerError`: Server errors (non 2xx responses) |
| 20 | + - `NetworkError`: IO Errors, connectivity problems |
| 21 | + - `UnknownError`: Any other errors, like serialization exceptions |
18 | 22 |
|
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. |
| 23 | +It is generic on two types: a success response (`S`), and an error response (`E`). |
20 | 24 |
|
21 | | -## Example |
| 25 | +- `S`: Kotlin representation of a successful API response |
| 26 | +- `E`: Representation of an unsuccessful API response |
22 | 27 |
|
23 | | -```kotlin |
24 | | -data class DetailsResponse( |
25 | | - val details: String |
26 | | -) |
| 28 | +## Usage |
| 29 | + |
| 30 | +Suppose an API returns the following body for a successful response: |
| 31 | + |
| 32 | +_Successful Response_ |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "name": "John doe", |
| 37 | + "age": 21 |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +And this for an unsuccessful response: |
27 | 42 |
|
28 | | -data class DetailsError( |
29 | | - val errorMessage: String |
30 | | -) |
| 43 | +_Error Response_ |
31 | 44 |
|
32 | | -interface Api { |
33 | | - @Get("/details") |
34 | | - suspend fun details(): NetworkResponse<DetailsResponse, DetailsError> |
| 45 | +```json |
| 46 | +{ |
| 47 | + "message": "The requested person was not found" |
35 | 48 | } |
| 49 | +``` |
| 50 | + |
| 51 | +You can create two data classes to model the these responses: |
| 52 | + |
| 53 | +```kotlin |
| 54 | +data class PersonResponse(val name: String, val age: Int) |
| 55 | + |
| 56 | +data class ErrorResponse(val message: String) |
| 57 | +``` |
| 58 | + |
| 59 | +Then modify your Retrofit service to return a `NetworkResponse`: |
| 60 | + |
| 61 | +```kotlin |
| 62 | +@GET("/person") |
| 63 | +suspend fun getPerson(): NetworkResponse<PersonResponse, ErrorResponse>> |
| 64 | + |
| 65 | +// You can also request for `Deferred` responses |
| 66 | +@GET("/person") |
| 67 | +fun getPersonAsync(): Deferred<NetworkResponse<PersonResponse, ErrorResponse>> |
| 68 | +``` |
36 | 69 |
|
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) |
| 70 | +Finally, add this call adapter factory to your Retrofit instance: |
| 71 | + |
| 72 | +```kotlin |
| 73 | +Retrofit.Builder() |
| 74 | + .addCallAdapterFactory(NetworkResponseAdapterFactory()) |
| 75 | + .build() |
| 76 | +``` |
| 77 | + |
| 78 | +And voila! You can now consume your API as: |
| 79 | + |
| 80 | +```kotlin |
| 81 | +// Repository.kt |
| 82 | +suspend fun getPerson() { |
| 83 | + when (val person = apiService.getPerson()) { |
| 84 | + is NetworkResponse.Success -> { |
| 85 | + /* Successful response */ |
| 86 | + } |
| 87 | + is NetworkResponse.Error -> { |
| 88 | + /* Handle error */ |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Or, if you care about the type of the error: |
| 93 | + when (val person = apiService.getPerson()) { |
| 94 | + is NetworkResponse.Success -> { |
| 95 | + /* ... */ |
| 96 | + } |
| 97 | + is NetworkResponse.ServerError -> { |
| 98 | + /* ... */ |
| 99 | + } |
| 100 | + is NetworkResponse.NetworkError -> { |
| 101 | + /* ... */ |
| 102 | + } |
| 103 | + is NetworkResponse.UnknownError -> { |
| 104 | + /* ... */ |
| 105 | + } |
44 | 106 | } |
45 | | - } |
46 | 107 | } |
47 | 108 | ``` |
48 | 109 |
|
| 110 | +See [Special Cases](./special-cases.md) for dealing with more complicated scenarios. |
| 111 | + |
| 112 | +## Benefits |
| 113 | + |
| 114 | +Modelling errors as a part of your state is a recommended practice. This library helps you deal with scenarios where you can successfully recover from errors, and extract meaningful information from them too! |
| 115 | + |
| 116 | +- `NetworkResponseAdapter` provides a much cleaner solution than Retrofit's built in `Call` type for dealing with errors.`Call` throws an exception on any kind of an error, leaving it up to you to catch it and parse it manually to figure out what went wrong. `NetworkResponseAdapter` does all of that for you and returns the result in an easily consumable `NetworkResponse` subtype. |
| 117 | + |
| 118 | +- The RxJava retrofit adapter treats non 2xx response codes as errors, which seems silly in the context of Rx where errors terminate streams. Also, just like the `Call<T>` type, it makes you deal with all types of errors in an `onError` callback, where you have to manually parse it to find out exactly what went wrong. |
| 119 | + |
| 120 | +- Using the `Response` class provided by Retrofit is cumbersome, as you have to manually parse error bodies with it. |
| 121 | + |
49 | 122 | ## Installation |
50 | 123 |
|
51 | 124 | Add the Jitpack repository to your list of repositories: |
52 | 125 |
|
53 | 126 | ```groovy |
54 | 127 | allprojects { |
55 | | - repositories { |
56 | | - maven { url 'https://jitpack.io' } |
57 | | - } |
| 128 | + repositories { |
| 129 | + maven { url 'https://jitpack.io' } |
| 130 | + } |
58 | 131 | } |
59 | 132 | ``` |
60 | 133 |
|
61 | | -Then add the dependency in your gradle file: |
| 134 | +And then add the dependency in your gradle file: |
62 | 135 |
|
63 | 136 | ```groovy |
64 | 137 | dependencies { |
65 | | - implementation "com.github.haroldadmin:NetworkResponseAdapter:(latest-version)" |
| 138 | + implementation "com.github.haroldadmin:NetworkResponseAdapter:(latest-version)" |
66 | 139 | } |
67 | 140 | ``` |
68 | 141 |
|
69 | | -And finally, register `NetworkResponseAdapter` with Retrofit: |
70 | | - |
71 | | -```kotlin |
72 | | -val retrofit = Retrofit.Builder() |
73 | | - .addCallAdapterFactory(NetworkResponseAdapterFactory()) |
74 | | - ... // Other config |
75 | | - .build() |
76 | | -``` |
| 142 | +[](https://jitpack.io/#haroldadmin/NetworkResponseAdapter) |
77 | 143 |
|
78 | | -<!-- prettier-ignore-start --> |
79 | | -!!! note |
80 | | - This library uses OkHttp 4, which requires Android API version 21+ and Java 8+. |
81 | | -<!-- prettier-ignore-end --> |
| 144 | +_This library uses OkHttp 4, which requires Android API version 21+ and Java 8+_ |
82 | 145 |
|
83 | 146 | ## License |
84 | 147 |
|
|
0 commit comments