From 5c06b01bb53df2b81533fa057055fe246dc6fc5b Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Tue, 21 Nov 2017 10:57:27 +0100 Subject: [PATCH] final refactoring before release --- README.md | 3 +- pom.xml | 2 +- .../postmark/client/AccountApiClient.java | 44 ++++---- .../java/postmark/client/ApiClient.java | 106 ++++++++++-------- .../java/postmark/client/Parameters.java | 28 ++--- src/test/java/unit/client/ParametersTest.java | 8 +- 6 files changed, 98 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index f2d201f..d8f6640 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ Add the dependency to your project: ``` -Note: to retrieve version, visit [maven central repository](http://repo1.maven.org/maven2/com/wildbit/java/postmark/) , or check the badge at top of the page,showing latest version in Maven repository archives. +Note: to retrieve version, visit [maven central repository](http://repo1.maven.org/maven2/com/wildbit/java/postmark/) , or check the badge at top of the page, showing latest version synced in Maven repository archives. +Maven central repository might be slightly acurrate, in case new version was published recently. ## Issues & Comments diff --git a/pom.xml b/pom.xml index ca22a7a..fb5c4ca 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ - 0.9.3 + 1.0.0 UTF-8 1.8 5.0.0-M4 diff --git a/src/main/java/com/wildbit/java/postmark/client/AccountApiClient.java b/src/main/java/com/wildbit/java/postmark/client/AccountApiClient.java index 5c04e0c..b1c4def 100644 --- a/src/main/java/com/wildbit/java/postmark/client/AccountApiClient.java +++ b/src/main/java/com/wildbit/java/postmark/client/AccountApiClient.java @@ -19,6 +19,10 @@ */ public class AccountApiClient extends BaseApiClient { + private final String serversEndpoint = "/servers/"; + private final String domainsEndpoint = "/domains/"; + private final String sendersEndpoint = "/senders/"; + public AccountApiClient(String baseUrl, MultivaluedHashMap headers) { super(baseUrl,headers); } @@ -33,27 +37,27 @@ public AccountApiClient(String baseUrl, MultivaluedHashMap header public Server getServers(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/servers/" + id)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(serversEndpoint + id)); return dataHandler.fromJson(response, Server.class); } public Server createServer(Server data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/servers"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(serversEndpoint), data); return dataHandler.fromJson(response, Server.class); } public Server setServer(Integer id, Server data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/servers/" + id), data); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(serversEndpoint + id), data); return dataHandler.fromJson(response, Server.class); } public Servers getServers(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/servers" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(serversEndpoint + parameters)); return dataHandler.fromJson(response, Servers.class); } public String deleteServer(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl("/servers/" + id)); + return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl(serversEndpoint + id)); } @@ -62,35 +66,35 @@ public String deleteServer(Integer id) throws PostmarkException, IOException { */ public Domains getDomains(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/domains" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(domainsEndpoint + parameters)); return dataHandler.fromJson(response, Domains.class); } public DomainDetails getDomainDetails(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/domains/" + id)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(domainsEndpoint + id)); return dataHandler.fromJson(response, DomainDetails.class); } public DomainDetails createDomain(Domain domain) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/domains"), domain); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(domainsEndpoint), domain); return dataHandler.fromJson(response, DomainDetails.class); } public DomainDetails setDomain(Integer id, DomainDetails domain) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/domains/" + id), domain); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(domainsEndpoint + id), domain); return dataHandler.fromJson(response, DomainDetails.class); } public String deleteDomain(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl("/domains/" + id)); + return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl(domainsEndpoint + id)); } public String verifyDomainSPF(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/domains/" + id + "/verifySPF")); + return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(domainsEndpoint + id + "/verifySPF")); } public String rotateDomainDKIM(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/domains/" + id + "/rotateDKIM")); + return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(domainsEndpoint + id + "/rotateDKIM")); } @@ -99,40 +103,40 @@ public String rotateDomainDKIM(Integer id) throws PostmarkException, IOException */ public Signatures getSenderSignatures(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/senders" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(sendersEndpoint + parameters)); return dataHandler.fromJson(response, Signatures.class); } public SignatureDetails getSenderSignatureDetails(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/senders/" + id)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(sendersEndpoint + id)); return dataHandler.fromJson(response, SignatureDetails.class); } public SignatureDetails createSenderSignature(SignatureToCreate signature) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/senders"), signature); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendersEndpoint), signature); return dataHandler.fromJson(response, SignatureDetails.class); } public SignatureDetails setSenderSignature(Integer id, SignatureToCreate signature) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/senders/" + id), signature); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(sendersEndpoint + id), signature); return dataHandler.fromJson(response, SignatureDetails.class); } public String deleteSenderSignature(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl("/senders/" + id)); + return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl(sendersEndpoint + id)); } public String resendSenderSignatureConfirmation(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/senders/" + id + "/resend")); + return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendersEndpoint + id + "/resend")); } public SignatureDetails verifySenderSignatureSPF(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/senders/" + id + "/verifySPF")); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendersEndpoint + id + "/verifySPF")); return dataHandler.fromJson(response, SignatureDetails.class); } public String requestSenderSignatureDKIM(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/senders/" + id + "/requestNewDKIM")); + return execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendersEndpoint + id + "/requestNewDKIM")); } } diff --git a/src/main/java/com/wildbit/java/postmark/client/ApiClient.java b/src/main/java/com/wildbit/java/postmark/client/ApiClient.java index f91d3b0..450c3eb 100644 --- a/src/main/java/com/wildbit/java/postmark/client/ApiClient.java +++ b/src/main/java/com/wildbit/java/postmark/client/ApiClient.java @@ -29,6 +29,16 @@ */ public class ApiClient extends BaseApiClient { + private final String bouncesEndpoint = "/bounces/"; + private final String templatesEndpoint = "/templates/"; + private final String serverEndpoint = "/server/"; + private final String outboundMessagesEndpoint = "/messages/outbound/"; + private final String inboundMessagesEndpoint = "/messages/inbound/"; + private final String outboundStatsEndpoint = "/stats/outbound/"; + private final String triggerTagsEndpoint = "/triggers/tags/"; + private final String triggerInboundRulesEndpoint = "/triggers/inboundRules/"; + private final String sendingEndpoint = "/email/"; + public ApiClient(String baseUrl, MultivaluedHashMap headers) { super(baseUrl,headers); } @@ -42,12 +52,12 @@ public ApiClient(String baseUrl, MultivaluedHashMap headers, bo */ public MessageResponse deliverMessage(Message data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/email"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendingEndpoint), data); return dataHandler.fromJson(response, MessageResponse.class); } public ArrayList deliverMessage(ArrayList data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/email/batch"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendingEndpoint + "batch"), data); return dataHandler.fromJson(response, new TypeReference>() {}); } @@ -62,26 +72,26 @@ public DeliveryStats getDeliveryStats() throws PostmarkException, IOException { } public Bounces getBounces(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/bounces" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(bouncesEndpoint + parameters)); return dataHandler.fromJson(response, Bounces.class); } public Bounce getBounce(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/bounces/" + id)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(bouncesEndpoint + id)); return dataHandler.fromJson(response, Bounce.class); } public BounceDump getBounceDump(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/bounces/" + id + "/dump")); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(bouncesEndpoint + id + "/dump")); return dataHandler.fromJson(response, BounceDump.class); } public String activateBounce(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/bounces/" + id + "/activate")); + return execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(bouncesEndpoint + id + "/activate")); } public ArrayList getBounceTags() throws PostmarkException, IOException { - return dataHandler.fromJson(execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/bounces/tags")), ArrayList.class); + return dataHandler.fromJson(execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(bouncesEndpoint + "tags")), ArrayList.class); } @@ -90,31 +100,31 @@ public ArrayList getBounceTags() throws PostmarkException, IOException { */ public Template getTemplate(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/templates/" + id)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(templatesEndpoint + id)); return dataHandler.fromJson(response, Template.class); } public BaseTemplate createTemplate(TemplateContent data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/templates/"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(templatesEndpoint), data); return dataHandler.fromJson(response, BaseTemplate.class); } public BaseTemplate setTemplate(Integer id, TemplateContent data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/templates/" + id), data); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(templatesEndpoint + id), data); return dataHandler.fromJson(response, BaseTemplate.class); } public Templates getTemplates(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/templates/" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(templatesEndpoint + parameters)); return dataHandler.fromJson(response, Templates.class); } public String deleteTemplate(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl("/templates/" + id)); + return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl(templatesEndpoint + id)); } public TemplateValidation validateTemplate(TemplateToValidate data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/templates/validate"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(templatesEndpoint + "validate"), data); return dataHandler.fromJson(response, TemplateValidation.class); } @@ -127,7 +137,7 @@ public MessageResponse deliverMessage(TemplatedMessage data) throws PostmarkExce if (data.getTemplateModel().getClass() == String.class) { data.setTemplateModel(dataHandler.fromJson(data.getTemplateModel().toString(),Object.class)); } - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/email/withTemplate"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(sendingEndpoint + "withTemplate"), data); System.out.println(response); return dataHandler.fromJson(response, MessageResponse.class); } @@ -138,12 +148,12 @@ public MessageResponse deliverMessage(TemplatedMessage data) throws PostmarkExce */ public Server getServer() throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/server")); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(serverEndpoint)); return dataHandler.fromJson(response, Server.class); } public Server setServer(Server data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/server"), data); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(serverEndpoint), data); return dataHandler.fromJson(response, Server.class); } @@ -156,37 +166,37 @@ public Server setServer(Server data) throws PostmarkException, IOException { */ public OutboundMessages getMessages(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + parameters)); return dataHandler.fromJson(response, OutboundMessages.class); } public OutboundMessageDetails getMessageDetails(String id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound/" + id + "/details")); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + id + "/details")); return dataHandler.fromJson(response, OutboundMessageDetails.class); } public OutboundMessageDump getMessageDump(String id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound/" + id + "/dump")); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + id + "/dump")); return dataHandler.fromJson(response, OutboundMessageDump.class); } public OutboundMessageOpens getMessageOpens(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound/opens" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + "opens" + parameters)); return dataHandler.fromJson(response, OutboundMessageOpens.class); } public OutboundMessageOpens getMessageOpens(String id, Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound/opens/" + id + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + "opens/" + id + parameters)); return dataHandler.fromJson(response, OutboundMessageOpens.class); } public OutboundMessageClicks getMessageClicks(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound/clicks" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + "clicks" + parameters)); return dataHandler.fromJson(response, OutboundMessageClicks.class); } public OutboundMessageClicks getMessageClicks(String id, Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/outbound/clicks/" + id + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundMessagesEndpoint + "clicks/" + id + parameters)); return dataHandler.fromJson(response, OutboundMessageClicks.class); } @@ -195,22 +205,22 @@ public OutboundMessageClicks getMessageClicks(String id, Parameters parameters) */ public InboundMessages getInboundMessages(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/inbound" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(inboundMessagesEndpoint + parameters)); return dataHandler.fromJson(response, InboundMessages.class); } public InboundMessageDetails getInboundMessageDetails(String id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/messages/inbound/" + id + "/details")); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(inboundMessagesEndpoint + id + "/details")); return dataHandler.fromJson(response, InboundMessageDetails.class); } public String bypassInboundMessage(String id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/messages/inbound/" + id + "/bypass")); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(inboundMessagesEndpoint + id + "/bypass")); return dataHandler.fromJson(response, String.class); } public String retryFailedInboundMessage(String id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/messages/inbound/" + id + "/retry")); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(inboundMessagesEndpoint + id + "/retry")); return dataHandler.fromJson(response, String.class); } @@ -219,67 +229,67 @@ public String retryFailedInboundMessage(String id) throws PostmarkException, IOE */ public OutboundStats getOutboundStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + parameters)); return dataHandler.fromJson(response, OutboundStats.class); } public OutboundSendStats getOutboundSendStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/sends" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "sends" + parameters)); return dataHandler.fromJson(response, OutboundSendStats.class); } public OutboundBounceStats getOutboundBounceStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/bounces" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "bounces" + parameters)); return dataHandler.fromJson(response, OutboundBounceStats.class); } public OutboundSpamStats getOutboundSpamStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/spam" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "spam" + parameters)); return dataHandler.fromJson(response, OutboundSpamStats.class); } public OutboundTrackedStats getOutboundTrackedStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/tracked" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "tracked" + parameters)); return dataHandler.fromJson(response, OutboundTrackedStats.class); } public OutboundOpenStats getOutboundOpenStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/opens" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "opens" + parameters)); return dataHandler.fromJson(response, OutboundOpenStats.class); } public OutboundOpenPlatformStats getOutboundOpenPlatformStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/opens/platforms" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "opens/platforms" + parameters)); return dataHandler.fromJson(response, OutboundOpenPlatformStats.class); } public HashMap getOutboundOpenClientStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/opens/platforms" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "opens/platforms" + parameters)); return dataHandler.fromJson(response, HashMap.class); } public HashMap getOutboundOpenReadTimes(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/opens/readTimes" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "opens/readTimes" + parameters)); return dataHandler.fromJson(response, HashMap.class); } public OutboundClickStats getOutboundClickStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/clicks" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "clicks" + parameters)); return dataHandler.fromJson(response, OutboundClickStats.class); } public HashMap getOutboundClickBrowsersStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/clicks/browserfamilies" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "clicks/browserfamilies" + parameters)); return dataHandler.fromJson(response, HashMap.class); } public OutboundClickPlatformStats getOutboundClickPlatformStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/clicks/platforms" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "clicks/platforms" + parameters)); return dataHandler.fromJson(response, OutboundClickPlatformStats.class); } public OutboundClickLocationStats getOutboundClickLocationStats(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/stats/outbound/clicks/location" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(outboundStatsEndpoint + "clicks/location" + parameters)); return dataHandler.fromJson(response, OutboundClickLocationStats.class); } @@ -289,26 +299,26 @@ public OutboundClickLocationStats getOutboundClickLocationStats(Parameters param */ public TagMatcher createTriggerTag(TagMatcher data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/triggers/tags"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(triggerTagsEndpoint), data); return dataHandler.fromJson(response, TagMatcher.class); } public TagMatcher getTriggerTag(Integer id) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/triggers/tags/" + id)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(triggerTagsEndpoint + id)); return dataHandler.fromJson(response, TagMatcher.class); } public TagMatcher setTriggerTag(Integer id, TagMatcher data) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl("/triggers/tags/" + id), data); + String response = execute(HttpClient.REQUEST_TYPES.PUT, getEndpointUrl(triggerTagsEndpoint + id), data); return dataHandler.fromJson(response, TagMatcher.class); } public String deleteTriggerTag(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl("/triggers/tags/" + id)); + return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl(triggerTagsEndpoint + id)); } public TagMatchers getTriggerTags(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/triggers/tags" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(triggerTagsEndpoint + parameters)); return dataHandler.fromJson(response, TagMatchers.class); } @@ -320,16 +330,16 @@ public TagMatchers getTriggerTags(Parameters parameters) throws PostmarkExceptio public InboundRuleResponse createInboundRule(String rule) throws PostmarkException, IOException { InboundRule data = new InboundRule(); data.setRule(rule); - String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl("/triggers/inboundRules"), data); + String response = execute(HttpClient.REQUEST_TYPES.POST, getEndpointUrl(triggerInboundRulesEndpoint), data); return dataHandler.fromJson(response, InboundRuleResponse.class); } public String deleteInboundRule(Integer id) throws PostmarkException, IOException { - return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl("/triggers/inboundRules/" + id)); + return execute(HttpClient.REQUEST_TYPES.DELETE, getEndpointUrl(triggerInboundRulesEndpoint + id)); } public InboundRules getInboundRules(Parameters parameters) throws PostmarkException, IOException { - String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl("/triggers/inboundRules" + parameters)); + String response = execute(HttpClient.REQUEST_TYPES.GET, getEndpointUrl(triggerInboundRulesEndpoint + parameters)); return dataHandler.fromJson(response, InboundRules.class); } diff --git a/src/main/java/com/wildbit/java/postmark/client/Parameters.java b/src/main/java/com/wildbit/java/postmark/client/Parameters.java index 7cac117..091ad29 100644 --- a/src/main/java/com/wildbit/java/postmark/client/Parameters.java +++ b/src/main/java/com/wildbit/java/postmark/client/Parameters.java @@ -3,6 +3,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; /** * Helper class for creating parameters to pass to API requests. It makes sure proper format of parameters @@ -64,28 +65,17 @@ public Parameters build(String name, Date value) { * @return parameters transformed to proper string format API client accepts */ public String toString() { - String parameterString = ""; + StringBuilder parameterString = new StringBuilder(); + int count = 0; for (HashMap.Entry entry : parameters.entrySet()) { - parameterString += addParameters(parameterString, entry.getKey() + "=" + entry.getValue()); + parameterString.append(count==0 ? "?" : "&"); + parameterString.append(entry.getKey()); + parameterString.append("="); + parameterString.append(entry.getValue()); + count++; } - return parameterString; - } - - /** - * Convert parameter to a String to attach to parameter list string. - * - * @param parameter single HTTP request parameter name - * @param value single HTTP request parameter value - * @return parameter as String - */ - private String addParameters(String parameter, String value) { - if (parameter.isEmpty()) { - return "?" + value; - } - else{ - return "&" + value; - } + return parameterString.toString(); } } diff --git a/src/test/java/unit/client/ParametersTest.java b/src/test/java/unit/client/ParametersTest.java index 2a6c513..d4ef5a6 100644 --- a/src/test/java/unit/client/ParametersTest.java +++ b/src/test/java/unit/client/ParametersTest.java @@ -40,24 +40,24 @@ void build() { @Test void stringParameter() { Parameters parameters = Parameters.init().build("fromAddress","igor@example.com"); - assertEquals(parameters.toString(),"?fromAddress=igor@example.com"); + assertEquals("?fromAddress=igor@example.com",parameters.toString()); } @Test void integerParameter() { Parameters parameters = Parameters.init().build("count",1); - assertEquals(parameters.toString(),"?count=1"); + assertEquals("?count=1",parameters.toString()); } @Test void dateParameter() throws ParseException { String parameters = Parameters.init().build("fromDate", sampleDate(dateString)).toString(); - assertEquals(parameters,"?fromDate=" + dateString); + assertEquals("?fromDate=" + dateString, parameters.toString()); } @Test void multipleParameters() throws ParseException { Parameters parameters = Parameters.init().build("fromDate",sampleDate(dateString)).build("count",1).build("offset",0); - assertEquals(parameters.toString(),"?fromDate=" + dateString + "&offset=0&count=1"); + assertEquals("?fromDate=" + dateString + "&offset=0&count=1",parameters.toString()); } }