Skip to content

Commit c607fc9

Browse files
committed
Webhooks resourceresponses, WebhookDescription class
1 parent ad1f0f6 commit c607fc9

File tree

8 files changed

+121
-60
lines changed

8 files changed

+121
-60
lines changed
Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
1-
21
package com.sparkpost.model;
32

4-
import java.util.Arrays;
5-
import java.util.List;
6-
73
import com.google.gson.annotations.SerializedName;
84
import com.yepher.jsondoc.annotations.Description;
9-
105
import lombok.Data;
116
import lombok.EqualsAndHashCode;
127

13-
/**
14-
* DTO for storing info about a webhook.
15-
*
16-
* @author grava
17-
*/
188
@Data
199
@EqualsAndHashCode(callSuper = true)
20-
public class Webhook extends Base {
10+
public class Webhook extends WebhookDescription {
2111

2212
@Description(value = "Webhook id")
2313
private String id;
2414

25-
@Description(value = "User-friendly name", sample = {"Inbound Customer Replies"})
26-
private String name;
27-
28-
@Description(value = "URL of the target to which to POST relay batches", sample = {"https://webhooks.customer.example/replies"})
29-
private String target;
30-
3115
@Description(value = "Type of authentication to be used during POST requests to target.", sample = {"none", "basic", "oauth2"})
3216
@SerializedName("auth_type")
3317
private String authType;
@@ -41,19 +25,4 @@ public class Webhook extends Base {
4125
@SerializedName("auth_credentials")
4226
private AuthCredentials authCredentials;
4327

44-
@Deprecated
45-
@Description(
46-
value = "Authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target",
47-
sample = {"5ebe2294ecd0e0f08eab7690d2a6ee69"})
48-
@SerializedName("auth_token")
49-
private String authToken;
50-
51-
@Description(value = "Array of events", sample = {""})
52-
private List<String> events;
53-
54-
@Description(
55-
value = "Restrict which inbound messages will be relayed to the target",
56-
sample = {"\"match\": { \"protocol\": \"SMTP\", \"domain\": \"replies.customer.example\" }"})
57-
private Match match;
58-
5928
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
package com.sparkpost.model;
3+
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import com.google.gson.annotations.SerializedName;
8+
import com.yepher.jsondoc.annotations.Description;
9+
10+
import lombok.Data;
11+
import lombok.EqualsAndHashCode;
12+
13+
/**
14+
* DTO for storing info about a webhook.
15+
*
16+
* @author grava
17+
*/
18+
@Data
19+
@EqualsAndHashCode(callSuper = true)
20+
public class WebhookDescription extends Base {
21+
22+
@Description(value = "User-friendly name", sample = {"Inbound Customer Replies"})
23+
private String name;
24+
25+
@Description(value = "URL of the target to which to POST relay batches", sample = {"https://webhooks.customer.example/replies"})
26+
private String target;
27+
28+
@Deprecated
29+
@Description(
30+
value = "Authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target",
31+
sample = {"5ebe2294ecd0e0f08eab7690d2a6ee69"})
32+
@SerializedName("auth_token")
33+
private String authToken;
34+
35+
@Description(value = "Array of events", sample = {""})
36+
private List<String> events;
37+
38+
@Description(
39+
value = "Restrict which inbound messages will be relayed to the target",
40+
sample = {"\"match\": { \"protocol\": \"SMTP\", \"domain\": \"replies.customer.example\" }"})
41+
private Match match;
42+
43+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
@Data
88
@EqualsAndHashCode(callSuper = true)
9-
public class WebhookCreateResponseEntry extends Base {
9+
public class WebhookIdResponseEntry extends Base {
1010

11-
@Description(value = "Created webhook id.")
11+
@Description(value = "Created or updated webhook id.")
1212
private String id;
1313
}

libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.sparkpost.model.responses;
2+
3+
import com.sparkpost.model.Webhook;
4+
import com.yepher.jsondoc.annotations.Description;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
8+
@Data
9+
@EqualsAndHashCode(callSuper = true)
10+
public class WebhookDescribeResponse extends Response {
11+
12+
@Description(value = "", sample = "")
13+
private Webhook results;
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sparkpost.model.responses;
2+
3+
import com.sparkpost.model.WebhookIdResponseEntry;
4+
import com.yepher.jsondoc.annotations.Description;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
8+
@Data
9+
@EqualsAndHashCode(callSuper = true)
10+
public class WebhookIdContainerResponse extends Response {
11+
12+
13+
@Description(value = "Created or updated webhook id description")
14+
WebhookIdResponseEntry results;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sparkpost.resources;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
6+
public class ResourceBase {
7+
8+
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
9+
private static final Gson GSON = new GsonBuilder().setDateFormat(DATE_TIME_FORMAT).create();
10+
11+
protected static String getObjectAsJsonForClass(final Object object, final Class tClass) {
12+
return GSON.toJson(object, tClass);
13+
}
14+
15+
}

libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceWebhooks.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import com.sparkpost.exception.SparkPostException;
55
import com.sparkpost.model.Webhook;
6+
import com.sparkpost.model.WebhookDescription;
67
import com.sparkpost.model.responses.Response;
7-
import com.sparkpost.model.responses.WebhookCreateResponse;
8+
import com.sparkpost.model.responses.WebhookDescribeResponse;
9+
import com.sparkpost.model.responses.WebhookIdContainerResponse;
810
import com.sparkpost.model.responses.WebhookListAllResponse;
911
import com.sparkpost.transport.RestConnection;
1012

@@ -16,7 +18,9 @@
1618
*
1719
* @author grava
1820
*/
19-
public class ResourceWebhooks {
21+
public class ResourceWebhooks extends ResourceBase {
22+
23+
private static final String DEFAULT_TIMEZONE = "UTC";
2024

2125
public static Response listSampleValuesAndEvents(RestConnection conn) throws SparkPostException {
2226
Response response = conn.get("webhooks/events/documentation");
@@ -31,6 +35,10 @@ public static Response getSamplePayloadForEvents(RestConnection conn, String eve
3135
return response;
3236
}
3337

38+
public static WebhookListAllResponse listAll(RestConnection conn) throws SparkPostException {
39+
return listAll(conn, DEFAULT_TIMEZONE);
40+
}
41+
3442
public static WebhookListAllResponse listAll(RestConnection conn, String timezone) throws SparkPostException {
3543

3644
Endpoint ep = new Endpoint("webhooks");
@@ -40,27 +48,39 @@ public static WebhookListAllResponse listAll(RestConnection conn, String timezon
4048
return allWebhooks;
4149
}
4250

43-
public static WebhookCreateResponse create(RestConnection conn, Webhook webhook) throws SparkPostException {
51+
public static WebhookIdContainerResponse create(RestConnection conn, Webhook webhook) throws SparkPostException {
4452

4553
String json = webhook.toJson();
4654
Response response = conn.post("webhooks", json);
47-
WebhookCreateResponse webhookCreateResponse = WebhookCreateResponse.decode(response, WebhookCreateResponse.class);
48-
return webhookCreateResponse;
55+
WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode(
56+
response,
57+
WebhookIdContainerResponse.class
58+
);
59+
return webhookIdContainerResponse;
60+
}
61+
62+
public static WebhookDescribeResponse describe(RestConnection conn, String id) throws SparkPostException {
63+
return describe(conn, id, DEFAULT_TIMEZONE);
4964
}
5065

51-
public static Response describe(RestConnection conn, String id, String timezone) throws SparkPostException {
66+
public static WebhookDescribeResponse describe(RestConnection conn, String id, String timezone) throws SparkPostException {
5267

5368
Endpoint ep = new Endpoint("webhooks/" + id);
5469
ep.addParam("timezone", timezone);
5570
Response response = conn.get(ep.toString());
56-
return response;
71+
WebhookDescribeResponse webhookDescribeResponse = WebhookDescribeResponse.decode(response, WebhookDescribeResponse.class);
72+
return webhookDescribeResponse;
5773
}
5874

59-
public static Response update(RestConnection conn, String id, Webhook webhook) throws SparkPostException {
75+
public static WebhookIdContainerResponse update(RestConnection conn, String id, WebhookDescription webhookDescription) throws SparkPostException {
6076

61-
String json = webhook.toJson();
62-
Response response = conn.post("webhooks/" + id, json);
63-
return response;
77+
String json = getObjectAsJsonForClass(webhookDescription, WebhookDescription.class);
78+
Response response = conn.put("webhooks/" + id, json);
79+
WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode(
80+
response,
81+
WebhookIdContainerResponse.class
82+
);
83+
return webhookIdContainerResponse;
6484
}
6585

6686
public static Response delete(RestConnection conn, String id) throws SparkPostException {

0 commit comments

Comments
 (0)