|
| 1 | +package com.uid2.client; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import com.google.gson.annotations.SerializedName; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class IdentityMapV3Response { |
| 13 | + IdentityMapV3Response(String response, IdentityMapV3Input identityMapInput) { |
| 14 | + JsonObject responseJson = new Gson().fromJson(response, JsonObject.class); |
| 15 | + status = responseJson.get("status").getAsString(); |
| 16 | + |
| 17 | + if (!isSuccess()) { |
| 18 | + throw new Uid2Exception("Got unexpected identity map status: " + status); |
| 19 | + } |
| 20 | + |
| 21 | + Gson gson = new Gson(); |
| 22 | + JsonObject body = getBodyAsJson(responseJson); |
| 23 | + |
| 24 | + Iterable<JsonElement> mapped = getJsonArray(body, "mapped"); |
| 25 | + for (JsonElement identity : mapped) { |
| 26 | + List<String> rawDiis = getRawDiis(identity, identityMapInput); |
| 27 | + MappedIdentity mappedIdentity = gson.fromJson(identity, MappedIdentity.class); |
| 28 | + for (String rawDii : rawDiis) { |
| 29 | + mappedIdentities.put(rawDii, mappedIdentity); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + Iterable<JsonElement> unmapped = getJsonArray(body, "unmapped"); |
| 34 | + for (JsonElement identity : unmapped) { |
| 35 | + List<String> rawDiis = getRawDiis(identity, identityMapInput); |
| 36 | + UnmappedIdentity unmappedIdentity = gson.fromJson(identity, UnmappedIdentity.class); |
| 37 | + for (String rawDii : rawDiis) { |
| 38 | + unmappedIdentities.put(rawDii, unmappedIdentity); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static Iterable<JsonElement> getJsonArray(JsonObject body, String header) { |
| 44 | + JsonElement jsonElement = body.get(header); |
| 45 | + if (jsonElement == null) { |
| 46 | + return Collections.emptyList(); |
| 47 | + } |
| 48 | + return jsonElement.getAsJsonArray(); |
| 49 | + } |
| 50 | + |
| 51 | + private List<String> getRawDiis(JsonElement identity, IdentityMapV3Input identityMapInput) { |
| 52 | + String identifier = identity.getAsJsonObject().get("identifier").getAsString(); |
| 53 | + return identityMapInput.getRawDiis(identifier); |
| 54 | + } |
| 55 | + |
| 56 | + public boolean isSuccess() { |
| 57 | + return "success".equals(status); |
| 58 | + } |
| 59 | + static JsonObject getBodyAsJson(JsonObject jsonResponse) { |
| 60 | + return jsonResponse.get("body").getAsJsonObject(); |
| 61 | + } |
| 62 | + |
| 63 | + static public class MappedIdentity { |
| 64 | + public MappedIdentity(String rawUid, String bucketId) { |
| 65 | + this.rawUid = rawUid; |
| 66 | + this.bucketId = bucketId; |
| 67 | + } |
| 68 | + |
| 69 | + public String getRawUid() {return rawUid;} |
| 70 | + public String getBucketId() {return bucketId;} |
| 71 | + |
| 72 | + @SerializedName("advertising_id") |
| 73 | + private final String rawUid; |
| 74 | + @SerializedName("bucket_id") |
| 75 | + private final String bucketId; |
| 76 | + } |
| 77 | + |
| 78 | + static public class UnmappedIdentity { |
| 79 | + public UnmappedIdentity(String reason) { |
| 80 | + this.reason = reason; |
| 81 | + } |
| 82 | + |
| 83 | + public String getReason() {return reason;} |
| 84 | + |
| 85 | + private final String reason; |
| 86 | + } |
| 87 | + |
| 88 | + public HashMap<String, MappedIdentity> getMappedIdentities() { |
| 89 | + return mappedIdentities; |
| 90 | + } |
| 91 | + |
| 92 | + public HashMap<String, UnmappedIdentity> getUnmappedIdentities() { |
| 93 | + return unmappedIdentities; |
| 94 | + } |
| 95 | + |
| 96 | + private final String status; |
| 97 | + private final HashMap<String, MappedIdentity> mappedIdentities = new HashMap<>(); |
| 98 | + private final HashMap<String, UnmappedIdentity> unmappedIdentities = new HashMap<>(); |
| 99 | +} |
0 commit comments