This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Woo: Part one for orders/batch endpoint #3119
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cebfe75
Add /orders/batch endpoint
hafizrahman 44bc77a
Add sample response for PUT wc/v3/orders/batch with both success and …
hafizrahman 7b8793e
Add BatchOrderApiResponse class
hafizrahman 0a0d3eb
Add unit test for batch order response's deserializer.
hafizrahman 3c423f4
Remove unneeded Serialized name and add subclass to Response
hafizrahman 254cf5c
Directly register TypeAdapter with annotation.
hafizrahman 6e2cdef
Move unit test to its own file with the same package location as Batc…
hafizrahman 6a3a729
Remove unused import
hafizrahman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...java/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponseTest.kt
This file contains hidden or 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,32 @@ | ||
| package org.wordpress.android.fluxc.network.rest.wpcom.wc.order | ||
|
|
||
| import com.google.gson.GsonBuilder | ||
| import org.junit.Test | ||
| import org.wordpress.android.fluxc.UnitTestUtils | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class BatchOrderApiResponseTest { | ||
| @Test | ||
| fun testDeserializeBatchOrderResponse() { | ||
| val testGson = GsonBuilder() | ||
| .create() | ||
|
|
||
| val batchOrderJson = UnitTestUtils.getStringFromResourceFile( | ||
| this.javaClass, "wc/orders-batch.json" | ||
| ) | ||
|
|
||
| val response = testGson.fromJson(batchOrderJson, BatchOrderApiResponse::class.java) | ||
| val orders = response.update | ||
|
|
||
| assertEquals(2, orders.size) | ||
|
|
||
| val firstOrder = orders[0] as BatchOrderApiResponse.OrderResponse.Success | ||
| assertEquals(1032L, firstOrder.order.id) | ||
| assertEquals("224.00", firstOrder.order.total) | ||
|
|
||
| val secondOrder = orders[1] as BatchOrderApiResponse.OrderResponse.Error | ||
| assertEquals(525L, secondOrder.id) | ||
| assertEquals("woocommerce_rest_shop_order_invalid_id", secondOrder.error.code) | ||
| assertEquals(400, secondOrder.error.data.status) | ||
| } | ||
| } |
This file contains hidden or 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,160 @@ | ||
| { | ||
| "update": [ | ||
| { | ||
| "id": 1032, | ||
| "parent_id": 0, | ||
| "status": "completed", | ||
| "currency": "USD", | ||
| "version": "9.4.3", | ||
| "prices_include_tax": false, | ||
| "date_created": "2024-12-04T11:21:17", | ||
| "date_modified": "2024-12-04T11:23:19", | ||
| "discount_total": "0.00", | ||
| "discount_tax": "0.00", | ||
| "shipping_total": "3.00", | ||
| "shipping_tax": "0.00", | ||
| "cart_tax": "0.00", | ||
| "total": "224.00", | ||
| "total_tax": "0.00", | ||
| "customer_id": 0, | ||
| "order_key": "wc_order_XXXXXXXXXXXX", | ||
| "billing": { | ||
| "first_name": "", | ||
| "last_name": "", | ||
| "company": "", | ||
| "address_1": "", | ||
| "address_2": "", | ||
| "city": "", | ||
| "state": "", | ||
| "postcode": "", | ||
| "country": "", | ||
| "email": "", | ||
| "phone": "" | ||
| }, | ||
| "shipping": { | ||
| "first_name": "", | ||
| "last_name": "", | ||
| "company": "", | ||
| "address_1": "", | ||
| "address_2": "", | ||
| "city": "", | ||
| "state": "", | ||
| "postcode": "", | ||
| "country": "", | ||
| "phone": "" | ||
| }, | ||
| "payment_method": "", | ||
| "payment_method_title": "", | ||
| "transaction_id": "", | ||
| "customer_ip_address": "", | ||
| "customer_user_agent": "", | ||
| "created_via": "rest-api", | ||
| "customer_note": "Asdasd", | ||
| "date_completed": "2024-12-04T11:23:19", | ||
| "date_paid": "2024-12-04T11:23:19", | ||
| "cart_hash": "", | ||
| "number": "1032", | ||
| "meta_data": [ | ||
| { | ||
| "id": 18608, | ||
| "key": "_wc_order_attribution_source_type", | ||
| "value": "mobile_app" | ||
| } | ||
| ], | ||
| "line_items": [ | ||
| { | ||
| "id": 162, | ||
| "name": "Album", | ||
| "product_id": 71, | ||
| "variation_id": 0, | ||
| "quantity": 1, | ||
| "tax_class": "", | ||
| "subtotal": "216.00", | ||
| "subtotal_tax": "0.00", | ||
| "total": "216.00", | ||
| "total_tax": "0.00", | ||
| "taxes": [], | ||
| "meta_data": [], | ||
| "sku": "woo-album", | ||
| "price": 216, | ||
| "image": { | ||
| "id": "100", | ||
| "src": "https://example.com/images/album-1.jpg" | ||
| }, | ||
| "parent_name": null, | ||
| "bundled_by": "", | ||
| "bundled_item_title": "", | ||
| "bundled_items": [] | ||
| } | ||
| ], | ||
| "tax_lines": [], | ||
| "shipping_lines": [ | ||
| { | ||
| "id": 166, | ||
| "method_title": "Reg ship", | ||
| "method_id": "flat_rate", | ||
| "instance_id": "", | ||
| "total": "3.00", | ||
| "total_tax": "0.00", | ||
| "taxes": [], | ||
| "meta_data": [] | ||
| } | ||
| ], | ||
| "fee_lines": [ | ||
| { | ||
| "id": 165, | ||
| "name": "Just for you", | ||
| "tax_class": "", | ||
| "tax_status": "none", | ||
| "amount": "", | ||
| "total": "5.00", | ||
| "total_tax": "0.00", | ||
| "taxes": [], | ||
| "meta_data": [] | ||
| } | ||
| ], | ||
| "coupon_lines": [], | ||
| "refunds": [], | ||
| "payment_url": "https://example.com/checkout/order-pay/1032/", | ||
| "is_editable": false, | ||
| "needs_payment": false, | ||
| "needs_processing": true, | ||
| "date_created_gmt": "2024-12-04T04:21:17", | ||
| "date_modified_gmt": "2024-12-04T04:23:19", | ||
| "date_completed_gmt": "2024-12-04T04:23:19", | ||
| "date_paid_gmt": "2024-12-04T04:23:19", | ||
| "currency_symbol": "$", | ||
| "_links": { | ||
| "self": [ | ||
| { | ||
| "href": "https://example.com/wp-json/wc/v3/orders/1032", | ||
| "targetHints": { | ||
| "allow": [ | ||
| "GET", | ||
| "POST", | ||
| "PUT", | ||
| "PATCH", | ||
| "DELETE" | ||
| ] | ||
| } | ||
| } | ||
| ], | ||
| "collection": [ | ||
| { | ||
| "href": "https://example.com/wp-json/wc/v3/orders" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "id": "525", | ||
| "error": { | ||
| "code": "woocommerce_rest_shop_order_invalid_id", | ||
| "message": "Invalid ID.", | ||
| "data": { | ||
| "status": 400 | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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
55 changes: 55 additions & 0 deletions
55
...n/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/order/BatchOrderApiResponse.kt
This file contains hidden or 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,55 @@ | ||
| 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.JsonAdapter | ||
| import java.lang.reflect.Type | ||
| import org.wordpress.android.fluxc.network.Response | ||
|
|
||
| data class BatchOrderApiResponse( | ||
| val update: List<OrderResponse> | ||
| ) : Response { | ||
| @JsonAdapter(OrderResponseDeserializer::class) | ||
| 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 | ||
| ) | ||
|
|
||
| private 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) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor, I would make this
internal, I know we haven't been doing it often in the past, but I think it's still something we should consider for new features, as it would reduce leaking the networking models to the upper layers.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense. However when I do so after the last commit, 6e2cdef, the unit test is unable to see this class, even though the package location is now the same. Still see the same issue after adding
@VisibleForTesting, too.Not sure yet why this happens, do you know? I'll merge this PR for now to continue work and add this change later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because the test is defined in another module (
example), I think it would be better to move the test to the same moduleplugins/woocommerce