-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension functions suspendSafeResult and toSafeResult (#47)
- Loading branch information
1 parent
12fb207
commit d4168a3
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
core/network/src/main/kotlin/xyz/ksharma/krail/network/ResponseExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package xyz.ksharma.krail.network | ||
|
||
import retrofit2.Response | ||
|
||
/** | ||
* Processes a Retrofit response and returns a [Result] object. | ||
* | ||
* If the response is successful, the body is extracted and wrapped in a [Result.success]. | ||
* If the response is unsuccessful, an [Exception] is created with the error code and wrapped in a [Result.failure]. | ||
* | ||
* @return A [Result] object containing the response body or an error. | ||
*/ | ||
fun <T> Response<T>.toSafeResult(): Result<T> { | ||
return if (this.isSuccessful) { | ||
this.body()?.let { body -> | ||
Result.success(body) | ||
} ?: Result.failure(Exception("Response body is null")) | ||
} else { | ||
Result.failure(Exception("API call failed with error: ${this.code()}")) | ||
} | ||
} |
Empty file.