From 8bf9501189d627f2c3da91931f05096598c6bc59 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 3 Mar 2017 11:25:18 -0600 Subject: [PATCH 1/8] Fixing issue #57 extra slash in URL with HTTP Client >=4.5.3 --- README.md | 4 +++ libs/sparkpost-lib/pom.xml | 2 +- .../com/sparkpost/resources/Endpoint.java | 22 ++++++++++++--- .../sparkpost/transport/RestConnection.java | 27 ++++++++++++------- .../test/java/com/sparkpost/EndPointTest.java | 8 +++--- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 742b43c..ebad35a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ Use this library in Java applications to easily access the SparkPost Email API i ## Version Compatibility Note +### Version 0.6.2 -> 0.6.3 + +Due to [issue 57](https://github.com/SparkPost/java-sparkpost/issues/57) and to maintain compatibility with old and new version of Apache HTTP Client `SPARKPOST_BASE_URL` must not end with a `/` slash. + ### Version 0.12 -> 0.13 Although we try to maintain library backward compatibility this migration may require some minor changes to your code. Substitution data was changed from `Map` to `Map`. Most client code will just need to change their map to this new signature. diff --git a/libs/sparkpost-lib/pom.xml b/libs/sparkpost-lib/pom.xml index f7082f2..863e87f 100644 --- a/libs/sparkpost-lib/pom.xml +++ b/libs/sparkpost-lib/pom.xml @@ -25,7 +25,7 @@ org.apache.httpcomponents httpclient - 4.5 + 4.5.3 sparkpost-lib diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java index 83b9881..b64085b 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java @@ -42,7 +42,7 @@ public class RestConnection implements IRestConnection { private final Client client; - private final String endpoint; + private final String baseUrl; /** * Supported HTTP methods @@ -73,22 +73,22 @@ public RestConnection(Client client) throws SparkPostException { * * @param client * Client object to use (in particular for authentication info) - * @param endpoint + * @param baseUrl * Endpoint to use instead of the default defaultApiEndpoint * @throws SparkPostException */ - public RestConnection(Client client, String endpoint) throws SparkPostException { + public RestConnection(Client client, String baseUrl) throws SparkPostException { this.client = client; - if (StringUtils.isAnyEmpty(endpoint)) { - this.endpoint = defaultApiEndpoint; + if (StringUtils.isAnyEmpty(baseUrl)) { + this.baseUrl = defaultApiEndpoint; } else { - this.endpoint = endpoint; + this.baseUrl = baseUrl; } - if (endpoint.endsWith("/")) { - throw new IllegalStateException("SPARKPOST_BASE_URL should not end with a '/', SPARKPOST_BASE_URL=" + endpoint + ""); + if (baseUrl.endsWith("/")) { + throw new IllegalStateException("SPARKPOST_BASE_URL should not end with a '/', SPARKPOST_BASE_URL=" + baseUrl + ""); } } @@ -106,8 +106,8 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr HttpURLConnection conn; try { URL url; - url = new URL(this.endpoint + path); - + url = new URL(this.baseUrl + path); + // Retrieve the URLConnection object (but doesn't actually connect): // (HttpUrlConnection doesn't connect to the server until we've // got one of its streams) @@ -311,7 +311,7 @@ private Response receiveSuccessResponse(HttpURLConnection conn, Response respons return response; } - // This is used to handle 2xx HTTP responses + // This is used to handle non 2xx HTTP responses private Response receiveErrorResponse(HttpURLConnection conn, Response response) throws SparkPostException, IOException { response.setRequestId(conn.getHeaderField("X-SparkPost-Request-Id")); @@ -330,7 +330,6 @@ private Response receiveErrorResponse(HttpURLConnection conn, Response response) ServerErrorResponses errorResponses = null; try { - // We are in the success case handling but check the error stream anyway just in case try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream(), DEFAULT_CHARSET))) { String line = ""; @@ -345,8 +344,8 @@ private Response receiveErrorResponse(HttpURLConnection conn, Response response) try { errorResponses = (ServerErrorResponses) ServerErrorResponses.decode(response, ServerErrorResponses.class); } catch (Exception e) { - // Maybe there is something wrong where HTML is returned or some other very bad error. So we protect - // ourselves from that exception so we can throw a more specific exception later. + // Maybe there is something wrong where HTML is returned or some other very bad error. Protect + // against exception here so a more specific exception can be thrown later later. logger.error("Failed to parse server response:\n", e); } @@ -355,7 +354,7 @@ private Response receiveErrorResponse(HttpURLConnection conn, Response response) } } catch (Exception e) { // Log but ignore since an exception is getting thrown anyway - logger.error("Error while handlign an HTTP response error. Ignoring and will use orginal exception", e); + logger.error("Error while handling an HTTP response error. Ignoring and will use orginal exception", e); } if (logger.isDebugEnabled()) { @@ -392,7 +391,7 @@ private Response receiveErrorResponse(HttpURLConnection conn, Response response) } } - // This method actually performs an HTTP request. + // This method actually performs the HTTP request // It is called by get(), put(), post() and delete() below private Response doHttpMethod(String path, Method method, String data, Response response) throws SparkPostException { HttpURLConnection conn = null; @@ -414,7 +413,8 @@ private Response doHttpMethod(String path, Method method, String data, Response } } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#get(java.lang.String) */ @Override @@ -423,7 +423,8 @@ public Response get(String path) throws SparkPostException { return doHttpMethod(path, Method.GET, null, response); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#post(java.lang.String, java.lang.String) */ @Override @@ -432,7 +433,8 @@ public Response post(String path, String json) throws SparkPostException { return doHttpMethod(path, Method.POST, json, response); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#put(java.lang.String, java.lang.String) */ @Override @@ -441,7 +443,8 @@ public Response put(String path, String json) throws SparkPostException { return doHttpMethod(path, Method.PUT, json, response); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#delete(java.lang.String) */ @Override diff --git a/pom.xml b/pom.xml index e06ebe3..4c84cf8 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.sparkpost sparkpost - 0.16.2 + 0.17 pom *** SparkPost Java Code *** SparkPost client library for Java https://www.sparkpost.com/ @@ -258,7 +258,7 @@ com.sparkpost sparkpost-lib - 0.16.2 + 0.17 From 26feacfbb2e54f3dbca32bcb23850d922683250d Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 7 Mar 2017 07:32:41 -0500 Subject: [PATCH 7/8] Adds explicit tests for EndPoint adding a single leading `/` to the beginning of a URI. --- .../test/java/com/sparkpost/EndPointTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/EndPointTest.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/EndPointTest.java index 5eecbfb..e1c14e5 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/EndPointTest.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/EndPointTest.java @@ -34,6 +34,39 @@ public void setUp() { public void tearDown() { } + /** + * Test of "/" is added to empty Endoint + */ + @Test + public void testSimpleEmptyEndPoint() { + Endpoint endPoint = new Endpoint(""); + + String result = endPoint.toString(); + Assert.assertEquals("/", result); + } + + /** + * Test if "/" URI starts with only one `/` + */ + @Test + public void testSimpleEmptyEndPoint2() { + Endpoint endPoint = new Endpoint("/"); + + String result = endPoint.toString(); + Assert.assertEquals("/", result); + } + + /** + * Test if "/" URI starts with only one `/` + */ + @Test + public void testSimpleEndPointLeadingSlash() { + Endpoint endPoint = new Endpoint("/test"); + + String result = endPoint.toString(); + Assert.assertEquals("/test", result); + } + /** * Test of Simple EndPoint */ From a18353053213a8419f24f02c43c09d923df3c597 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 7 Mar 2017 22:28:23 -0500 Subject: [PATCH 8/8] Changes RestConnection to use Endpoint instead of a path string --- .../com/sparkpost/resources/Endpoint.java | 2 - .../sparkpost/resources/ResourceMetrics.java | 36 +++++----- .../resources/ResourceRecipientLists.java | 8 +-- .../resources/ResourceSendingDomains.java | 10 +-- .../resources/ResourceSuppressionList.java | 10 +-- .../resources/ResourceTemplates.java | 12 ++-- .../resources/ResourceTransmissions.java | 6 +- .../sparkpost/resources/ResourceWebhooks.java | 14 ++-- .../sparkpost/transport/IRestConnection.java | 60 ++++++++++++++++ .../sparkpost/transport/RestConnection.java | 69 +++++++++++++++++++ .../resources/ResourceMetricTests.java | 36 +++++----- .../ResourceRecipientListsTests.java | 8 +-- .../ResourceSendingDomainsTests.java | 8 +-- .../ResourceSuppressionListTests.java | 10 +-- .../resources/ResourceTemplatesTests.java | 14 ++-- .../resources/ResourceTransmissionsTests.java | 8 +-- .../testhelpers/StubRestConnection.java | 48 +++++++++++++ 17 files changed, 267 insertions(+), 92 deletions(-) diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/Endpoint.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/Endpoint.java index ae81a61..5192fab 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/Endpoint.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/Endpoint.java @@ -5,8 +5,6 @@ /** * Used internally to the SparkPost Library to write URL queries. - * - * @author grava */ public class Endpoint { diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceMetrics.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceMetrics.java index a5db690..6a39362 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceMetrics.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceMetrics.java @@ -20,7 +20,7 @@ public class ResourceMetrics { public static Response getDiscoverabilityLinks(IRestConnection conn) throws SparkPostException { Endpoint ep = new Endpoint("metrics/"); - return conn.get(ep.toString()); + return conn.get(ep); } public static DeliverabiltyMetricsResponse getDeliverabilityMetricsSummary( @@ -36,7 +36,7 @@ public static DeliverabiltyMetricsResponse getDeliverabilityMetricsSummary( Endpoint ep = new Endpoint("metrics/deliverability"); ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -57,7 +57,7 @@ public static DeliverabiltyMetricsResponse getDeliverabilityMetricsByDomain( Endpoint ep = new Endpoint("metrics/deliverability/domain"); ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -79,7 +79,7 @@ public static DeliverabiltyMetricsResponse getDeliverabilityMetricsByCampaign( Endpoint ep = new Endpoint("metrics/deliverability/campaign"); ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -101,7 +101,7 @@ public static DeliverabiltyMetricsResponse getDeliverabilityMetricsByTemplate( Endpoint ep = new Endpoint("metrics/deliverability/template"); ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -122,7 +122,7 @@ public static DeliverabiltyMetricsResponse getDeliverabilityMetricsByWatchedDoma Endpoint ep = new Endpoint("metrics/deliverability/watched-domain"); ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -144,7 +144,7 @@ public static DeliverabiltyMetricsResponse getTimeSeriesMetrics( ep.addParam("precision", precision); ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -165,7 +165,7 @@ public static DeliverabiltyMetricsResponse getBounceReasonMetrics( ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -186,7 +186,7 @@ public static DeliverabiltyMetricsResponse getBounceReasonMetricsByDomain( ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -207,7 +207,7 @@ public static DeliverabiltyMetricsResponse getBounceClassificationMetrics( ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -227,7 +227,7 @@ public static DeliverabiltyMetricsResponse getRejectionReasonMetrics( ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -247,7 +247,7 @@ public static DeliverabiltyMetricsResponse getRejectionReasonMetricsByDomain( ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -267,7 +267,7 @@ public static DeliverabiltyMetricsResponse getDelayReasonMetrics( ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -287,7 +287,7 @@ public static DeliverabiltyMetricsResponse getDelayReasonMetricsByDomain( ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -307,7 +307,7 @@ public static DeliverabiltyMetricsResponse getEngagementDetails( ep.addCommonParams(from, to, null, campaigns, templates, metrics, timezone, limit, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -325,7 +325,7 @@ public static DeliverabiltyMetricsResponse getDeliveriesByAttempt( Endpoint ep = new Endpoint("metrics/deliverability/attempt"); ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, null, null); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DeliverabiltyMetricsResponse newResponse = DeliverabiltyMetricsResponse.decode(response, DeliverabiltyMetricsResponse.class); return newResponse; @@ -337,7 +337,7 @@ public static CampaignListResponse getCampaignsList(IRestConnection conn, String ep.addParam("match", match); ep.addParam("limit", limit); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); CampaignListResponse newResponse = CampaignListResponse.decode(response, CampaignListResponse.class); return newResponse; @@ -349,7 +349,7 @@ public static DomainListResponse getDomainsList(IRestConnection conn, String mat ep.addParam("match", match); ep.addParam("limit", limit); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); DomainListResponse newResponse = DomainListResponse.decode(response, DomainListResponse.class); return newResponse; diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceRecipientLists.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceRecipientLists.java index 786873f..e182838 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceRecipientLists.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceRecipientLists.java @@ -24,14 +24,14 @@ public static Response create(IRestConnection conn, Integer maxNumberOfRecipient String json = recipientList.toJson(); Endpoint ep = new Endpoint("recipient-lists"); ep.addParam("num_rcpt_errors", maxNumberOfRecipientErrors); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); return response; } public static RecipientListRetrieveResponse retrieve(IRestConnection conn, String recipientListId, Boolean showRecipients) throws SparkPostException { Endpoint ep = new Endpoint("recipient-lists/" + recipientListId); ep.addParam("show_recipients", showRecipients); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); RecipientListRetrieveResponse retrieveResponse = RecipientListRetrieveResponse.decode(response, RecipientListRetrieveResponse.class); return retrieveResponse; @@ -39,14 +39,14 @@ public static RecipientListRetrieveResponse retrieve(IRestConnection conn, Strin public static RecipientListsListAllResponse listAll(IRestConnection conn) throws SparkPostException { Endpoint ep = new Endpoint("recipient-lists"); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); RecipientListsListAllResponse listResponse = RecipientListsListAllResponse.decode(response, RecipientListsListAllResponse.class); return listResponse; } public static Response delete(IRestConnection conn, String recipientListId) throws SparkPostException { Endpoint ep = new Endpoint("recipient-lists/" + recipientListId); - Response response = conn.delete(ep.toString()); + Response response = conn.delete(ep); return response; } } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSendingDomains.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSendingDomains.java index 6d06c39..cf81069 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSendingDomains.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSendingDomains.java @@ -23,20 +23,20 @@ public static Response create(IRestConnection conn, SendingDomain domain) throws String json = domain.toJson(); Endpoint ep = new Endpoint("sending-domains"); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); return response; } public static Response retrieve(IRestConnection conn, String domainName) throws SparkPostException { Endpoint ep = new Endpoint("sending-domains/" + domainName); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); return response; } public static Response list(IRestConnection conn) throws SparkPostException { Endpoint ep = new Endpoint("sending-domains/"); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); return response; } @@ -44,7 +44,7 @@ public static Response update(IRestConnection conn, String domainName, SendingDo String json = domain.toJson(); Endpoint ep = new Endpoint("sending-domains/" + domainName); - Response response = conn.put(ep.toString(), json); + Response response = conn.put(ep, json); return response; } @@ -52,7 +52,7 @@ public static Response verify(IRestConnection conn, String domainName, VerifyAtt String json = verify.toJson(); Endpoint ep = new Endpoint("sending-domains/" + domainName + "/verify"); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); return response; } } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSuppressionList.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSuppressionList.java index a587779..6c7476d 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSuppressionList.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceSuppressionList.java @@ -26,7 +26,7 @@ public static Response insertOrUpdate(IRestConnection conn, String email, final String json = copy.toJson(); Endpoint ep = new Endpoint("suppression-list/" + email); - Response response = conn.put(ep.toString(), json); + Response response = conn.put(ep, json); return response; } @@ -34,7 +34,7 @@ public static Response insertOrUpdateBulk(IRestConnection conn, SuppressionList String json = suppressionList.toJson(); Endpoint ep = new Endpoint("suppression-list/"); - return conn.put(ep.toString(), json); + return conn.put(ep, json); } public static Response search(IRestConnection conn, String to, String from, String types, String limit) throws SparkPostException { @@ -44,20 +44,20 @@ public static Response search(IRestConnection conn, String to, String from, Stri ep.addParam("from", from); ep.addParam("types", types); ep.addParam("limit", limit); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); return response; } public static Response check(IRestConnection conn, String email) throws SparkPostException { Endpoint ep = new Endpoint("suppression-list/" + email); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); return response; } public static Response remove(IRestConnection conn, String email) throws SparkPostException { Endpoint ep = new Endpoint("suppression-list/" + email); - Response response = conn.delete(ep.toString()); + Response response = conn.delete(ep); return response; } } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTemplates.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTemplates.java index 6ecbb4f..6daa7b3 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTemplates.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTemplates.java @@ -25,7 +25,7 @@ public static TemplateCreateResponse create(IRestConnection conn, TemplateAttrib String json = tpl.toJson(); Endpoint ep = new Endpoint("templates"); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); TemplateCreateResponse createResponse = TemplateCreateResponse.decode(response, TemplateCreateResponse.class); return createResponse; } @@ -34,7 +34,7 @@ public static TemplateRetrieveResponse retrieve(IRestConnection conn, String id, Endpoint ep = new Endpoint("templates/" + id); ep.addParam("draft", draft); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); TemplateRetrieveResponse templateResponse = (TemplateRetrieveResponse) TemplateItemResponse.decode(response, TemplateRetrieveResponse.class); return templateResponse; @@ -42,7 +42,7 @@ public static TemplateRetrieveResponse retrieve(IRestConnection conn, String id, public static TemplateListResponse listAll(IRestConnection conn) throws SparkPostException { Endpoint ep = new Endpoint("templates/"); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); TemplateListResponse listResponse = (TemplateListResponse) TemplateListResponse.decode(response, TemplateListResponse.class); return listResponse; } @@ -52,7 +52,7 @@ public static Response update(IRestConnection conn, String id, Boolean updatePub Endpoint ep = new Endpoint("templates/" + id); ep.addParam("update_published", updatePublished); String json = tpl.toJson(); - Response response = conn.put(ep.toString(), json); + Response response = conn.put(ep, json); return response; } @@ -61,14 +61,14 @@ public static TemplatePreviewResponse preview(IRestConnection conn, String id, B Endpoint ep = new Endpoint("templates/" + id + "/preview"); ep.addParam("draft", draft); String json = subst.toJson(); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); TemplatePreviewResponse newResponse = TemplatePreviewResponse.decode(response, TemplatePreviewResponse.class); return newResponse; } public static Response delete(IRestConnection conn, String id) throws SparkPostException { Endpoint ep = new Endpoint("templates/" + id); - Response response = conn.delete(ep.toString()); + Response response = conn.delete(ep); // Delete response is an empty dictionary so no need to deserialize the JSON object. return response; } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTransmissions.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTransmissions.java index fa6b578..967c7ab 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTransmissions.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceTransmissions.java @@ -40,7 +40,7 @@ private static TransmissionCreateResponse createTra Endpoint ep = new Endpoint("transmissions"); ep.addParam("num_rcpt_errors", numRcptErrors); String json = trans.toJson(); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); TransmissionCreateResponse newResult = TransmissionCreateResponse.decode(response, TransmissionCreateResponse.class); return newResult; @@ -48,7 +48,7 @@ private static TransmissionCreateResponse createTra public static TransmissionRetrieveResults retrieve(IRestConnection conn, String id) throws SparkPostException { Endpoint ep = new Endpoint("transmissions/" + id); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); TransmissionRetrieveResults newResult = TransmissionRetrieveResults.decode(response, TransmissionRetrieveResults.class); return newResult; @@ -59,7 +59,7 @@ public static TransmissionListResponse list(IRestConnection conn, String campaig Endpoint ep = new Endpoint("transmissions"); ep.addParam("campaign_id", campaignId); ep.addParam("template_id", templateId); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); TransmissionListResponse transmissionResponse = TransmissionListResponse.decode(response, TransmissionListResponse.class); return transmissionResponse; 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 df0bbb3..44bb729 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 @@ -24,7 +24,7 @@ public class ResourceWebhooks { public static Response listSampleValuesAndEvents(IRestConnection conn) throws SparkPostException { Endpoint ep = new Endpoint("webhooks/events/documentation"); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); return response; } @@ -32,7 +32,7 @@ public static Response getSamplePayloadForEvents(IRestConnection conn, String ev Endpoint ep = new Endpoint("webhooks/events/samples"); ep.addParam("events", events); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); return response; } @@ -44,7 +44,7 @@ public static WebhookListAllResponse listAll(IRestConnection conn, String timezo Endpoint ep = new Endpoint("webhooks"); ep.addParam("timezone", timezone); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); WebhookListAllResponse allWebhooks = WebhookListAllResponse.decode(response, WebhookListAllResponse.class); return allWebhooks; } @@ -53,7 +53,7 @@ public static WebhookIdContainerResponse create(IRestConnection conn, Webhook we String json = webhook.toJson(); Endpoint ep = new Endpoint("webhooks"); - Response response = conn.post(ep.toString(), json); + Response response = conn.post(ep, json); WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode(response, WebhookIdContainerResponse.class); return webhookIdContainerResponse; } @@ -66,7 +66,7 @@ public static WebhookDescribeResponse describe(IRestConnection conn, String id, Endpoint ep = new Endpoint("webhooks/" + id); ep.addParam("timezone", timezone); - Response response = conn.get(ep.toString()); + Response response = conn.get(ep); WebhookDescribeResponse webhookDescribeResponse = WebhookDescribeResponse.decode(response, WebhookDescribeResponse.class); return webhookDescribeResponse; } @@ -75,7 +75,7 @@ public static WebhookIdContainerResponse update(IRestConnection conn, String id, String json = webhookDescription.toJson(WebhookDescription.class); Endpoint ep = new Endpoint("webhooks/" + id); - Response response = conn.put(ep.toString(), json); + Response response = conn.put(ep, json); WebhookIdContainerResponse webhookIdContainerResponse = WebhookIdContainerResponse.decode(response, WebhookIdContainerResponse.class); return webhookIdContainerResponse; } @@ -83,7 +83,7 @@ public static WebhookIdContainerResponse update(IRestConnection conn, String id, public static Response delete(IRestConnection conn, String id) throws SparkPostException { Endpoint ep = new Endpoint("webhooks/" + id); - Response response = conn.delete(ep.toString()); + Response response = conn.delete(ep); return response; } } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/IRestConnection.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/IRestConnection.java index 8d2bf9e..5af2060 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/IRestConnection.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/IRestConnection.java @@ -3,6 +3,7 @@ import com.sparkpost.exception.SparkPostException; import com.sparkpost.model.responses.Response; +import com.sparkpost.resources.Endpoint; public interface IRestConnection { @@ -16,14 +17,28 @@ public interface IRestConnection { * Perform an HTTP GET request. This method throws an exception if the * server returns anything else than a 200. * + * @deprecated use method that uses Endpoint as an argument instead * @param path * API endpoint to send the request to. * @return Server response to the request. * @throws SparkPostException * if something goes wrong */ + @Deprecated Response get(String path) throws SparkPostException; + /** + * Perform an HTTP GET request. This method throws an exception if the + * server returns anything else than a 200. + * + * @param path + * API endpoint to send the request to. + * @return Server response to the request. + * @throws SparkPostException + * if something goes wrong + */ + Response get(Endpoint endpoint) throws SparkPostException; + /** * Perform an HTTP POST request. This method throws an exception if the * server returns anything else than a 200. @@ -36,12 +51,28 @@ public interface IRestConnection { * @throws SparkPostException * if something goes wrong */ + @Deprecated Response post(String path, String json) throws SparkPostException; + /** + * Perform an HTTP POST request. This method throws an exception if the + * server returns anything else than a 200. + * + * @param path + * API endpoint to send the request to. + * @param json + * POST data block to send with the request. May be null. + * @return Server response to the request. + * @throws SparkPostException + * if something goes wrong + */ + Response post(Endpoint endpoint, String json) throws SparkPostException; + /** * Perform an HTTP PUT request. This method throws an exception if the * server returns anything else than a 200. * + * @deprecated use method that uses Endpoint as an argument instead * @param path * API endpoint to send the request to. * @param json @@ -50,18 +81,47 @@ public interface IRestConnection { * @throws SparkPostException * if something goes wrong */ + @Deprecated Response put(String path, String json) throws SparkPostException; + /** + * Perform an HTTP PUT request. This method throws an exception if the + * server returns anything else than a 200. + * + * @param path + * API endpoint to send the request to. + * @param json + * PUT data block to send with the request. May be null. + * @return Server response to the request. + * @throws SparkPostException + * if something goes wrong + */ + Response put(Endpoint endpoint, String json) throws SparkPostException; + /** * Perform an HTTP DELETE request. This method throws an exception if the * server returns anything else than a 200. * + * @deprecated use method that uses Endpoint as an argument instead * @param path * API endpoint to send the request to. * @return Server response to the request. * @throws SparkPostException * if something goes wrong */ + @Deprecated Response delete(String path) throws SparkPostException; + /** + * Perform an HTTP DELETE request. This method throws an exception if the + * server returns anything else than a 200. + * + * @param endpoint + * API endpoint to send the request to. + * @return Server response to the request. + * @throws SparkPostException + * if something goes wrong + */ + Response delete(Endpoint endpoint) throws SparkPostException; + } diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java index b64085b..1ce1481 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java @@ -24,6 +24,7 @@ import com.sparkpost.exception.SparkPostIllegalServerResponseException; import com.sparkpost.model.responses.Response; import com.sparkpost.model.responses.ServerErrorResponses; +import com.sparkpost.resources.Endpoint; /** * The REST connection class wraps HTTP requests to the SparkPost API. @@ -391,6 +392,29 @@ private Response receiveErrorResponse(HttpURLConnection conn, Response response) } } + // This method actually performs the HTTP request + // It is called by get(), put(), post() and delete() below + private Response doHttpMethod(Endpoint endpoint, Method method, String data, Response response) throws SparkPostException { + HttpURLConnection conn = null; + try { + String path = endpoint.toString(); + response.setRequest(path); + conn = createConnectionObject(path, method); + sendRequest(conn, data, response); + receiveResponse(conn, response); + + if (logger.isDebugEnabled()) { + logger.debug("Server Response:" + response); + } + + return response; + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + // This method actually performs the HTTP request // It is called by get(), put(), post() and delete() below private Response doHttpMethod(String path, Method method, String data, Response response) throws SparkPostException { @@ -418,38 +442,83 @@ private Response doHttpMethod(String path, Method method, String data, Response * @see com.sparkpost.transport.IRestConnection#get(java.lang.String) */ @Override + @Deprecated public Response get(String path) throws SparkPostException { Response response = new Response(); return doHttpMethod(path, Method.GET, null, response); } + /* + * (non-Javadoc) + * @see com.sparkpost.transport.IRestConnection#get(com.sparkpost.resources.Endpoint) + */ + @Override + @Deprecated + public Response get(Endpoint endpoint) throws SparkPostException { + Response response = new Response(); + return doHttpMethod(endpoint, Method.GET, null, response); + } + /* * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#post(java.lang.String, java.lang.String) */ @Override + @Deprecated public Response post(String path, String json) throws SparkPostException { Response response = new Response(); return doHttpMethod(path, Method.POST, json, response); } + /* + * (non-Javadoc) + * @see com.sparkpost.transport.IRestConnection#post(com.sparkpost.resources.Endpoint, java.lang.String) + */ + @Override + public Response post(Endpoint endpoint, String json) throws SparkPostException { + Response response = new Response(); + return doHttpMethod(endpoint, Method.POST, json, response); + } + /* * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#put(java.lang.String, java.lang.String) */ @Override + @Deprecated public Response put(String path, String json) throws SparkPostException { Response response = new Response(); return doHttpMethod(path, Method.PUT, json, response); } + /* + * (non-Javadoc) + * @see com.sparkpost.transport.IRestConnection#put(com.sparkpost.resources.Endpoint, java.lang.String) + */ + @Override + public Response put(Endpoint endpoint, String json) throws SparkPostException { + Response response = new Response(); + return doHttpMethod(endpoint, Method.PUT, json, response); + } + /* * (non-Javadoc) * @see com.sparkpost.transport.IRestConnection#delete(java.lang.String) */ @Override + @Deprecated public Response delete(String path) throws SparkPostException { Response response = new Response(); return doHttpMethod(path, Method.DELETE, null, response); } + + /* + * (non-Javadoc) + * @see com.sparkpost.transport.IRestConnection#delete(com.sparkpost.resources.Endpoint) + */ + @Override + public Response delete(Endpoint endpoint) throws SparkPostException { + Response response = new Response(); + return doHttpMethod(endpoint, Method.DELETE, null, response); + } } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceMetricTests.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceMetricTests.java index 4525cfd..823fa9c 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceMetricTests.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceMetricTests.java @@ -55,7 +55,7 @@ public void testGetDiscoverabilityLinks() throws SparkPostException { Response discoverabilityLinks = ResourceMetrics.getDiscoverabilityLinks(conn); Assert.assertNotNull(discoverabilityLinks); - Assert.assertEquals(conn.getPath(), "/metrics/"); + Assert.assertEquals(conn.getRequestUri(), "/metrics/"); verifyWasGet(conn); } @@ -74,7 +74,7 @@ public void testGetDeliverabilityMetricsSummary() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone"); verifyWasGet(conn); } @@ -97,7 +97,7 @@ public void testGetDeliverabilityMetricsByDomain() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/domain?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit&order_by=orderBy"); verifyWasGet(conn); } @@ -120,7 +120,7 @@ public void testGetDeliverabilityMetricsByCampaign() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/campaign?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit&order_by=orderBy"); verifyWasGet(conn); } @@ -143,7 +143,7 @@ public void testGetDeliverabilityMetricsByTemplate() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/template?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit&order_by=orderBy"); verifyWasGet(conn); } @@ -166,7 +166,7 @@ public void testGetDeliverabilityMetricsByWatchedDomain() throws SparkPostExcept Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/watched-domain?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit&order_by=orderBy"); verifyWasGet(conn); } @@ -187,7 +187,7 @@ public void testGetTimeSeriesMetrics() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/time-series?precision=precision&from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone"); verifyWasGet(conn); } @@ -206,7 +206,7 @@ public void testGetBounceReasonMetrics() throws SparkPostException { Response discoverabilityLinks = ResourceMetrics.getBounceReasonMetrics(conn, from, to, domains, campaigns, templates, metrics, timezone, limit); Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/bounce-reason?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -227,7 +227,7 @@ public void testGetBounceReasonMetricsByDomain() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/bounce-reason/domain?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -248,7 +248,7 @@ public void testBounceClassificationMetrics() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/bounce-classification?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -268,7 +268,7 @@ public void testRejectionReasonMetrics() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/rejection-reason?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -288,7 +288,7 @@ public void testRejectionReasonMetricsByDomain() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/rejection-reason/domain?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -308,7 +308,7 @@ public void testDelayReasonMetrics() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/delay-reason?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -328,7 +328,7 @@ public void testDelayReasonMetricsByDomain() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/delay-reason/domain?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -348,7 +348,7 @@ public void testEngagementDetails() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/link-name?from=from%40domain.com&to=to%40domain.com&campaigns=comapigns&templates=templates&metrics=metrics&timezone=timezone&limit=limit"); verifyWasGet(conn); } @@ -367,7 +367,7 @@ public void testDeliveriesByAttempt() throws SparkPostException { Assert.assertNotNull(discoverabilityLinks); Assert.assertEquals( - conn.getPath(), + conn.getRequestUri(), "/metrics/deliverability/attempt?from=from%40domain.com&to=to%40domain.com&domains=domains&campaigns=comapigns&templates=templates&timezone=timezone"); verifyWasGet(conn); } @@ -381,7 +381,7 @@ public void testCampaignsList() throws SparkPostException { Response discoverabilityLinks = ResourceMetrics.getCampaignsList(conn, match, limit); Assert.assertNotNull(discoverabilityLinks); - Assert.assertEquals(conn.getPath(), "/metrics/campaigns?match=match&limit=limit"); + Assert.assertEquals(conn.getRequestUri(), "/metrics/campaigns?match=match&limit=limit"); verifyWasGet(conn); } @@ -394,7 +394,7 @@ public void testDomainsList() throws SparkPostException { Response discoverabilityLinks = ResourceMetrics.getDomainsList(conn, match, limit); Assert.assertNotNull(discoverabilityLinks); - Assert.assertEquals(conn.getPath(), "/metrics/domains?match=match&limit=limit"); + Assert.assertEquals(conn.getRequestUri(), "/metrics/domains?match=match&limit=limit"); verifyWasGet(conn); } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceRecipientListsTests.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceRecipientListsTests.java index db18269..928fa20 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceRecipientListsTests.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceRecipientListsTests.java @@ -73,7 +73,7 @@ public void testCreate() throws SparkPostException { Response response = ResourceRecipientLists.create(conn, maxNumberOfRecipientErrors, recipientList); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/recipient-lists?num_rcpt_errors=0"); + Assert.assertEquals(conn.getRequestUri(), "/recipient-lists?num_rcpt_errors=0"); verifyWasPost(conn); } @@ -85,7 +85,7 @@ public void testRetrieve() throws SparkPostException { Response response = ResourceRecipientLists.retrieve(conn, recipientListId, Boolean.TRUE); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/recipient-lists/recipientListId?show_recipients=true"); + Assert.assertEquals(conn.getRequestUri(), "/recipient-lists/recipientListId?show_recipients=true"); verifyWasGet(conn); } @@ -95,7 +95,7 @@ public void testListAll() throws SparkPostException { Response response = ResourceRecipientLists.listAll(conn); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/recipient-lists"); + Assert.assertEquals(conn.getRequestUri(), "/recipient-lists"); verifyWasGet(conn); } @@ -107,7 +107,7 @@ public void testDelete() throws SparkPostException { Response response = ResourceRecipientLists.delete(conn, recipientListId); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/recipient-lists/recipientListId"); + Assert.assertEquals(conn.getRequestUri(), "/recipient-lists/recipientListId"); verifyWasDelete(conn); } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSendingDomainsTests.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSendingDomainsTests.java index b2565bc..4893a96 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSendingDomainsTests.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSendingDomainsTests.java @@ -57,7 +57,7 @@ public void testCreate() throws SparkPostException { Response response = ResourceSendingDomains.create(conn, sendingDomain); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/sending-domains"); + Assert.assertEquals(conn.getRequestUri(), "/sending-domains"); verifyWasPost(conn); } @@ -69,7 +69,7 @@ public void testRetrieve() throws SparkPostException { Response response = ResourceSendingDomains.retrieve(conn, domainName); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/sending-domains/" + domainName); + Assert.assertEquals(conn.getRequestUri(), "/sending-domains/" + domainName); verifyWasGet(conn); } @@ -82,7 +82,7 @@ public void testUpdate() throws SparkPostException { Response response = ResourceSendingDomains.update(conn, domainName, sendingDomain); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/sending-domains/" + domainName); + Assert.assertEquals(conn.getRequestUri(), "/sending-domains/" + domainName); verifyWasPut(conn); } @@ -95,7 +95,7 @@ public void testVerify() throws SparkPostException { Response response = ResourceSendingDomains.verify(conn, domainName, verifyAttributes); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/sending-domains/" + domainName + "/verify"); + Assert.assertEquals(conn.getRequestUri(), "/sending-domains/" + domainName + "/verify"); verifyWasPost(conn); } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSuppressionListTests.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSuppressionListTests.java index 11448ef..e4b9738 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSuppressionListTests.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceSuppressionListTests.java @@ -58,7 +58,7 @@ public void testInsertOrUpdate() throws SparkPostException { Response response = ResourceSuppressionList.insertOrUpdate(conn, email, entry); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/suppression-list/" + email); + Assert.assertEquals(conn.getRequestUri(), "/suppression-list/" + email); verifyWasPut(conn); } @@ -70,7 +70,7 @@ public void testInsertOrUpdateBulk() throws SparkPostException { Response response = ResourceSuppressionList.insertOrUpdateBulk(conn, suppressionList); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/suppression-list/"); + Assert.assertEquals(conn.getRequestUri(), "/suppression-list/"); verifyWasPut(conn); } @@ -85,7 +85,7 @@ public void testSearch() throws SparkPostException { Response response = ResourceSuppressionList.search(conn, to, from, types, limit); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/suppression-list?to=to&from=from&types=types&limit=limit"); + Assert.assertEquals(conn.getRequestUri(), "/suppression-list?to=to&from=from&types=types&limit=limit"); verifyWasGet(conn); } @@ -97,7 +97,7 @@ public void testCheck() throws SparkPostException { Response response = ResourceSuppressionList.check(conn, email); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/suppression-list/" + email); + Assert.assertEquals(conn.getRequestUri(), "/suppression-list/" + email); verifyWasGet(conn); } @@ -109,7 +109,7 @@ public void testRemove() throws SparkPostException { Response response = ResourceSuppressionList.remove(conn, email); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/suppression-list/" + email); + Assert.assertEquals(conn.getRequestUri(), "/suppression-list/" + email); verifyWasDelete(conn); } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTemplatesTests.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTemplatesTests.java index a1e4e71..3f21b29 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTemplatesTests.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTemplatesTests.java @@ -89,7 +89,7 @@ public void testInsertOrUpdate() throws SparkPostException { Response response = ResourceTemplates.create(conn, tpl); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/templates"); + Assert.assertEquals(conn.getRequestUri(), "/templates"); verifyWasPost(conn); } @@ -102,7 +102,7 @@ public void testRetrieve() throws SparkPostException { Response response = ResourceTemplates.retrieve(conn, id, draft); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/templates/id?draft=true"); + Assert.assertEquals(conn.getRequestUri(), "/templates/id?draft=true"); verifyWasGet(conn); } @@ -113,7 +113,7 @@ public void testListAll() throws SparkPostException { Response response = ResourceTemplates.listAll(conn); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/templates/"); + Assert.assertEquals(conn.getRequestUri(), "/templates/"); verifyWasGet(conn); } @@ -127,7 +127,7 @@ public void tesUpdate() throws SparkPostException { Response response = ResourceTemplates.update(conn, id, updatePublished, tpl); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/templates/id?update_published=true"); + Assert.assertEquals(conn.getRequestUri(), "/templates/id?update_published=true"); verifyWasPut(conn); } @@ -141,7 +141,7 @@ public void tesPreview() throws SparkPostException { Response response = ResourceTemplates.preview(conn, id, draft, subst); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/templates/" + id + "/preview?draft=true"); + Assert.assertEquals(conn.getRequestUri(), "/templates/" + id + "/preview?draft=true"); verifyWasPost(conn); } @@ -153,8 +153,8 @@ public void tesDelete() throws SparkPostException { Response response = ResourceTemplates.delete(conn, id); Assert.assertNotNull(response); - System.out.println(conn.getPath()); - Assert.assertEquals(conn.getPath(), "/templates/" + id); + System.out.println(conn.getRequestUri()); + Assert.assertEquals(conn.getRequestUri(), "/templates/" + id); verifyWasDelete(conn); } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTransmissionsTests.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTransmissionsTests.java index b9d115b..a9c177b 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTransmissionsTests.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceTransmissionsTests.java @@ -75,7 +75,7 @@ public void testCreate1() throws SparkPostException { Response response = ResourceTransmissions.create(conn, numRcptErrors, trans); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/transmissions?num_rcpt_errors=0"); + Assert.assertEquals(conn.getRequestUri(), "/transmissions?num_rcpt_errors=0"); verifyWasPost(conn); } @@ -88,7 +88,7 @@ public void testCreate2() throws SparkPostException { Response response = ResourceTransmissions.create(conn, numRcptErrors, trans); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/transmissions?num_rcpt_errors=0"); + Assert.assertEquals(conn.getRequestUri(), "/transmissions?num_rcpt_errors=0"); verifyWasPost(conn); } @@ -100,7 +100,7 @@ public void testRetrieve() throws SparkPostException { Response response = ResourceTransmissions.retrieve(conn, id); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/transmissions/" + id); + Assert.assertEquals(conn.getRequestUri(), "/transmissions/" + id); verifyWasGet(conn); } @@ -113,7 +113,7 @@ public void testList() throws SparkPostException { Response response = ResourceTransmissions.list(conn, campaignId, templateId); Assert.assertNotNull(response); - Assert.assertEquals(conn.getPath(), "/transmissions?campaign_id=campaignId&template_id=templateId"); + Assert.assertEquals(conn.getRequestUri(), "/transmissions?campaign_id=campaignId&template_id=templateId"); verifyWasGet(conn); } diff --git a/libs/sparkpost-lib/src/test/java/com/sparkpost/testhelpers/StubRestConnection.java b/libs/sparkpost-lib/src/test/java/com/sparkpost/testhelpers/StubRestConnection.java index 501d434..20e05b1 100644 --- a/libs/sparkpost-lib/src/test/java/com/sparkpost/testhelpers/StubRestConnection.java +++ b/libs/sparkpost-lib/src/test/java/com/sparkpost/testhelpers/StubRestConnection.java @@ -3,12 +3,15 @@ import com.sparkpost.exception.SparkPostException; import com.sparkpost.model.responses.Response; +import com.sparkpost.resources.Endpoint; import com.sparkpost.transport.IRestConnection; public class StubRestConnection implements IRestConnection { private String path; + private Endpoint endpoint; + private String json; private Response response; @@ -56,6 +59,36 @@ public Response delete(String path) throws SparkPostException { return this.response; } + @Override + public Response get(Endpoint endpoint) throws SparkPostException { + this.endpoint = endpoint; + this.wasGet = true; + return this.response; + } + + @Override + public Response post(Endpoint endpoint, String json) throws SparkPostException { + this.endpoint = endpoint; + this.wasPost = true; + this.json = json; + return this.response; + } + + @Override + public Response put(Endpoint endpoint, String json) throws SparkPostException { + this.endpoint = endpoint; + this.wasPut = true; + this.json = json; + return this.response; + } + + @Override + public Response delete(Endpoint endpoint) throws SparkPostException { + this.endpoint = endpoint; + this.wasDelete = true; + return this.response; + } + public String getPath() { return this.path; } @@ -64,6 +97,21 @@ public void setPath(String path) { this.path = path; } + public Endpoint getEndpoint() { + return this.endpoint; + } + + public void setEndpoint(Endpoint endpoint) { + this.endpoint = endpoint; + } + + public String getRequestUri() { + if (this.endpoint != null && this.endpoint.toString() != null) { + return this.endpoint.toString(); + } + return this.path; + } + public String getJson() { return this.json; }