Skip to content

Commit 472c179

Browse files
committed
Merge pull request #46 from kalnik-a-a/actualizeWebhooks
Actualize webhooks v2
2 parents 6295747 + 1fd20eb commit 472c179

File tree

9 files changed

+193
-34
lines changed

9 files changed

+193
-34
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sparkpost.model;
2+
3+
import com.google.gson.annotations.SerializedName;
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 AuthCredentials extends Base {
11+
12+
@Description(value = "Basic Auth Username. Used for 'basic' authorization flow.")
13+
private String username;
14+
15+
@Description(value = "Basic Auth Password. Used for 'basic' authorization flow.")
16+
private String password;
17+
18+
@Description(value = "OAuth2 access token generated by SparkPost. Returned in case of 'oauth2' authorization flow.")
19+
@SerializedName("access_token")
20+
private String accessToken;
21+
22+
@Description(value = "The lifetime of the access_token in seconds. Returned in case of 'oauth2' authorization flow.")
23+
@SerializedName("expires_in")
24+
private Integer expiresIn;
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.sparkpost.model;
2+
3+
import com.google.gson.annotations.SerializedName;
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 AuthRequestDetails extends Base {
11+
12+
@Description(value = "This is the URL SparkPost will request tokens from.", sample = {"https://oauth.myurl.com/tokens"})
13+
private String url;
14+
15+
@Description(value = "OAuth 2.0 parameters that your target URL uses")
16+
private AuthRequestClientDetails body;
17+
18+
19+
@Data
20+
@EqualsAndHashCode(callSuper = true)
21+
public static class AuthRequestClientDetails extends Base {
22+
23+
@Description(value = "OAuth 2.0 Client ID")
24+
@SerializedName("client_id")
25+
private String clientId;
26+
27+
@Description(value = "OAuth 2.0 Client Secret")
28+
@SerializedName("client_secret")
29+
private String clientSecret;
30+
}
31+
}

libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ public String toJson(boolean prettyPrint) {
3737

3838
return gson.toJson(this);
3939
}
40+
41+
/**
42+
* Generate JSON from this object for required type.
43+
* @param tClass - target Class.
44+
* @return json of object.
45+
*/
46+
public String toJson(Class tClass) {
47+
return GSON_BUILDER.setPrettyPrinting().create().toJson(this, tClass);
48+
}
4049
}
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
1-
21
package com.sparkpost.model;
32

4-
import java.util.List;
5-
63
import com.google.gson.annotations.SerializedName;
74
import com.yepher.jsondoc.annotations.Description;
8-
95
import lombok.Data;
106
import lombok.EqualsAndHashCode;
117

12-
/**
13-
* DTO for storing info about a webhook.
14-
*
15-
* @author grava
16-
*/
178
@Data
189
@EqualsAndHashCode(callSuper = true)
19-
public class Webhook extends Base {
20-
21-
@Description(value = "User-friendly name", sample = {"Inbound Customer Replies"})
22-
private String name;
10+
public class Webhook extends WebhookDescription {
2311

24-
@Description(value = "URL of the target to which to POST relay batches", sample = {"https://webhooks.customer.example/replies"})
25-
private String target;
12+
@Description(value = "Webhook id")
13+
private String id;
2614

27-
@Description(
28-
value = "Authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target",
29-
sample = {"5ebe2294ecd0e0f08eab7690d2a6ee69"})
30-
@SerializedName("auth_token")
31-
private String authToken;
15+
@Description(value = "Type of authentication to be used during POST requests to target.", sample = {"none", "basic", "oauth2"})
16+
@SerializedName("auth_type")
17+
private String authType;
3218

33-
@Description(value = "Array of events", sample = {""})
34-
private List<String> events;
19+
@Description(value = "When using auth_type == 'oauth2', auth_request_details must be set by the user. "
20+
+ "Additionally, auth_credentials is set by the system and cannot be configured by the user")
21+
@SerializedName("auth_request_details")
22+
private AuthRequestDetails authRequestDetails;
3523

36-
@Description(
37-
value = "Restrict which inbound messages will be relayed to the target",
38-
sample = {"\"match\": { \"protocol\": \"SMTP\", \"domain\": \"replies.customer.example\" }"})
39-
private Match match;
24+
@Description(value = "When using auth_type == 'basic', auth_credentials must be set by the user")
25+
@SerializedName("auth_credentials")
26+
private AuthCredentials authCredentials;
4027

4128
}
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.sparkpost.model;
2+
3+
import com.yepher.jsondoc.annotations.Description;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
7+
@Data
8+
@EqualsAndHashCode(callSuper = true)
9+
public class WebhookIdResponseEntry extends Base {
10+
11+
@Description(value = "Created or updated webhook id.")
12+
private String id;
13+
}
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+
}

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +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;
8+
import com.sparkpost.model.responses.WebhookDescribeResponse;
9+
import com.sparkpost.model.responses.WebhookIdContainerResponse;
710
import com.sparkpost.model.responses.WebhookListAllResponse;
811
import com.sparkpost.transport.RestConnection;
912

@@ -17,6 +20,8 @@
1720
*/
1821
public class ResourceWebhooks {
1922

23+
private static final String DEFAULT_TIMEZONE = "UTC";
24+
2025
public static Response listSampleValuesAndEvents(RestConnection conn) throws SparkPostException {
2126
Response response = conn.get("webhooks/events/documentation");
2227
return response;
@@ -30,6 +35,10 @@ public static Response getSamplePayloadForEvents(RestConnection conn, String eve
3035
return response;
3136
}
3237

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

3544
Endpoint ep = new Endpoint("webhooks");
@@ -39,26 +48,39 @@ public static WebhookListAllResponse listAll(RestConnection conn, String timezon
3948
return allWebhooks;
4049
}
4150

42-
public static Response create(RestConnection conn, Webhook webhook) throws SparkPostException {
51+
public static WebhookIdContainerResponse create(RestConnection conn, Webhook webhook) throws SparkPostException {
4352

4453
String json = webhook.toJson();
4554
Response response = conn.post("webhooks", json);
46-
return response;
55+
WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode(
56+
response,
57+
WebhookIdContainerResponse.class
58+
);
59+
return webhookIdContainerResponse;
4760
}
4861

49-
public static Response describe(RestConnection conn, String id, String timezone) throws SparkPostException {
62+
public static WebhookDescribeResponse describe(RestConnection conn, String id) throws SparkPostException {
63+
return describe(conn, id, DEFAULT_TIMEZONE);
64+
}
65+
66+
public static WebhookDescribeResponse describe(RestConnection conn, String id, String timezone) throws SparkPostException {
5067

5168
Endpoint ep = new Endpoint("webhooks/" + id);
5269
ep.addParam("timezone", timezone);
5370
Response response = conn.get(ep.toString());
54-
return response;
71+
WebhookDescribeResponse webhookDescribeResponse = WebhookDescribeResponse.decode(response, WebhookDescribeResponse.class);
72+
return webhookDescribeResponse;
5573
}
5674

57-
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 {
5876

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

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

0 commit comments

Comments
 (0)