-
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.
- Loading branch information
Showing
14 changed files
with
204 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package org.blackcandy.android.api | ||
|
||
class ApiException(message: String?) : Exception(message) | ||
class ApiException(val code: Int?, message: String?) : Exception(message) |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/org/blackcandy/android/api/ApiResponse.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,27 @@ | ||
package org.blackcandy.android.api | ||
|
||
import org.blackcandy.android.utils.TaskResult | ||
|
||
sealed interface ApiResponse<out T> { | ||
data class Success<T>(val data: T) : ApiResponse<T> | ||
|
||
data class Failure(val exception: ApiException) : ApiResponse<Nothing> | ||
|
||
fun orNull(): T? = | ||
when (this) { | ||
is Success -> data | ||
is Failure -> null | ||
} | ||
|
||
fun orThrow(): T = | ||
when (this) { | ||
is Success -> data | ||
is Failure -> throw exception | ||
} | ||
|
||
fun asResult(): TaskResult<T> = | ||
when (this) { | ||
is Success -> TaskResult.Success(data) | ||
is Failure -> TaskResult.Failure(exception.message) | ||
} | ||
} |
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
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
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
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
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
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
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
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
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,7 @@ | ||
package org.blackcandy.android.utils | ||
|
||
sealed interface TaskResult<out T> { | ||
data class Success<T>(val data: T) : TaskResult<T> | ||
|
||
data class Failure(val message: String?) : TaskResult<Nothing> | ||
} |
Oops, something went wrong.