From 04369a1f97317eaded310f2053c85814327a1013 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 24 Jun 2016 22:54:49 -0400 Subject: [PATCH] Add basic Zang functionality --- apiv3.properties | 2 + .../net/frozenorb/apiv3/util/ZangUtils.java | 49 +++++++++++++++++++ .../net/frozenorb/apiv3/zang/ZangResult.java | 22 +++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/main/java/net/frozenorb/apiv3/util/ZangUtils.java create mode 100644 src/main/java/net/frozenorb/apiv3/zang/ZangResult.java diff --git a/apiv3.properties b/apiv3.properties index 9ee8927..7db7b9f 100644 --- a/apiv3.properties +++ b/apiv3.properties @@ -9,5 +9,7 @@ http.port=80 mandrill.apiKey=0OYtwymqJP6oqvszeJu0vQ maxMind.userId=66817 maxMind.maxMindLicenseKey=8Aw9NsOUeOp7 +zang.accountSid=ACf18890845596403e330944d98886440c +zang.authToken=dc70bbd1fbd8411ba133fa93813a461b auth.websiteApiKey=RVbp4hY6sCFVaf auth.bungeeApiKey=6d9cf76dc9f0d23 \ 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 new file mode 100644 index 0000000..93186c3 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java @@ -0,0 +1,49 @@ +package net.frozenorb.apiv3.util; + +import com.google.common.base.Charsets; +import com.google.common.net.MediaType; +import com.mongodb.async.SingleResultCallback; +import io.vertx.core.http.HttpClient; +import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.HttpHeaders; +import io.vertx.core.json.JsonObject; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.zang.ZangResult; + +import java.io.IOException; +import java.util.Base64; + +@UtilityClass +public class ZangUtils { + + private static final String zangAccountSid = APIv3.getConfig().getProperty("zang.accountSid"); + 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 getCarrierInfo(String phoneNumber, SingleResultCallback callback) { + String authHeader = "Basic " + Base64.getEncoder().encodeToString((zangAccountSid + ":" + zangAuthToken).getBytes(Charsets.UTF_8)); + + httpsClient.post(443, "api.zang.io", "/v2/Accounts/" + zangAccountSid + "/Lookups/Carrier.json", (response) -> { + response.bodyHandler((body) -> { + JsonObject bodyJson = new JsonObject(body.toString()); + + if (bodyJson.containsKey("carrier_lookups")) { + // Zang returns an array, but we don't batch at all so we always just get the first element + JsonObject lookupResult = bodyJson.getJsonArray("carrier_lookups").getJsonObject(0); + callback.onResult(new ZangResult(lookupResult), null); + } else { + callback.onResult(null, new IOException("Could not parse Zang result: " + bodyJson.encode())); + } + }); + + response.exceptionHandler((error) -> { + callback.onResult(null, error); + }); + }) + .putHeader("Authorization", authHeader) + .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()) + .end("PhoneNumber=" + phoneNumber); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/zang/ZangResult.java b/src/main/java/net/frozenorb/apiv3/zang/ZangResult.java new file mode 100644 index 0000000..78fb3ab --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/zang/ZangResult.java @@ -0,0 +1,22 @@ +package net.frozenorb.apiv3.zang; + +import io.vertx.core.json.JsonObject; +import lombok.Getter; + +public final class ZangResult { + + @Getter private String countryCode; + @Getter private int carrierId; + @Getter private String network; + @Getter private boolean mobile; + + private ZangResult() {} // For Jackson + + public ZangResult(JsonObject legacy) { + this.countryCode = legacy.getString("country_code"); + this.carrierId = legacy.getInteger("carrier_id"); + this.network = legacy.getString("network"); + this.mobile = Boolean.parseBoolean(legacy.getString("mobile")); + } + +} \ No newline at end of file