Skip to content

Commit bc6f3a5

Browse files
committed
feat: Update docs for v5
1 parent 2386f17 commit bc6f3a5

File tree

7 files changed

+214
-207
lines changed

7 files changed

+214
-207
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ gradle-app.setting
4949
.gradletasknamecache
5050

5151
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
52-
# gradle/wrapper/gradle-wrapper.properties
52+
# gradle/wrapper/gradle-wrapper.properties

README.md

Lines changed: 8 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
[![Build Status](https://github.com/haroldadmin/networkresponseadapter/workflows/CI/badge.svg)](https://github.com/haroldadmin/networkresponseadapter/actions)
44

5-
https://haroldadmin.github.io/NetworkResponseAdapter/
6-
75
A call adapter that handles errors as a part of state
86

7+
## Documentation
8+
9+
[**https://haroldadmin.github.io/NetworkResponseAdapter**](https://haroldadmin.github.io/NetworkResponseAdapter)
10+
911
---
1012

1113
This library provides a Kotlin Coroutines based Retrofit call adapter for wrapping your API responses in
@@ -17,149 +19,16 @@ a `NetworkResponse` type.
1719

1820
- `Success`: Represents successful network calls (2xx response codes)
1921
- `Error`: Represents unsuccessful network calls
20-
- `ServerError`: Server errors (non 2xx responses)
21-
- `NetworkError`: IO Errors, connectivity problems
22-
- `UnknownError`: Any other errors, like serialization exceptions
22+
- `ServerError`: Server errors (non 2xx responses)
23+
- `NetworkError`: IO Errors, connectivity problems
24+
- `UnknownError`: Any other errors, like serialization exceptions
2325

2426
It is generic on two types: a success response (`S`), and an error response (`E`).
2527

2628
- `S`: Kotlin representation of a successful API response
2729
- `E`: Representation of an unsuccessful API response
2830

29-
## Usage
30-
31-
Suppose an API returns the following body for a successful response:
32-
33-
_Successful Response_
34-
35-
```json
36-
{
37-
"name": "John doe",
38-
"age": 21
39-
}
40-
```
41-
42-
And this for an unsuccessful response:
43-
44-
_Error Response_
45-
46-
```json
47-
{
48-
"message": "The requested person was not found"
49-
}
50-
```
51-
52-
You can create two data classes to model the these responses:
53-
54-
```kotlin
55-
data class PersonResponse(val name: String, val age: Int)
56-
57-
data class ErrorResponse(val message: String)
58-
```
59-
60-
Then modify your Retrofit service to return a `NetworkResponse`:
61-
62-
```kotlin
63-
@GET("/person")
64-
suspend fun getPerson(): NetworkResponse<PersonResponse, ErrorResponse>>
65-
66-
// You can also request for `Deferred` responses
67-
@GET("/person")
68-
fun getPersonAsync(): Deferred<NetworkResponse<PersonResponse, ErrorResponse>>
69-
```
70-
71-
Finally, add this call adapter factory to your Retrofit instance:
72-
73-
```kotlin
74-
Retrofit.Builder()
75-
.addCallAdapterFactory(NetworkResponseAdapterFactory())
76-
.build()
77-
```
78-
79-
And voila! You can now consume your API as:
80-
81-
```kotlin
82-
// Repository.kt
83-
suspend fun getPerson() {
84-
when (val person = apiService.getPerson()) {
85-
is NetworkResponse.Success -> {
86-
/* Successful response */
87-
}
88-
is NetworkResponse.Error -> {
89-
/* Handle error */
90-
}
91-
}
92-
93-
// Or, if you care about the type of the error:
94-
when (val person = apiService.getPerson()) {
95-
is NetworkResponse.Success -> {
96-
/* ... */
97-
}
98-
is NetworkResponse.ServerError -> {
99-
/* ... */
100-
}
101-
is NetworkResponse.NetworkError -> {
102-
/* ... */
103-
}
104-
is NetworkResponse.UnknownError -> {
105-
/* ... */
106-
}
107-
}
108-
}
109-
```
110-
111-
## Utilities
112-
113-
**Retry Failed Network Calls**
114-
115-
Use the included utility function `executeWithRetry` to automatically retry your network requests if they result in
116-
a `NetworkResponse.NetworkError`
117-
118-
```kotlin
119-
suspend fun getPerson() {
120-
val response = executeWithRetry(times = 5) {
121-
apiService.getPerson()
122-
}
123-
}
124-
```
125-
126-
**Overloaded `invoke()` on `NetworkResponse`**
127-
128-
The `NetworkResponse` interface has an overloaded `invoke()` operator that returns the success body if the request was
129-
successful, or null otherwise
130-
131-
```kotlin
132-
val usersResponse = usersRepo.getUsers()
133-
println(usersResponse() ?: "No users were found")
134-
```
135-
136-
**Handle Empty Response Bodies**
137-
138-
Some API responses convey information through headers only and contain empty bodies. Use `Unit` to represent the success
139-
response type of such network calls.
140-
141-
```kotlin
142-
@DELETE("/person")
143-
suspend fun deletePerson(): NetworkResponse<Unit, ErrorType>
144-
```
145-
146-
---
147-
148-
## Benefits
149-
150-
This library helps you deal with scenarios where you can successfully recover from errors, and extract meaningful
151-
information from them too!
152-
153-
- `NetworkResponseAdapter` provides a much cleaner solution than Retrofit's built in `Call` type for dealing with
154-
errors.`Call` throws an exception on any kind of error, leaving it up to you to catch it and parse it manually to
155-
figure out what went wrong. `NetworkResponseAdapter` does all of that for you and returns the result in an easily
156-
consumable `NetworkResponse` type.
157-
158-
- The RxJava retrofit adapter treats non 2xx response codes as errors, which seems silly in the context of Rx where
159-
errors terminate streams. Also, just like the `Call<T>` type, it makes you deal with all types of errors in
160-
an `onError` callback, where you have to manually parse it to find out exactly what went wrong.
161-
162-
- Using the `Response` class provided by Retrofit is cumbersome, as you have to manually parse error bodies with it.
31+
Find complete documentation at [**https://haroldadmin.github.io/NetworkResponseAdapter**](https://haroldadmin.github.io/NetworkResponseAdapter).
16332

16433
## Installation
16534

@@ -181,8 +50,6 @@ dependencies {
18150
}
18251
```
18352

184-
_This library uses OkHttp 4, which requires Android API version 21+ and Java 8+_
185-
18653
[![Release](https://jitpack.io/v/haroldadmin/NetworkResponseAdapter.svg)](https://jitpack.io/#haroldadmin/NetworkResponseAdapter)
18754

18855
## License

docs/docs/benefits.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/docs/index.md

Lines changed: 106 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,143 @@
55

66
## Introduction
77

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.
912

1013
## Network Response
1114

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:
1316

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
1822

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`).
2024

21-
## Example
25+
- `S`: Kotlin representation of a successful API response
26+
- `E`: Representation of an unsuccessful API response
2227

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:
2742

28-
data class DetailsError(
29-
val errorMessage: String
30-
)
43+
_Error Response_
3144

32-
interface Api {
33-
@Get("/details")
34-
suspend fun details(): NetworkResponse<DetailsResponse, DetailsError>
45+
```json
46+
{
47+
"message": "The requested person was not found"
3548
}
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+
```
3669

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+
}
44106
}
45-
}
46107
}
47108
```
48109

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+
49122
## Installation
50123

51124
Add the Jitpack repository to your list of repositories:
52125

53126
```groovy
54127
allprojects {
55-
repositories {
56-
maven { url 'https://jitpack.io' }
57-
}
128+
repositories {
129+
maven { url 'https://jitpack.io' }
130+
}
58131
}
59132
```
60133

61-
Then add the dependency in your gradle file:
134+
And then add the dependency in your gradle file:
62135

63136
```groovy
64137
dependencies {
65-
implementation "com.github.haroldadmin:NetworkResponseAdapter:(latest-version)"
138+
implementation "com.github.haroldadmin:NetworkResponseAdapter:(latest-version)"
66139
}
67140
```
68141

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+
[![Release](https://jitpack.io/v/haroldadmin/NetworkResponseAdapter.svg)](https://jitpack.io/#haroldadmin/NetworkResponseAdapter)
77143

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+_
82145

83146
## License
84147

0 commit comments

Comments
 (0)