-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44bc77a
commit 7b8793e
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...n/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} | ||
} | ||
} |