Add basic Zang functionality

This commit is contained in:
Colin McDonald 2016-06-24 22:54:49 -04:00
parent a68a44f781
commit 04369a1f97
3 changed files with 73 additions and 0 deletions

View File

@ -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

View File

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

View File

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