From b4732bcd8b94da9de62160a9e356903693a716f0 Mon Sep 17 00:00:00 2001 From: <> Date: Mon, 10 Jun 2024 18:08:24 +0000 Subject: [PATCH] Deployed 19e0804 with MkDocs version: 1.6.0 --- .nojekyll | 0 404.html | 616 ++ apiresponse/index.html | 976 +++ assets/images/favicon.png | Bin 0 -> 1870 bytes assets/javascripts/bundle.ad660dcc.min.js | 29 + assets/javascripts/bundle.ad660dcc.min.js.map | 7 + assets/javascripts/lunr/min/lunr.ar.min.js | 1 + assets/javascripts/lunr/min/lunr.da.min.js | 18 + assets/javascripts/lunr/min/lunr.de.min.js | 18 + assets/javascripts/lunr/min/lunr.du.min.js | 18 + assets/javascripts/lunr/min/lunr.el.min.js | 1 + assets/javascripts/lunr/min/lunr.es.min.js | 18 + assets/javascripts/lunr/min/lunr.fi.min.js | 18 + assets/javascripts/lunr/min/lunr.fr.min.js | 18 + assets/javascripts/lunr/min/lunr.he.min.js | 1 + assets/javascripts/lunr/min/lunr.hi.min.js | 1 + assets/javascripts/lunr/min/lunr.hu.min.js | 18 + assets/javascripts/lunr/min/lunr.hy.min.js | 1 + assets/javascripts/lunr/min/lunr.it.min.js | 18 + assets/javascripts/lunr/min/lunr.ja.min.js | 1 + assets/javascripts/lunr/min/lunr.jp.min.js | 1 + assets/javascripts/lunr/min/lunr.kn.min.js | 1 + assets/javascripts/lunr/min/lunr.ko.min.js | 1 + assets/javascripts/lunr/min/lunr.multi.min.js | 1 + assets/javascripts/lunr/min/lunr.nl.min.js | 18 + assets/javascripts/lunr/min/lunr.no.min.js | 18 + assets/javascripts/lunr/min/lunr.pt.min.js | 18 + assets/javascripts/lunr/min/lunr.ro.min.js | 18 + assets/javascripts/lunr/min/lunr.ru.min.js | 18 + assets/javascripts/lunr/min/lunr.sa.min.js | 1 + .../lunr/min/lunr.stemmer.support.min.js | 1 + assets/javascripts/lunr/min/lunr.sv.min.js | 18 + assets/javascripts/lunr/min/lunr.ta.min.js | 1 + assets/javascripts/lunr/min/lunr.te.min.js | 1 + assets/javascripts/lunr/min/lunr.th.min.js | 1 + assets/javascripts/lunr/min/lunr.tr.min.js | 18 + assets/javascripts/lunr/min/lunr.vi.min.js | 1 + assets/javascripts/lunr/min/lunr.zh.min.js | 1 + assets/javascripts/lunr/tinyseg.js | 206 + assets/javascripts/lunr/wordcut.js | 6708 +++++++++++++++++ .../workers/search.b8dbb3d2.min.js | 42 + .../workers/search.b8dbb3d2.min.js.map | 7 + assets/stylesheets/main.6543a935.min.css | 1 + assets/stylesheets/main.6543a935.min.css.map | 1 + assets/stylesheets/palette.06af60db.min.css | 1 + .../stylesheets/palette.06af60db.min.css.map | 1 + empty-body/index.html | 666 ++ global/index.html | 782 ++ index.html | 860 +++ ktor/index.html | 847 +++ ktorfit/index.html | 789 ++ mapper/index.html | 954 +++ merge/index.html | 753 ++ migration/index.html | 806 ++ operator/index.html | 1065 +++ retrieve/index.html | 768 ++ retrofit/index.html | 996 +++ retry/index.html | 675 ++ search/search_index.json | 1 + sequential/index.html | 730 ++ sitemap.xml | 83 + sitemap.xml.gz | Bin 0 -> 322 bytes snapshot/index.html | 753 ++ sponsor/index.html | 728 ++ 64 files changed, 21138 insertions(+) create mode 100644 .nojekyll create mode 100644 404.html create mode 100644 apiresponse/index.html create mode 100644 assets/images/favicon.png create mode 100644 assets/javascripts/bundle.ad660dcc.min.js create mode 100644 assets/javascripts/bundle.ad660dcc.min.js.map create mode 100644 assets/javascripts/lunr/min/lunr.ar.min.js create mode 100644 assets/javascripts/lunr/min/lunr.da.min.js create mode 100644 assets/javascripts/lunr/min/lunr.de.min.js create mode 100644 assets/javascripts/lunr/min/lunr.du.min.js create mode 100644 assets/javascripts/lunr/min/lunr.el.min.js create mode 100644 assets/javascripts/lunr/min/lunr.es.min.js create mode 100644 assets/javascripts/lunr/min/lunr.fi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.fr.min.js create mode 100644 assets/javascripts/lunr/min/lunr.he.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hu.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hy.min.js create mode 100644 assets/javascripts/lunr/min/lunr.it.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ja.min.js create mode 100644 assets/javascripts/lunr/min/lunr.jp.min.js create mode 100644 assets/javascripts/lunr/min/lunr.kn.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ko.min.js create mode 100644 assets/javascripts/lunr/min/lunr.multi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.nl.min.js create mode 100644 assets/javascripts/lunr/min/lunr.no.min.js create mode 100644 assets/javascripts/lunr/min/lunr.pt.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ro.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ru.min.js create mode 100644 assets/javascripts/lunr/min/lunr.sa.min.js create mode 100644 assets/javascripts/lunr/min/lunr.stemmer.support.min.js create mode 100644 assets/javascripts/lunr/min/lunr.sv.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ta.min.js create mode 100644 assets/javascripts/lunr/min/lunr.te.min.js create mode 100644 assets/javascripts/lunr/min/lunr.th.min.js create mode 100644 assets/javascripts/lunr/min/lunr.tr.min.js create mode 100644 assets/javascripts/lunr/min/lunr.vi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.zh.min.js create mode 100644 assets/javascripts/lunr/tinyseg.js create mode 100644 assets/javascripts/lunr/wordcut.js create mode 100644 assets/javascripts/workers/search.b8dbb3d2.min.js create mode 100644 assets/javascripts/workers/search.b8dbb3d2.min.js.map create mode 100644 assets/stylesheets/main.6543a935.min.css create mode 100644 assets/stylesheets/main.6543a935.min.css.map create mode 100644 assets/stylesheets/palette.06af60db.min.css create mode 100644 assets/stylesheets/palette.06af60db.min.css.map create mode 100644 empty-body/index.html create mode 100644 global/index.html create mode 100644 index.html create mode 100644 ktor/index.html create mode 100644 ktorfit/index.html create mode 100644 mapper/index.html create mode 100644 merge/index.html create mode 100644 migration/index.html create mode 100644 operator/index.html create mode 100644 retrieve/index.html create mode 100644 retrofit/index.html create mode 100644 retry/index.html create mode 100644 search/search_index.json create mode 100644 sequential/index.html create mode 100644 sitemap.xml create mode 100644 sitemap.xml.gz create mode 100644 snapshot/index.html create mode 100644 sponsor/index.html diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 00000000..e69de29b diff --git a/404.html b/404.html new file mode 100644 index 00000000..06033903 --- /dev/null +++ b/404.html @@ -0,0 +1,616 @@ + + + +
+ + + + + + + + + + + + + + + + + + +ApiResponse
serves as an interface designed to create consistent responses from API or I/O calls, such as network, database, or whatever. It offers convenient extensions to manage your payloads, encompassing both body data and exceptional scenarios. ApiResponse
encompasses three distinct types: Success, Failure.Error, and Failure.Exception.
This represents a successful response from API or I/O tasks. You can create an instance of [ApiResponse.Success] by giving the generic type and data.
+val apiResponse = ApiResponse.Success(data = myData)
+val data = apiResponse.data
+
Depending on your model designs, you can also utilize tag
property. The tag
is an additional value that can be held to distinguish the origin of the [data] or to facilitate post-processing of successful data.
val apiResponse = ApiResponse.Success(data = myData, tag = myTag)
+val tag = apiResponse.tag
+
This signals a failed tasks captured by unexpected exceptions during API request creation or response processing on the client side, such as a network connection failure. You can obtain exception details from the ApiResponse.Failure.Exception
.
val apiResponse = ApiResponse.Failure.Exception(exception = HttpTimeoutException())
+val exception = apiResponse.exception
+val message = apiResponse.message
+
This denotes a failed API or I/O request, typically due to bad requests or internal server errors. You can additionally put an error payload that can contain detailed error information.
+val apiResponse = ApiResponse.Failure.Error(payload = errorBody)
+val payload = apiResponse.payload
+
You can also define custom error responses that extend ApiResponse.Failure.Error
or ApiResponse.Failure.Exception
, as demonstrated in the example below:
data object LimitedRequest : ApiResponse.Failure.Error(
+ payload = "your request is limited",
+)
+
+data object WrongArgument : ApiResponse.Failure.Error(
+ payload = "wrong argument",
+)
+
+data object HttpException : ApiResponse.Failure.Exception(
+ throwable = RuntimeException("http exception")
+)
+
The custom error response is very useful when you want to explicitly define and handle error responses, especially when working with map extensions.
+val apiResponse = service.fetchMovieList()
+apiResponse.onSuccess {
+ // ..
+}.flatMap {
+ // if the ApiResponse is Failure.Error and contains error body, then maps it to a custom error response.
+ if (this is ApiResponse.Failure.Error) {
+ val errorBody = (payload as? Response)?.body?.string()
+ if (errorBody != null) {
+ val errorMessage: ErrorMessage = Json.decodeFromString(errorBody)
+ when (errorMessage.code) {
+ 10000 -> LimitedRequest
+ 10001 -> WrongArgument
+ }
+ }
+ }
+ this
+}
+
Then you can handle the errors based on your custom message in other layers:
+val apiResponse = repository.fetchMovieList()
+apiResponse.onError {
+ when (this) {
+ LimitedRequest -> // update your UI
+ WrongArgument -> // update your UI
+ }
+}
+
You might not want to use the flatMap
extension for all API requests. If you aim to standardize custom error types across all API requests, you can explore the Global Failure Mapper.
Sandwich provides convenient ways to create an ApiResponse
using functions such as ApiResponse.of
or apiResponseOf
, as shown below:
val apiResponse = ApiResponse.of { service.request() }
+val apiResponse = apiResponseOf { service.request() }
+
If you need to run suspend functions inside the lambda, you can use ApiResponse.suspendOf
or suspendApiResponseOf
instead:
val apiResponse = ApiResponse.suspendOf { service.request() }
+val apiResponse = suspendApiResponseOf { service.request() }
+
Note
+If you intend to utilize the global operator or global ApiResponse mapper in Sandwich, you should create an ApiResponse
using the ApiResponse.of
method to ensure the application of these global functions.
You can effectively handling ApiResponse
using the following extensions:
ApiResponse
is of type ApiResponse.Success
. Within this scope, you can directly access the body data.ApiResponse
is of type ApiResponse.Failure.Error
. Here, you can access the messareOrNull
and payload
here.ApiResponse
is of type ApiResponse.Failure.Exception
. You can access the messareOrNull
and exception
here.ApiResponse
is either ApiResponse.Failure.Error
or ApiResponse.Failure.Exception
. You can access the messareOrNull
here.Each scope operates according to its corresponding ApiResponse
type:
val response = disneyService.fetchDisneyPosterList()
+response.onSuccess {
+ // this scope will be executed if the request successful.
+ // handle the success case
+ }.onError {
+ // this scope will be executed when the request failed with errors.
+ // handle the error case
+ }.onException {
+ // this scope will be executed when the request failed with exceptions.
+ // handle the exception case
+ }
+
If you don't want to specify each failure case, you can simplify it by using the onFailure
extension:
val response = disneyService.fetchDisneyPosterList()
+response.onSuccess {
+ // this scope will be executed if the request successful.
+ // handle the success case
+ }.onFailure {
+
+ }
+
With the ApiResponse
type, you can leverage Coroutines extensions to handle responses seamlessly within coroutine scopes. These extensions provide a convenient way to process different response types. Here's how you can use them:
suspendOnSuccess: This extension runs if the ApiResponse
is of type ApiResponse.Success
. You can access the body data directly within this scope.
suspendOnError: This extension is executed if the ApiResponse
is of type ApiResponse.Failure.Error
. You can access the error message and the error body in this scope.
suspendOnException: If the ApiResponse
is of type ApiResponse.Failure.Exception
, this extension is triggered. You can access the exception message in this scope.
suspendOnFailure: This extension is executed if the ApiResponse
is either ApiResponse.Failure.Error
or ApiResponse.Failure.Exception
. You can access the error message in this scope.
Each extension scope operates based on the corresponding ApiResponse
type. By utilizing these extensions, you can handle responses effectively within different coroutine contexts.
flow {
+ val response = disneyService.fetchDisneyPosterList()
+ response.suspendOnSuccess {
+ posterDao.insertPosterList(data) // insertPosterList(data) is a suspend function.
+ emit(data)
+ }.suspendOnError {
+ // handles error cases
+ }.suspendOnException {
+ // handles exceptional cases
+ }
+}.flowOn(Dispatchers.IO)
+
Sandwich offers some useful extensions to transform your ApiResponse
into a Flow by using the toFlow
extension:
val flow = disneyService.fetchDisneyPosterList()
+ .onError {
+ // handles error cases when the API request gets an error response.
+ }.onException {
+ // handles exceptional cases when the API request gets an exception response.
+ }.toFlow() // returns a coroutines flow
+ .flowOn(Dispatchers.IO)
+
If you want to transform the original data and work with a Flow
containing the transformed data, you can do so as shown in the examples below:
val response = pokedexClient.fetchPokemonList(page = page)
+response.toFlow { pokemons ->
+ pokemons.forEach { pokemon -> pokemon.page = page }
+ pokemonDao.insertPokemonList(pokemons)
+ pokemonDao.getAllPokemonList(page)
+}.flowOn(Dispatchers.IO)
+