diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthCredentials.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthCredentials.java new file mode 100644 index 0000000..4c6e8ba --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthCredentials.java @@ -0,0 +1,25 @@ +package com.sparkpost.model; + +import com.google.gson.annotations.SerializedName; +import com.yepher.jsondoc.annotations.Description; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class AuthCredentials extends Base { + + @Description(value = "Basic Auth Username. Used for 'basic' authorization flow.") + private String username; + + @Description(value = "Basic Auth Password. Used for 'basic' authorization flow.") + private String password; + + @Description(value = "OAuth2 access token generated by SparkPost. Returned in case of 'oauth2' authorization flow.") + @SerializedName("access_token") + private String accessToken; + + @Description(value = "The lifetime of the access_token in seconds. Returned in case of 'oauth2' authorization flow.") + @SerializedName("expires_in") + private Integer expiresIn; +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthRequestDetails.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthRequestDetails.java new file mode 100644 index 0000000..9adc4cb --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthRequestDetails.java @@ -0,0 +1,31 @@ +package com.sparkpost.model; + +import com.google.gson.annotations.SerializedName; +import com.yepher.jsondoc.annotations.Description; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class AuthRequestDetails extends Base { + + @Description(value = "This is the URL SparkPost will request tokens from.", sample = {"https://oauth.myurl.com/tokens"}) + private String url; + + @Description(value = "OAuth 2.0 parameters that your target URL uses") + private AuthRequestClientDetails body; + + + @Data + @EqualsAndHashCode(callSuper = true) + public static class AuthRequestClientDetails extends Base { + + @Description(value = "OAuth 2.0 Client ID") + @SerializedName("client_id") + private String clientId; + + @Description(value = "OAuth 2.0 Client Secret") + @SerializedName("client_secret") + private String clientSecret; + } +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java index ec2dabb..8e0477a 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java @@ -37,4 +37,13 @@ public String toJson(boolean prettyPrint) { return gson.toJson(this); } + + /** + * Generate JSON from this object for required type. + * @param tClass - target Class. + * @return json of object. + */ + public String toJson(Class tClass) { + return GSON_BUILDER.setPrettyPrinting().create().toJson(this, tClass); + } } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Webhook.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Webhook.java index 53e40ee..eab3335 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Webhook.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Webhook.java @@ -1,41 +1,28 @@ - package com.sparkpost.model; -import java.util.List; - import com.google.gson.annotations.SerializedName; import com.yepher.jsondoc.annotations.Description; - import lombok.Data; import lombok.EqualsAndHashCode; -/** - * DTO for storing info about a webhook. - * - * @author grava - */ @Data @EqualsAndHashCode(callSuper = true) -public class Webhook extends Base { - - @Description(value = "User-friendly name", sample = {"Inbound Customer Replies"}) - private String name; +public class Webhook extends WebhookDescription { - @Description(value = "URL of the target to which to POST relay batches", sample = {"https://webhooks.customer.example/replies"}) - private String target; + @Description(value = "Webhook id") + private String id; - @Description( - value = "Authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target", - sample = {"5ebe2294ecd0e0f08eab7690d2a6ee69"}) - @SerializedName("auth_token") - private String authToken; + @Description(value = "Type of authentication to be used during POST requests to target.", sample = {"none", "basic", "oauth2"}) + @SerializedName("auth_type") + private String authType; - @Description(value = "Array of events", sample = {""}) - private List events; + @Description(value = "When using auth_type == 'oauth2', auth_request_details must be set by the user. " + + "Additionally, auth_credentials is set by the system and cannot be configured by the user") + @SerializedName("auth_request_details") + private AuthRequestDetails authRequestDetails; - @Description( - value = "Restrict which inbound messages will be relayed to the target", - sample = {"\"match\": { \"protocol\": \"SMTP\", \"domain\": \"replies.customer.example\" }"}) - private Match match; + @Description(value = "When using auth_type == 'basic', auth_credentials must be set by the user") + @SerializedName("auth_credentials") + private AuthCredentials authCredentials; } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookDescription.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookDescription.java new file mode 100644 index 0000000..85143f5 --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookDescription.java @@ -0,0 +1,43 @@ + +package com.sparkpost.model; + +import java.util.Arrays; +import java.util.List; + +import com.google.gson.annotations.SerializedName; +import com.yepher.jsondoc.annotations.Description; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * DTO for storing info about a webhook. + * + * @author grava + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class WebhookDescription extends Base { + + @Description(value = "User-friendly name", sample = {"Inbound Customer Replies"}) + private String name; + + @Description(value = "URL of the target to which to POST relay batches", sample = {"https://webhooks.customer.example/replies"}) + private String target; + + @Deprecated + @Description( + value = "Authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target", + sample = {"5ebe2294ecd0e0f08eab7690d2a6ee69"}) + @SerializedName("auth_token") + private String authToken; + + @Description(value = "Array of events", sample = {""}) + private List events; + + @Description( + value = "Restrict which inbound messages will be relayed to the target", + sample = {"\"match\": { \"protocol\": \"SMTP\", \"domain\": \"replies.customer.example\" }"}) + private Match match; + +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookIdResponseEntry.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookIdResponseEntry.java new file mode 100644 index 0000000..33bf705 --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookIdResponseEntry.java @@ -0,0 +1,13 @@ +package com.sparkpost.model; + +import com.yepher.jsondoc.annotations.Description; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class WebhookIdResponseEntry extends Base { + + @Description(value = "Created or updated webhook id.") + private String id; +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookDescribeResponse.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookDescribeResponse.java new file mode 100644 index 0000000..5ffe34e --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookDescribeResponse.java @@ -0,0 +1,14 @@ +package com.sparkpost.model.responses; + +import com.sparkpost.model.Webhook; +import com.yepher.jsondoc.annotations.Description; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class WebhookDescribeResponse extends Response { + + @Description(value = "", sample = "") + private Webhook results; +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookIdContainerResponse.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookIdContainerResponse.java new file mode 100644 index 0000000..927af4b --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookIdContainerResponse.java @@ -0,0 +1,15 @@ +package com.sparkpost.model.responses; + +import com.sparkpost.model.WebhookIdResponseEntry; +import com.yepher.jsondoc.annotations.Description; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class WebhookIdContainerResponse extends Response { + + + @Description(value = "Created or updated webhook id description") + WebhookIdResponseEntry results; +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceWebhooks.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceWebhooks.java index 796a169..e07324a 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceWebhooks.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceWebhooks.java @@ -3,7 +3,10 @@ import com.sparkpost.exception.SparkPostException; import com.sparkpost.model.Webhook; +import com.sparkpost.model.WebhookDescription; import com.sparkpost.model.responses.Response; +import com.sparkpost.model.responses.WebhookDescribeResponse; +import com.sparkpost.model.responses.WebhookIdContainerResponse; import com.sparkpost.model.responses.WebhookListAllResponse; import com.sparkpost.transport.RestConnection; @@ -17,6 +20,8 @@ */ public class ResourceWebhooks { + private static final String DEFAULT_TIMEZONE = "UTC"; + public static Response listSampleValuesAndEvents(RestConnection conn) throws SparkPostException { Response response = conn.get("webhooks/events/documentation"); return response; @@ -30,6 +35,10 @@ public static Response getSamplePayloadForEvents(RestConnection conn, String eve return response; } + public static WebhookListAllResponse listAll(RestConnection conn) throws SparkPostException { + return listAll(conn, DEFAULT_TIMEZONE); + } + public static WebhookListAllResponse listAll(RestConnection conn, String timezone) throws SparkPostException { Endpoint ep = new Endpoint("webhooks"); @@ -39,26 +48,39 @@ public static WebhookListAllResponse listAll(RestConnection conn, String timezon return allWebhooks; } - public static Response create(RestConnection conn, Webhook webhook) throws SparkPostException { + public static WebhookIdContainerResponse create(RestConnection conn, Webhook webhook) throws SparkPostException { String json = webhook.toJson(); Response response = conn.post("webhooks", json); - return response; + WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode( + response, + WebhookIdContainerResponse.class + ); + return webhookIdContainerResponse; } - public static Response describe(RestConnection conn, String id, String timezone) throws SparkPostException { + public static WebhookDescribeResponse describe(RestConnection conn, String id) throws SparkPostException { + return describe(conn, id, DEFAULT_TIMEZONE); + } + + public static WebhookDescribeResponse describe(RestConnection conn, String id, String timezone) throws SparkPostException { Endpoint ep = new Endpoint("webhooks/" + id); ep.addParam("timezone", timezone); Response response = conn.get(ep.toString()); - return response; + WebhookDescribeResponse webhookDescribeResponse = WebhookDescribeResponse.decode(response, WebhookDescribeResponse.class); + return webhookDescribeResponse; } - public static Response update(RestConnection conn, String id, Webhook webhook) throws SparkPostException { + public static WebhookIdContainerResponse update(RestConnection conn, String id, WebhookDescription webhookDescription) throws SparkPostException { - String json = webhook.toJson(); - Response response = conn.post("webhooks/" + id, json); - return response; + String json = webhookDescription.toJson(WebhookDescription.class); + Response response = conn.put("webhooks/" + id, json); + WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode( + response, + WebhookIdContainerResponse.class + ); + return webhookIdContainerResponse; } public static Response delete(RestConnection conn, String id) throws SparkPostException {