From 2267a1ad6e13b8d5052f8c7a79679d5d15f09694 Mon Sep 17 00:00:00 2001 From: kalnik-a-a Date: Tue, 26 Apr 2016 14:45:43 +0300 Subject: [PATCH 1/5] Webhook class was actualized. WebhookCreateResponse simplifies retrieving of created record id. --- .../com/sparkpost/model/AuthCredentials.java | 25 +++++++++++++++ .../sparkpost/model/AuthRequestDetails.java | 31 +++++++++++++++++++ .../java/com/sparkpost/model/Webhook.java | 15 +++++++++ .../model/WebhookCreateResponseEntry.java | 13 ++++++++ .../responses/WebhookCreateResponse.java | 15 +++++++++ .../sparkpost/resources/ResourceWebhooks.java | 6 ++-- 6 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthCredentials.java create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthRequestDetails.java create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookCreateResponseEntry.java create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java 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..f5d3982 --- /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 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/Webhook.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/Webhook.java index 53e40ee..678e1c5 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,6 +1,7 @@ package com.sparkpost.model; +import java.util.Arrays; import java.util.List; import com.google.gson.annotations.SerializedName; @@ -24,6 +25,20 @@ public class Webhook extends Base { @Description(value = "URL of the target to which to POST relay batches", sample = {"https://webhooks.customer.example/replies"}) private String target; + @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 = "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 = "When using auth_type == 'basic', auth_credentials must be set by the user") + @SerializedName("auth_credentials") + private AuthCredentials authCredentials; + + @Deprecated @Description( value = "Authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target", sample = {"5ebe2294ecd0e0f08eab7690d2a6ee69"}) diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookCreateResponseEntry.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookCreateResponseEntry.java new file mode 100644 index 0000000..f61255d --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookCreateResponseEntry.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 WebhookCreateResponseEntry extends Base { + + @Description(value = "Created webhook id.") + private String id; +} diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java new file mode 100644 index 0000000..510a42c --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java @@ -0,0 +1,15 @@ +package com.sparkpost.model.responses; + +import com.sparkpost.model.WebhookCreateResponseEntry; +import com.yepher.jsondoc.annotations.Description; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class WebhookCreateResponse extends Response { + + + @Description(value = "Created webhook description") + WebhookCreateResponseEntry 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..6028854 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 @@ -4,6 +4,7 @@ import com.sparkpost.exception.SparkPostException; import com.sparkpost.model.Webhook; import com.sparkpost.model.responses.Response; +import com.sparkpost.model.responses.WebhookCreateResponse; import com.sparkpost.model.responses.WebhookListAllResponse; import com.sparkpost.transport.RestConnection; @@ -39,11 +40,12 @@ public static WebhookListAllResponse listAll(RestConnection conn, String timezon return allWebhooks; } - public static Response create(RestConnection conn, Webhook webhook) throws SparkPostException { + public static WebhookCreateResponse create(RestConnection conn, Webhook webhook) throws SparkPostException { String json = webhook.toJson(); Response response = conn.post("webhooks", json); - return response; + WebhookCreateResponse webhookCreateResponse = WebhookCreateResponse.decode(response, WebhookCreateResponse.class); + return webhookCreateResponse; } public static Response describe(RestConnection conn, String id, String timezone) throws SparkPostException { From 15bdacad2438370160e021b9b616c88aeab7d540 Mon Sep 17 00:00:00 2001 From: kalnik-a-a Date: Tue, 26 Apr 2016 15:10:53 +0300 Subject: [PATCH 2/5] static inner class fix --- .../src/main/java/com/sparkpost/model/AuthRequestDetails.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index f5d3982..9adc4cb 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthRequestDetails.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/AuthRequestDetails.java @@ -18,7 +18,7 @@ public class AuthRequestDetails extends Base { @Data @EqualsAndHashCode(callSuper = true) - public class AuthRequestClientDetails extends Base { + public static class AuthRequestClientDetails extends Base { @Description(value = "OAuth 2.0 Client ID") @SerializedName("client_id") From ad1f0f6d53498a39c139eaff6e6f949839d8c17b Mon Sep 17 00:00:00 2001 From: kalnik-a-a Date: Thu, 28 Apr 2016 19:04:27 +0300 Subject: [PATCH 3/5] Webhook id field added --- .../src/main/java/com/sparkpost/model/Webhook.java | 3 +++ 1 file changed, 3 insertions(+) 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 678e1c5..160921f 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 @@ -19,6 +19,9 @@ @EqualsAndHashCode(callSuper = true) public class Webhook extends Base { + @Description(value = "Webhook id") + private String id; + @Description(value = "User-friendly name", sample = {"Inbound Customer Replies"}) private String name; From c607fc988c80ab639c0bfdef04ee4c6bf644f773 Mon Sep 17 00:00:00 2001 From: Aleksandr Kalnik Date: Fri, 29 Apr 2016 01:18:18 +0300 Subject: [PATCH 4/5] Webhooks resourceresponses, WebhookDescription class --- .../java/com/sparkpost/model/Webhook.java | 33 +------------- .../sparkpost/model/WebhookDescription.java | 43 +++++++++++++++++++ ...Entry.java => WebhookIdResponseEntry.java} | 4 +- .../responses/WebhookCreateResponse.java | 15 ------- .../responses/WebhookDescribeResponse.java | 14 ++++++ .../responses/WebhookIdContainerResponse.java | 15 +++++++ .../com/sparkpost/resources/ResourceBase.java | 15 +++++++ .../sparkpost/resources/ResourceWebhooks.java | 42 +++++++++++++----- 8 files changed, 121 insertions(+), 60 deletions(-) create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookDescription.java rename libs/sparkpost-lib/src/main/java/com/sparkpost/model/{WebhookCreateResponseEntry.java => WebhookIdResponseEntry.java} (64%) delete mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookDescribeResponse.java create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookIdContainerResponse.java create mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceBase.java 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 160921f..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,33 +1,17 @@ - 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 Webhook extends Base { +public class Webhook extends WebhookDescription { @Description(value = "Webhook id") private String id; - @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; - @Description(value = "Type of authentication to be used during POST requests to target.", sample = {"none", "basic", "oauth2"}) @SerializedName("auth_type") private String authType; @@ -41,19 +25,4 @@ public class Webhook extends Base { @SerializedName("auth_credentials") private AuthCredentials authCredentials; - @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/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/WebhookCreateResponseEntry.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookIdResponseEntry.java similarity index 64% rename from libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookCreateResponseEntry.java rename to libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookIdResponseEntry.java index f61255d..33bf705 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookCreateResponseEntry.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/WebhookIdResponseEntry.java @@ -6,8 +6,8 @@ @Data @EqualsAndHashCode(callSuper = true) -public class WebhookCreateResponseEntry extends Base { +public class WebhookIdResponseEntry extends Base { - @Description(value = "Created webhook id.") + @Description(value = "Created or updated webhook id.") private String id; } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java deleted file mode 100644 index 510a42c..0000000 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/WebhookCreateResponse.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.sparkpost.model.responses; - -import com.sparkpost.model.WebhookCreateResponseEntry; -import com.yepher.jsondoc.annotations.Description; -import lombok.Data; -import lombok.EqualsAndHashCode; - -@Data -@EqualsAndHashCode(callSuper = true) -public class WebhookCreateResponse extends Response { - - - @Description(value = "Created webhook description") - WebhookCreateResponseEntry results; -} 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/ResourceBase.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceBase.java new file mode 100644 index 0000000..a2e1891 --- /dev/null +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceBase.java @@ -0,0 +1,15 @@ +package com.sparkpost.resources; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class ResourceBase { + + private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; + private static final Gson GSON = new GsonBuilder().setDateFormat(DATE_TIME_FORMAT).create(); + + protected static String getObjectAsJsonForClass(final Object object, final Class tClass) { + return GSON.toJson(object, tClass); + } + +} 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 6028854..69bf7e8 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,8 +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.WebhookCreateResponse; +import com.sparkpost.model.responses.WebhookDescribeResponse; +import com.sparkpost.model.responses.WebhookIdContainerResponse; import com.sparkpost.model.responses.WebhookListAllResponse; import com.sparkpost.transport.RestConnection; @@ -16,7 +18,9 @@ * * @author grava */ -public class ResourceWebhooks { +public class ResourceWebhooks extends ResourceBase { + + private static final String DEFAULT_TIMEZONE = "UTC"; public static Response listSampleValuesAndEvents(RestConnection conn) throws SparkPostException { Response response = conn.get("webhooks/events/documentation"); @@ -31,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"); @@ -40,27 +48,39 @@ public static WebhookListAllResponse listAll(RestConnection conn, String timezon return allWebhooks; } - public static WebhookCreateResponse 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); - WebhookCreateResponse webhookCreateResponse = WebhookCreateResponse.decode(response, WebhookCreateResponse.class); - return webhookCreateResponse; + WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode( + response, + WebhookIdContainerResponse.class + ); + return webhookIdContainerResponse; + } + + public static WebhookDescribeResponse describe(RestConnection conn, String id) throws SparkPostException { + return describe(conn, id, DEFAULT_TIMEZONE); } - public static Response describe(RestConnection conn, String id, String timezone) throws SparkPostException { + 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 = getObjectAsJsonForClass(webhookDescription, 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 { From 1fd20eba36af3225c6321885b70e6fa24a0c1234 Mon Sep 17 00:00:00 2001 From: kalnik-a-a Date: Fri, 29 Apr 2016 11:00:07 +0300 Subject: [PATCH 5/5] ResourceBase removed. One more toJson() method added to Base --- .../src/main/java/com/sparkpost/model/Base.java | 9 +++++++++ .../com/sparkpost/resources/ResourceBase.java | 15 --------------- .../com/sparkpost/resources/ResourceWebhooks.java | 4 ++-- 3 files changed, 11 insertions(+), 17 deletions(-) delete mode 100644 libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceBase.java 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/resources/ResourceBase.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceBase.java deleted file mode 100644 index a2e1891..0000000 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceBase.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.sparkpost.resources; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -public class ResourceBase { - - private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; - private static final Gson GSON = new GsonBuilder().setDateFormat(DATE_TIME_FORMAT).create(); - - protected static String getObjectAsJsonForClass(final Object object, final Class tClass) { - return GSON.toJson(object, tClass); - } - -} 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 69bf7e8..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 @@ -18,7 +18,7 @@ * * @author grava */ -public class ResourceWebhooks extends ResourceBase { +public class ResourceWebhooks { private static final String DEFAULT_TIMEZONE = "UTC"; @@ -74,7 +74,7 @@ public static WebhookDescribeResponse describe(RestConnection conn, String id, S public static WebhookIdContainerResponse update(RestConnection conn, String id, WebhookDescription webhookDescription) throws SparkPostException { - String json = getObjectAsJsonForClass(webhookDescription, WebhookDescription.class); + String json = webhookDescription.toJson(WebhookDescription.class); Response response = conn.put("webhooks/" + id, json); WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode( response,