From 76d12071a5efe50eaf0b4d1f8225f4cd4d934362 Mon Sep 17 00:00:00 2001 From: "Junjie(Jack) Chen" Date: Thu, 30 Nov 2023 12:11:11 -0500 Subject: [PATCH] Change list of webhooks return type (#294) --- CHANGELOG.md | 3 +- src/main/java/com/easypost/Constants.java | 5 +- .../java/com/easypost/model/CarbonOffset.java | 11 -- src/main/java/com/easypost/model/Rate.java | 1 - .../com/easypost/model/WebhookCollection.java | 10 -- .../easypost/model/WebhookDeserializer.java | 31 +++++ .../com/easypost/service/WebhookService.java | 14 +- src/test/cassettes/webhook/all.json | 130 +++++++++++++++--- src/test/java/com/easypost/WebhookTest.java | 10 +- 9 files changed, 159 insertions(+), 56 deletions(-) delete mode 100644 src/main/java/com/easypost/model/CarbonOffset.java delete mode 100644 src/main/java/com/easypost/model/WebhookCollection.java create mode 100644 src/main/java/com/easypost/model/WebhookDeserializer.java diff --git a/CHANGELOG.md b/CHANGELOG.md index e254475a4..5d85b6e7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ## Next Major Release -- Removed `withCarbonOffset` parameter from `create`, `buy`, and `regenerateRates` functions of the Shipment service as EasyPost now offers Carbon Neutral shipments by default for free +- Removes `withCarbonOffset` parameter from `create`, `buy`, and `regenerateRates` functions of the Shipment service as EasyPost now offers Carbon Neutral shipments by default for free - Removes the undocumented `createAndBuy` function from the Batch service. The proper usage is to create a batch first and buy it separately +- Changes return type of `all()` in webhook service from `WebhookCollection` to `a list of webhooks` ## v6.9.1 (2023-11-16) diff --git a/src/main/java/com/easypost/Constants.java b/src/main/java/com/easypost/Constants.java index 9e1a2af2a..75d11c7c7 100644 --- a/src/main/java/com/easypost/Constants.java +++ b/src/main/java/com/easypost/Constants.java @@ -9,6 +9,8 @@ import com.easypost.model.SmartrateCollectionDeserializer; import com.easypost.model.StatelessRate; import com.easypost.model.StatelessRateDeserializer; +import com.easypost.model.Webhook; +import com.easypost.model.WebhookDeserializer; import com.google.common.collect.ImmutableList; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; @@ -75,7 +77,8 @@ public abstract static class Http { .registerTypeAdapter(SmartrateCollection.class, new SmartrateCollectionDeserializer()) .registerTypeAdapter(Error.class, new ErrorDeserializer()) .registerTypeAdapter(AddressVerification.class, new AddressVerificationDeserializer()) - .registerTypeAdapter(StatelessRate[].class, new StatelessRateDeserializer()).create(); + .registerTypeAdapter(StatelessRate[].class, new StatelessRateDeserializer()) + .registerTypeAdapter(Webhook[].class, new WebhookDeserializer()).create(); public static final Gson PRETTY_PRINT_GSON = new GsonBuilder().setPrettyPrinting().serializeNulls() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); } diff --git a/src/main/java/com/easypost/model/CarbonOffset.java b/src/main/java/com/easypost/model/CarbonOffset.java deleted file mode 100644 index 28967f865..000000000 --- a/src/main/java/com/easypost/model/CarbonOffset.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.easypost.model; - -import lombok.Getter; - -@Getter -public final class CarbonOffset extends EasyPostResource { - private String currency; - private int grams; - private String price; - private String object; -} diff --git a/src/main/java/com/easypost/model/Rate.java b/src/main/java/com/easypost/model/Rate.java index 2bc2497e3..f691fa2d6 100644 --- a/src/main/java/com/easypost/model/Rate.java +++ b/src/main/java/com/easypost/model/Rate.java @@ -19,5 +19,4 @@ public class Rate extends EasyPostResource { private String shipmentId; private String carrierAccountId; private String billingType; - private CarbonOffset carbonOffset; } diff --git a/src/main/java/com/easypost/model/WebhookCollection.java b/src/main/java/com/easypost/model/WebhookCollection.java deleted file mode 100644 index ff90b2180..000000000 --- a/src/main/java/com/easypost/model/WebhookCollection.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.easypost.model; - -import java.util.List; - -import lombok.Getter; - -@Getter -public final class WebhookCollection extends EasyPostResource { - private List webhooks; -} diff --git a/src/main/java/com/easypost/model/WebhookDeserializer.java b/src/main/java/com/easypost/model/WebhookDeserializer.java new file mode 100644 index 000000000..e32224c03 --- /dev/null +++ b/src/main/java/com/easypost/model/WebhookDeserializer.java @@ -0,0 +1,31 @@ +package com.easypost.model; + +import com.google.gson.Gson; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +public final class WebhookDeserializer implements JsonDeserializer { + /** + * Deserialize a list of Webhook from a JSON object. + * + * @param json JSON object to deserialize. + * @param typeOfT Type of the object to deserialize. + * @param context Deserialization context. + * @return Deserialized Webhook object. + * @throws JsonParseException if the JSON object is not a valid Webhook. + */ + @Override + public Webhook[] deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) throws JsonParseException{ + JsonObject jo = json.getAsJsonObject(); + JsonElement results = jo.get("webhooks"); + Gson gson = new Gson(); + + return gson.fromJson(results, Webhook[].class); + } +} diff --git a/src/main/java/com/easypost/service/WebhookService.java b/src/main/java/com/easypost/service/WebhookService.java index d904467b6..e08663734 100644 --- a/src/main/java/com/easypost/service/WebhookService.java +++ b/src/main/java/com/easypost/service/WebhookService.java @@ -4,10 +4,11 @@ import com.easypost.http.Requestor; import com.easypost.http.Requestor.RequestMethod; import com.easypost.model.Webhook; -import com.easypost.model.WebhookCollection; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.List; public class WebhookService { private final EasyPostClient client; @@ -53,10 +54,10 @@ public Webhook retrieve(final String id) throws EasyPostException { /** * Get a list of all Webhook objects. * - * @return WebhookCollection object + * @return List of Webhook objects. * @throws EasyPostException when the request fails. */ - public WebhookCollection all() throws EasyPostException { + public List all() throws EasyPostException { return all(null); } @@ -64,13 +65,14 @@ public WebhookCollection all() throws EasyPostException { * Get a list of all Webhook objects. * * @param params params for request - * @return WebhookCollection object + * @return List of Webhook objects. * @throws EasyPostException when the request fails. */ - public WebhookCollection all(final Map params) throws EasyPostException { + public List all(final Map params) throws EasyPostException { String endpoint = "webhooks"; - return Requestor.request(RequestMethod.GET, endpoint, params, WebhookCollection.class, client); + Webhook[] response = Requestor.request(RequestMethod.GET, endpoint, params, Webhook[].class, client); + return Arrays.asList(response); } /** diff --git a/src/test/cassettes/webhook/all.json b/src/test/cassettes/webhook/all.json index 2c8c81fc2..899db84d4 100644 --- a/src/test/cassettes/webhook/all.json +++ b/src/test/cassettes/webhook/all.json @@ -1,80 +1,166 @@ [ { - "recordedAt": 1686160208, + "recordedAt": 1701296426, "request": { - "body": "", - "method": "GET", + "body": "{\n \"webhook\": {\n \"url\": \"http://example.com\"\n }\n}", + "method": "POST", "headers": { "Accept-Charset": [ "UTF-8" ], "User-Agent": [ "REDACTED" + ], + "Content-Type": [ + "application/json" ] }, "uri": "https://api.easypost.com/v2/webhooks" }, "response": { - "body": "{\n \"webhooks\": []\n}", + "body": "{\n \"mode\": \"test\",\n \"disabled_at\": null,\n \"created_at\": \"2023-11-29T22:20:26Z\",\n \"id\": \"hook_7eba01c88f0511ee96e52b0d27a10bfd\",\n \"url\": \"http://example.com\",\n \"object\": \"Webhook\"\n}", "httpVersion": null, "headers": { "null": [ - "HTTP/1.1 200 OK" + "HTTP/1.1 201 Created" ], "content-length": [ - "15" + "161" ], "expires": [ "0" ], "x-node": [ - "bigweb1nuq" + "bigweb33nuq" ], "x-frame-options": [ "SAMEORIGIN" ], - "x-download-options": [ - "noopen" + "x-backend": [ + "easypost" ], "x-permitted-cross-domain-policies": [ "none" ], - "x-backend": [ - "easypost" + "x-download-options": [ + "noopen" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" ], "pragma": [ "no-cache" ], - "strict-transport-security": [ - "max-age\u003d31536000; includeSubDomains; preload" + "x-content-type-options": [ + "nosniff" ], "x-xss-protection": [ "1; mode\u003dblock" ], + "x-ep-request-uuid": [ + "76e1c9566567b929e786a7bc0099f4f1" + ], + "x-proxied": [ + "extlb1nuq 003ad9bca0", + "intlb1nuq b3de2c47ef" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.148265" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202311250013-a0f06fbc2c-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 201, + "message": "Created" + }, + "uri": "https://api.easypost.com/v2/webhooks" + }, + "duration": 532 + }, + { + "recordedAt": 1701296426, + "request": { + "body": "", + "method": "GET", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ] + }, + "uri": "https://api.easypost.com/v2/webhooks" + }, + "response": { + "body": "{\n \"webhooks\": [\n {\n \"mode\": \"test\",\n \"disabled_at\": null,\n \"created_at\": \"2023-11-29T22:20:26Z\",\n \"id\": \"hook_7eba01c88f0511ee96e52b0d27a10bfd\",\n \"url\": \"http://example.com\",\n \"object\": \"Webhook\"\n }\n ]\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 200 OK" + ], + "content-length": [ + "176" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb38nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-backend": [ + "easypost" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-download-options": [ + "noopen" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "pragma": [ + "no-cache" + ], "x-content-type-options": [ "nosniff" ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], "x-ep-request-uuid": [ - "776d14356480c350f440b58e0020ba3a" + "76e1c95a6567b92ae786a7bd0099f5ad" ], "x-proxied": [ - "intlb1nuq 255dce5529", - "extlb1nuq 5ab12a3ed2" + "extlb1nuq 003ad9bca0", + "intlb1nuq b3de2c47ef" ], "referrer-policy": [ "strict-origin-when-cross-origin" ], "x-runtime": [ - "0.022129" + "0.026684" ], "content-type": [ "application/json; charset\u003dutf-8" ], - "etag": [ - "W/\"d03ff1e2df7ad33907b2086ec5f9e0a9\"" - ], "x-version-label": [ - "easypost-202306062224-41e583302e-master" + "easypost-202311250013-a0f06fbc2c-master" ], "cache-control": [ "private, no-cache, no-store" @@ -86,6 +172,6 @@ }, "uri": "https://api.easypost.com/v2/webhooks" }, - "duration": 327 + "duration": 415 } ] \ No newline at end of file diff --git a/src/test/java/com/easypost/WebhookTest.java b/src/test/java/com/easypost/WebhookTest.java index 31cc85718..5fa8b6592 100644 --- a/src/test/java/com/easypost/WebhookTest.java +++ b/src/test/java/com/easypost/WebhookTest.java @@ -3,7 +3,6 @@ import com.easypost.exception.EasyPostException; import com.easypost.model.Event; import com.easypost.model.Webhook; -import com.easypost.model.WebhookCollection; import com.easypost.utils.Utilities; import com.google.common.collect.ImmutableMap; import org.junit.jupiter.api.AfterEach; @@ -108,12 +107,15 @@ public void testRetrieve() throws EasyPostException { @Test public void testAll() throws EasyPostException { vcr.setUpTest("all"); + Map params = new HashMap<>(); + params.put("url", Fixtures.webhookUrl()); - WebhookCollection webhooks = vcr.client.webhook.all(); + vcr.client.webhook.create(params); - List webhooksList = webhooks.getWebhooks(); + List webhooks = vcr.client.webhook.all(); - assertTrue(webhooksList.stream().allMatch(webhook -> webhook != null)); + assertTrue(webhooks.size() > 0); + assertTrue(webhooks.stream().allMatch(webhook -> webhook != null)); } /**