Move mandrill sending from inside notifications to its own utility

This commit is contained in:
Colin McDonald 2016-06-24 22:36:15 -04:00
parent 8972605a02
commit 0a036f5ec5
2 changed files with 35 additions and 18 deletions

View File

@ -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<Void> callback) {

View File

@ -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<Void> 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());
}
}