Move mandrill sending from inside notifications to its own utility
This commit is contained in:
parent
8972605a02
commit
0a036f5ec5
|
@ -1,19 +1,15 @@
|
||||||
package net.frozenorb.apiv3.unsorted;
|
package net.frozenorb.apiv3.unsorted;
|
||||||
|
|
||||||
import com.google.common.net.MediaType;
|
|
||||||
import com.mongodb.async.SingleResultCallback;
|
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.JsonArray;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import net.frozenorb.apiv3.APIv3;
|
|
||||||
import net.frozenorb.apiv3.model.NotificationTemplate;
|
import net.frozenorb.apiv3.model.NotificationTemplate;
|
||||||
|
import net.frozenorb.apiv3.util.MandrillUtils;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public final class Notification {
|
public final class Notification {
|
||||||
|
|
||||||
private static final HttpClient httpClient = APIv3.getVertxInstance().createHttpClient();
|
|
||||||
private final String subject;
|
private final String subject;
|
||||||
private final String body;
|
private final String body;
|
||||||
|
|
||||||
|
@ -36,19 +32,7 @@ public final class Notification {
|
||||||
.put("type", "to")
|
.put("type", "to")
|
||||||
));
|
));
|
||||||
|
|
||||||
JsonObject body = new JsonObject()
|
MandrillUtils.sendEmail(message, callback);
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendAsText(String phoneNumber, SingleResultCallback<Void> callback) {
|
public void sendAsText(String phoneNumber, SingleResultCallback<Void> callback) {
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue