Skip to content

Commit 7278174

Browse files
committed
feat: Update README
1 parent d27b9ba commit 7278174

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ You may then write your API service as:
5252
// APIService.kt
5353

5454
@GET("<api-url>")
55-
fun getPerson(): Deferred<NetworkResponse<PersonResponse, ErrorResponse>
55+
fun getPerson(): Deferred<NetworkResponse<PersonResponse, ErrorResponse>>
5656

57+
// or if you want to use Suspending functions
58+
@GET("<api-url>")
59+
suspend fun getPerson(): NetworkResponse<PersonResponse, ErrorResponse>>
5760
```
5861

5962
Make sure to add this call adapter factory when building your Retrofit instance:
6063
```kotlin
6164
Retrofit.Builder()
62-
.addCallAdapterFactory(CoroutinesNetworkResponseAdapterFactory())
65+
.addCallAdapterFactory(NetworkResponseAdapterFactory())
6366
.build()
6467
```
6568

@@ -70,6 +73,9 @@ Then consume the API as follows:
7073

7174
suspend fun getPerson() {
7275
val person = apiService.getPerson().await()
76+
// or if you use suspending functions,
77+
val person = runBlocking { apiService.getPerson() }
78+
7379
when (person) {
7480
is NetworkResponse.Success -> {
7581
// Handle Success
@@ -91,14 +97,19 @@ suspend fun getPerson() {
9197
apiService.getPerson.await()
9298
}
9399

100+
// or with suspending functions
101+
val response = executeWithRetry(times = 5) {
102+
apiService.getPerson()
103+
}
104+
94105
// Then handle the response
95106
}
96107
```
97108

98109
There's also an overloaded invoke operator on the NetworkResponse class so that its success body can be accessed directly.
99110
```kotlin
100-
val usersResponse = usersRepo.getUsers().await() // Returns users if response is successful, or null otherwise
101-
println(usersResponse() ?: "No users were found")
111+
val usersResponse = usersRepo.getUsers().await()
112+
println(usersResponse() ?: "No users were found") // Returns users if response is successful, or null otherwise
102113
```
103114

104115
---

0 commit comments

Comments
 (0)