From 7b8793e22936377c7fd8591de884133b6ec487e3 Mon Sep 17 00:00:00 2001 From: Hafiz Rahman Date: Thu, 12 Dec 2024 16:06:10 +0700 Subject: [PATCH] Add BatchOrderApiResponse class --- .../wpcom/wc/order/BatchOrderApiResponse.kt | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponse.kt diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponse.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponse.kt new file mode 100644 index 0000000000..1e9cee4f80 --- /dev/null +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponse.kt @@ -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 +) { + 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 { + 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) + ) + } + } + } +}