@@ -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
5962Make sure to add this call adapter factory when building your Retrofit instance:
6063``` kotlin
6164Retrofit .Builder ()
62- .addCallAdapterFactory(CoroutinesNetworkResponseAdapterFactory ())
65+ .addCallAdapterFactory(NetworkResponseAdapterFactory ())
6366 .build()
6467```
6568
@@ -70,6 +73,9 @@ Then consume the API as follows:
7073
7174suspend 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
98109There'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