Skip to content

Commit

Permalink
Add BatchOrderApiResponse class
Browse files Browse the repository at this point in the history
  • Loading branch information
hafizrahman committed Dec 12, 2024
1 parent 44bc77a commit 7b8793e
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.wordpress.android.fluxc.network.rest.wpcom.wc.order

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.annotations.SerializedName
import java.lang.reflect.Type

data class BatchOrderApiResponse(
@SerializedName("update") val update: List<OrderResponse>
) {
sealed class OrderResponse {
data class Success(
val order: OrderDto
) : OrderResponse()

data class Error(
val id: Long,
val error: ErrorResponse
) : OrderResponse()
}

data class ErrorResponse(
val code: String,
val message: String,
val data: ErrorData
)

data class ErrorData(
val status: Int
)

class OrderResponseDeserializer : JsonDeserializer<OrderResponse> {
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): OrderResponse {
val jsonObject = json.asJsonObject

return if (jsonObject.has("error")) {
OrderResponse.Error(
id = jsonObject.get("id").asLong,
error = context.deserialize(jsonObject.get("error"), ErrorResponse::class.java)
)
} else {
OrderResponse.Success(
context.deserialize(jsonObject, OrderDto::class.java)
)
}
}
}
}

0 comments on commit 7b8793e

Please sign in to comment.