Skip to content

Commit

Permalink
use gson model for input
Browse files Browse the repository at this point in the history
  • Loading branch information
jon8787 committed Feb 21, 2024
1 parent f84fb01 commit 1a90521
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 76 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/uid2/client/IdentityMapHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.uid2.client;

import com.google.gson.Gson;

import java.nio.charset.StandardCharsets;

public class IdentityMapHelper {
Expand All @@ -13,7 +15,7 @@ public class IdentityMapHelper {
* @return an EnvelopeV2 instance to use in the POST body of <a href="https://unifiedid.com/docs/endpoints/post-identity-map">/identity/map</a>
*/
public EnvelopeV2 createEnvelopeForIdentityMapRequest(IdentityMapInput identityMapInput) {
byte[] jsonBytes = identityMapInput.getAsJsonString().getBytes(StandardCharsets.UTF_8);
byte[] jsonBytes = new Gson().toJson(identityMapInput).getBytes(StandardCharsets.UTF_8);
return uid2Helper.createEnvelopeV2(jsonBytes);
}

Expand Down
90 changes: 33 additions & 57 deletions src/main/java/com/uid2/client/IdentityMapInput.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.uid2.client;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -40,73 +38,51 @@ public static IdentityMapInput fromHashedPhones(Iterable<String> hashedPhones) {
return new IdentityMapInput(IdentityType.Phone, hashedPhones, true);
}

String getAsJsonString() {
if (alreadyHashed) {
return createAlreadyHashedJsonRequestForIdentityMap(identityType, emailsOrPhones);
} else {
return createHashedJsonRequestForIdentityMap(identityType, emailsOrPhones);
}
}

private IdentityMapInput(IdentityType identityType, Iterable<String> emailsOrPhones, boolean alreadyHashed) {
this.identityType = identityType;
this.emailsOrPhones = emailsOrPhones;
this.alreadyHashed = alreadyHashed;
}


private static String createJsonRequestForIdentityMap(String property, Iterable<String> values) {
JsonObject json = new JsonObject();

JsonArray jsonArray = new JsonArray();
for (String value : values) {
jsonArray.add(value);
}

json.add(property, jsonArray);
return json.toString();
}

private String createHashedJsonRequestForIdentityMap(IdentityType identityType, Iterable<String> unhashedValues) {
if (identityType == IdentityType.Email) {
List<String> hashedNormalizedEmails = new ArrayList<>();
for (String unnormalizedEmail : unhashedValues) {
String hashedEmail = InputUtil.normalizeAndHashEmail(unnormalizedEmail);
hashedNormalizedEmails.add(hashedEmail);
hashedDiiToRawDii.put(hashedEmail, unnormalizedEmail);
hashedNormalizedEmails = new ArrayList<>();
for (String email : emailsOrPhones) {
if (alreadyHashed) {
hashedNormalizedEmails.add(email);
} else {
String hashedEmail = InputUtil.normalizeAndHashEmail(email);
hashedNormalizedEmails.add(hashedEmail);
hashedDiiToRawDii.put(hashedEmail, email);
}
}
return createJsonRequestForIdentityMap("email_hash", hashedNormalizedEmails);
} else { //phone
List<String> hashedNormalizedPhones = new ArrayList<>();
for (String phone : unhashedValues) {
if (!InputUtil.isPhoneNumberNormalized(phone)) {
throw new IllegalArgumentException("phone number is not normalized: " + phone);
hashedNormalizedPhones = new ArrayList<>();
for (String phone : emailsOrPhones) {
if (alreadyHashed) {
hashedNormalizedPhones.add(phone);
} else {
if (!InputUtil.isPhoneNumberNormalized(phone)) {
throw new IllegalArgumentException("phone number is not normalized: " + phone);
}

String hashedNormalizedPhone = InputUtil.getBase64EncodedHash(phone);
hashedDiiToRawDii.put(hashedNormalizedPhone, phone);
hashedNormalizedPhones.add(hashedNormalizedPhone);
}

String hashedNormalizedPhone = InputUtil.getBase64EncodedHash(phone);
hashedDiiToRawDii.put(hashedNormalizedPhone, phone);
hashedNormalizedPhones.add(hashedNormalizedPhone);
}
return createJsonRequestForIdentityMap("phone_hash", hashedNormalizedPhones);
}
}

private static String createAlreadyHashedJsonRequestForIdentityMap(IdentityType identityType, Iterable<String> hashedValues) {
if (identityType == IdentityType.Email) {
return createJsonRequestForIdentityMap("email_hash", hashedValues);
} else { //phone
return createJsonRequestForIdentityMap("phone_hash", hashedValues);
}
}

String getRawDii(String identifier) {
if (alreadyHashed)
if (wasInputAlreadyHashed())
return identifier;
return hashedDiiToRawDii.get(identifier);
}

private final IdentityType identityType;
private final Iterable<String> emailsOrPhones;
private final boolean alreadyHashed;
private final HashMap<String, String> hashedDiiToRawDii = new HashMap<>();
private boolean wasInputAlreadyHashed() {
return hashedDiiToRawDii.isEmpty();
}

@SerializedName("email_hash")
private List<String> hashedNormalizedEmails;
@SerializedName("phone_hash")
private List<String> hashedNormalizedPhones;

private final transient HashMap<String, String> hashedDiiToRawDii = new HashMap<>();
}
26 changes: 13 additions & 13 deletions src/main/java/com/uid2/client/IdentityMapResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

import java.util.HashMap;

public class IdentityMapResponse {
Expand All @@ -15,24 +17,25 @@ public class IdentityMapResponse {
throw new Uid2Exception("Got unexpected identity map status: " + status);
}

Gson gson = new Gson();
JsonObject body = getBodyAsJson(responseJson);

JsonArray mapped = body.get("mapped").getAsJsonArray();
for (JsonElement identity : mapped) {
JsonObject identityObject = identity.getAsJsonObject();
String identifier = getJsonString(identityObject, "identifier");
String rawDii = identityMapInput.getRawDii(identifier);
mappedIdentities.put(rawDii, new MappedIdentity(getJsonString(identityObject, "advertising_id"), getJsonString(identityObject, "bucket_id")));
String rawDii = getRawDii(identity, identityMapInput);
mappedIdentities.put(rawDii, gson.fromJson(identity, MappedIdentity.class));
}

JsonArray unmapped = body.get("unmapped").getAsJsonArray();
for (JsonElement identity : unmapped) {
JsonObject identityObject = identity.getAsJsonObject();
String identifier = getJsonString(identityObject, "identifier");
String rawDii = identityMapInput.getRawDii(identifier);
unmappedIdentities.put(rawDii, new UnmappedIdentity(getJsonString(identityObject, "reason")));
String rawDii = getRawDii(identity, identityMapInput);
unmappedIdentities.put(rawDii, gson.fromJson(identity, UnmappedIdentity.class));
}
}

private String getRawDii(JsonElement identity, IdentityMapInput identityMapInput) {
String identifier = identity.getAsJsonObject().get("identifier").getAsString();
return identityMapInput.getRawDii(identifier);
}

public boolean isSuccess() {
Expand All @@ -51,7 +54,9 @@ public MappedIdentity(String rawUid, String bucketId) {
public String getRawUid() {return rawUid;}
public String getBucketId() {return bucketId;}

@SerializedName("advertising_id")
private final String rawUid;
@SerializedName("bucket_id")
private final String bucketId;
}

Expand All @@ -65,11 +70,6 @@ public UnmappedIdentity(String reason) {
private final String reason;
}

static private String getJsonString(JsonObject json, String key) {
JsonElement keyElem = json.get(key);
return keyElem.getAsString();
}

public HashMap<String, MappedIdentity> getMappedIdentities() {
return mappedIdentities;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ public void identityMapEmails() {

@Test
public void identityMapInvalidEmail() {
IdentityMapInput identityMapInput = IdentityMapInput.fromEmails(Arrays.asList("[email protected]", "this is not an email"));
assertThrows(IllegalArgumentException.class,
() -> identityMapClient.generateIdentityMap(identityMapInput));
() -> IdentityMapInput.fromEmails(Arrays.asList("[email protected]", "this is not an email")));
}


@Test
public void identityMapInvalidPhone() {
IdentityMapInput identityMapInput = IdentityMapInput.fromPhones(Arrays.asList("+12345678901", "this is not a phone number"));
assertThrows(IllegalArgumentException.class,
() -> identityMapClient.generateIdentityMap(identityMapInput));
() -> IdentityMapInput.fromPhones(Arrays.asList("+12345678901", "this is not a phone number")));
}

@Test
Expand Down

0 comments on commit 1a90521

Please sign in to comment.