From 9110acd14a7545bc548b61931c28be0675d7718b Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sun, 26 Jun 2016 22:51:11 -0400 Subject: [PATCH] More work on Zang integration --- .../dataImport/converters/UserConverter.java | 6 +++++- .../net/frozenorb/apiv3/model/Server.java | 2 -- .../apiv3/unsorted/Notification.java | 3 ++- .../net/frozenorb/apiv3/util/ZangUtils.java | 21 +++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java index 2e6a9f4..dbc2623 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java @@ -47,11 +47,15 @@ public final class UserConverter implements Block { null, null, user.getString("email"), - Instant.EPOCH, + user.containsKey("email") ? Instant.EPOCH : null, null, null, null, user.getString("phone"), + user.containsKey("phone") ? Instant.EPOCH : null, + null, + null, + null, "INVALID", user.getDate("joined").toInstant(), user.getDate("joined").toInstant(), diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index f4d47e1..227f4af 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -1,7 +1,6 @@ package net.frozenorb.apiv3.model; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoCollection; @@ -12,7 +11,6 @@ import fr.javatic.mongo.jacksonCodec.objectId.Id; import lombok.Getter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies; -import net.frozenorb.apiv3.util.TimeUtils; import org.bson.Document; import java.time.Instant; diff --git a/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java b/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java index 0c558fc..1fe3204 100644 --- a/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java +++ b/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java @@ -5,6 +5,7 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.util.MandrillUtils; +import net.frozenorb.apiv3.util.ZangUtils; import java.util.Map; @@ -36,7 +37,7 @@ public final class Notification { } public void sendAsText(String phoneNumber, SingleResultCallback callback) { - callback.onResult(null, new UnsupportedOperationException()); + ZangUtils.sendText(phoneNumber, body, callback); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java b/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java index 957132c..ef7d3c8 100644 --- a/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java @@ -21,6 +21,27 @@ public class ZangUtils { private static final String zangAuthToken = APIv3.getConfig().getProperty("zang.authToken"); private static final HttpClient httpsClient = APIv3.getVertxInstance().createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true)); + public static void sendText(String to, String messageBody, SingleResultCallback callback) { + String authHeader = "Basic " + Base64.getEncoder().encodeToString((zangAccountSid + ":" + zangAuthToken).getBytes(Charsets.UTF_8)); + + httpsClient.post(443, "api.zang.io", "/v2/Accounts/" + zangAccountSid + "/SMS/Messages.json", (response) -> { + response.bodyHandler((body) -> { + JsonObject bodyJson = new JsonObject(body.toString()); + + if (bodyJson.getString("status", "").equals("queued")) { + callback.onResult(null, null); + } else { + callback.onResult(null, new IOException("Could not send text message: " + bodyJson.encode())); + } + }); + + response.exceptionHandler((error) -> callback.onResult(null, error)); + }) + .putHeader("Authorization", authHeader) + .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()) + .end("To=" + to + "&From=339-337-5300&Body=" + messageBody); + } + public static void getCarrierInfo(String phoneNumber, SingleResultCallback callback) { String authHeader = "Basic " + Base64.getEncoder().encodeToString((zangAccountSid + ":" + zangAuthToken).getBytes(Charsets.UTF_8));