From 0a036f5ec58a6f17e683ebc05ff46a8770ac8025 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 24 Jun 2016 22:36:15 -0400 Subject: [PATCH] Move mandrill sending from inside notifications to its own utility --- .../apiv3/unsorted/Notification.java | 20 ++--------- .../frozenorb/apiv3/util/MandrillUtils.java | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java diff --git a/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java b/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java index d13b60e..0c558fc 100644 --- a/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java +++ b/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java @@ -1,19 +1,15 @@ package net.frozenorb.apiv3.unsorted; -import com.google.common.net.MediaType; import com.mongodb.async.SingleResultCallback; -import io.vertx.core.http.HttpClient; -import io.vertx.core.http.HttpHeaders; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; -import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.NotificationTemplate; +import net.frozenorb.apiv3.util.MandrillUtils; import java.util.Map; public final class Notification { - private static final HttpClient httpClient = APIv3.getVertxInstance().createHttpClient(); private final String subject; private final String body; @@ -36,19 +32,7 @@ public final class Notification { .put("type", "to") )); - JsonObject body = new JsonObject() - .put("key", APIv3.getConfig().getProperty("mandrill.apiKey")) - .put("message", message); - - httpClient.post("mandrillapp.com", "/api/1.0/messages/send.json", (response) -> { - response.bodyHandler((resultBody) -> { - callback.onResult(null, null); - }); - - response.exceptionHandler((error) -> { - callback.onResult(null, error); - }); - }).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).end(body.encode()); + MandrillUtils.sendEmail(message, callback); } public void sendAsText(String phoneNumber, SingleResultCallback callback) { diff --git a/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java b/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java new file mode 100644 index 0000000..1378fe9 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.util; + +import com.google.common.net.MediaType; +import com.mongodb.async.SingleResultCallback; +import io.vertx.core.http.HttpClient; +import io.vertx.core.http.HttpHeaders; +import io.vertx.core.json.JsonObject; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; + +@UtilityClass +public class MandrillUtils { + + private static final String mandrillApiKey = APIv3.getConfig().getProperty("mandrill.apiKey"); + private static final HttpClient httpClient = APIv3.getVertxInstance().createHttpClient(); + + public static void sendEmail(JsonObject message, SingleResultCallback callback) { + JsonObject body = new JsonObject() + .put("key", mandrillApiKey) + .put("message", message); + + httpClient.post("mandrillapp.com", "/api/1.0/messages/send.json", (response) -> { + response.bodyHandler((resultBody) -> { + callback.onResult(null, null); + }); + + response.exceptionHandler((error) -> { + callback.onResult(null, error); + }); + }).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).end(body.encode()); + } + +} \ No newline at end of file