Skip to content

Commit

Permalink
Change list of webhooks return type (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 authored Nov 30, 2023
1 parent 0d98159 commit 76d1207
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 56 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/easypost/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/easypost/model/CarbonOffset.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/com/easypost/model/Rate.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ public class Rate extends EasyPostResource {
private String shipmentId;
private String carrierAccountId;
private String billingType;
private CarbonOffset carbonOffset;
}
10 changes: 0 additions & 10 deletions src/main/java/com/easypost/model/WebhookCollection.java

This file was deleted.

31 changes: 31 additions & 0 deletions src/main/java/com/easypost/model/WebhookDeserializer.java
Original file line number Diff line number Diff line change
@@ -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<Webhook[]> {
/**
* 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);
}
}
14 changes: 8 additions & 6 deletions src/main/java/com/easypost/service/WebhookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,24 +54,25 @@ 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<Webhook> all() throws EasyPostException {
return all(null);
}

/**
* 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<String, Object> params) throws EasyPostException {
public List<Webhook> all(final Map<String, Object> 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);
}

/**
Expand Down
130 changes: 108 additions & 22 deletions src/test/cassettes/webhook/all.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/test/java/com/easypost/WebhookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,12 +107,15 @@ public void testRetrieve() throws EasyPostException {
@Test
public void testAll() throws EasyPostException {
vcr.setUpTest("all");
Map<String, Object> params = new HashMap<>();
params.put("url", Fixtures.webhookUrl());

WebhookCollection webhooks = vcr.client.webhook.all();
vcr.client.webhook.create(params);

List<Webhook> webhooksList = webhooks.getWebhooks();
List<Webhook> webhooks = vcr.client.webhook.all();

assertTrue(webhooksList.stream().allMatch(webhook -> webhook != null));
assertTrue(webhooks.size() > 0);
assertTrue(webhooks.stream().allMatch(webhook -> webhook != null));
}

/**
Expand Down

0 comments on commit 76d1207

Please sign in to comment.