Comments (8)
Hi, could you let me know what kind of serialization library do you use? e.g., Gson, Moshi
from sandwich.
Gson or Moshi ,I can use them all
from sandwich.
I think this issue is related to the usage of Gson
or Moshi
.
Because converting json to object is their works.
from sandwich.
That way I can't request in Sandwich like Pokedex App.
In a sense,I'm going to have more trouble using Sandwich.
I don't want to every time in the response. SuspendOnSuccess conversion operation.
Does Sandwich support directly returning the data field?
Now MyApp request like this
`viewModelScope.launch {
val result = appDataManager.requestVerifyCode(itac, receiveTarget)
if (result is Result.Success) {
} else if (result is Result.Error) {
}
}`
suspend fun requestVerifyCode(itac: String, receiveTarget: String): Result<VerifyCodeEntity> { return safeApiCall( call = { executeResponse(apiService.requestVerifyCode(itac,receiveTarget)) }, errorMessage = "Network Error" ) }
`
data class LazyEntity(val code: Int, val msg: String, val data: T)
suspend fun safeApiCall(call: suspend () -> Result, errorMessage: String): Result {
return try {
call()
} catch (e: Exception) {
// An exception was thrown when calling the API so we're converting this to an IOException
Logger.i("Exception = ${e.message}")
Result.Error(IOException(errorMessage, e))
}
}
suspend fun executeResponse(
response: LazyEntity,
successBlock: (suspend CoroutineScope.() -> Unit)? = null,
errorBlock: (suspend CoroutineScope.() -> Unit)? = null
): Result {
return coroutineScope {
if (response.code == 0) {
errorBlock?.let { it() }
Logger.i("Exception = ${response.msg}")
Result.Error(IOException(response.msg))
} else {
successBlock?.let { it() }
Result.Success(response.data)
}
}
}`
@Headers("Content-Type: application/json") @POST("db-api/general/sms/verify_code") suspend fun requestVerifyCode( @Field("itac") type: String, @Field("receiveTarget") receiveTarget: String, ): LazyEntity<VerifyCodeEntity>
`sealed class Result {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val exception: Exception,val code:Int = 404) : Result<Nothing>()
override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
}
}
}`
So I think it's going to be more troublesome to use Sandwich in my app
from sandwich.
Hi, sorry I don't understand what you want to resolve.
As I understand, you can handle the response based on its type like the below?
val response = disneyService.fetchDisneyPosterList()
when (response) {
is ApiResponse.Success -> // do something
is ApiRespnse.Error.Failure -> //
is ApiResponse.Error.Exception -> //
}
Also, you can use like the below using if statement.
val response = disneyService.fetchDisneyPosterList()
if (response is ApiResponse.Success) {
// do something
}
from sandwich.
hello @skydoves, i had a case like this. the endpoint return the json with template like @dempsey. my problem is when I created the base class for the response it's working. but when I'm trying to use runAndRetry extension, it's show the warning :
Type mismatch. Required: Any Found: BaseResponse<TokenResponseModel>?
but when i change the body without the BaseResponse model, the 'runAndRetry ' method working well without the warning show up.
how do i solve this?
this is my base response model class
@Keep @Serializable data class BaseResponse<T>( @SerializedName("error") val error: ErrorModel? = null, @SerializedName("success") val success: Boolean?, @SerializedName("data") val data: T? = null )
from sandwich.
BTW, this dependency is aweseom! it's really helped me with the development process that I was doing
from sandwich.
Hey @ldileh, what is the return type of the otpVerification
method? You should return non-nullable type for using it.
from sandwich.
Related Issues (20)
- After enable R8 full mode getting ParameterizedType error HOT 7
- Maven Central Gradle 8 compatibility
- How to handle data structures like BaseResponse HOT 2
- Integrate Benchmark + Baseline Profiles
- Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option HOT 1
- Interface can't be instantiated! Register an InstanceCreator or a TypeAdapter for this type. HOT 2
- Difference Exception Handling between Ktor and Retrofit HOT 1
- ApiResponseCallAdapterFactory is missing in 2.0.4 HOT 2
- runAndRetry not working when type of ApiResponse had type variable HOT 8
- On Failure should return the Failure Sealed class HOT 5
- Support usage in vanilla JVM projects HOT 3
- Change parameter for map in ApiSuccessModelMapper HOT 1
- [ASK] Is there any feature that is equivalent to Single in sandwich? HOT 4
- [ASK] How can I handle 200 but error case? HOT 3
- [ASK] How can I get throw exception as onError/onFailure/onException in ViewModel ? HOT 1
- How to get custom error from my server? HOT 1
- [Help] Is it the best approach to validate responses? HOT 2
- Dependency Dashboard
- EmptyBodyInterceptor doesn't work with GsonConverterFactory HOT 6
- java.lang.NullPointerException:::sandwich.ApiResponse$Failure$Error.toString HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sandwich.