Code Monkey home page Code Monkey logo

Comments (8)

skydoves avatar skydoves commented on May 26, 2024

Hi, could you let me know what kind of serialization library do you use? e.g., Gson, Moshi

from sandwich.

StrikeZXA avatar StrikeZXA commented on May 26, 2024

Gson or Moshi ,I can use them all

from sandwich.

skydoves avatar skydoves commented on May 26, 2024

I think this issue is related to the usage of Gson or Moshi.
Because converting json to object is their works.

from sandwich.

StrikeZXA avatar StrikeZXA commented on May 26, 2024

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.

skydoves avatar skydoves commented on May 26, 2024

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.

ldileh avatar ldileh commented on May 26, 2024

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>?

Screenshot 2023-12-01 135918

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.

ldileh avatar ldileh commented on May 26, 2024

BTW, this dependency is aweseom! it's really helped me with the development process that I was doing

from sandwich.

skydoves avatar skydoves commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.