Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
39 changes: 13 additions & 26 deletions libs/sparkpost-lib/src/main/java/com/sparkpost/model/Webhook.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;

}
Original file line number Diff line number Diff line change
@@ -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<String> events;

@Description(
value = "Restrict which inbound messages will be relayed to the target",
sample = {"\"match\": { \"protocol\": \"SMTP\", \"domain\": \"replies.customer.example\" }"})
private Match match;

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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 {
Expand Down