-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIdentityMapV3Response.java
More file actions
128 lines (100 loc) · 3.95 KB
/
IdentityMapV3Response.java
File metadata and controls
128 lines (100 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.uid2.client;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class IdentityMapV3Response {
IdentityMapV3Response(String response, IdentityMapV3Input identityMapInput) {
ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
status = apiResponse.status;
if (!isSuccess()) {
throw new Uid2Exception("Got unexpected identity map status: " + status);
}
populateIdentities(apiResponse.body, identityMapInput);
}
private void populateIdentities(Map<String, List<ApiIdentity>> apiResponse, IdentityMapV3Input identityMapInput) {
for (Map.Entry<String, List<ApiIdentity>> identitiesForType : apiResponse.entrySet()) {
populateIdentitiesForType(identityMapInput, identitiesForType.getKey(), identitiesForType.getValue());
}
}
private void populateIdentitiesForType(IdentityMapV3Input identityMapInput, String identityType, List<ApiIdentity> identities) {
for (int i = 0; i < identities.size(); i++) {
ApiIdentity apiIdentity = identities.get(i);
List<String> inputDiis = identityMapInput.getInputDiis(identityType, i);
for (String inputDii : inputDiis) {
if (apiIdentity.error == null) {
mappedIdentities.put(inputDii, new MappedIdentity(apiIdentity));
} else {
unmappedIdentities.put(inputDii, new UnmappedIdentity(apiIdentity.error));
}
}
}
}
public boolean isSuccess() {
return "success".equals(status);
}
public static class ApiResponse {
@SerializedName("status")
public String status;
@SerializedName("body")
public Map<String, List<ApiIdentity>> body;
}
public static class ApiIdentity {
@SerializedName("u")
public String currentUid;
@SerializedName("p")
public String previousUid;
@SerializedName("r")
public Long refreshFromSeconds;
@SerializedName("e")
public String error;
}
public static class MappedIdentity {
public MappedIdentity(String currentUid, String previousUid, Instant refreshFrom) {
this.currentUid = currentUid;
this.previousUid = previousUid;
this.refreshFrom = refreshFrom;
}
public MappedIdentity(ApiIdentity apiIdentity) {
this(apiIdentity.currentUid, apiIdentity.previousUid, Instant.ofEpochSecond(apiIdentity.refreshFromSeconds));
}
private final String currentUid;
private final String previousUid;
private final Instant refreshFrom;
public String getCurrentRawUid() {
return currentUid;
}
public String getPreviousRawUid() {
return previousUid;
}
public Instant getRefreshFrom() {
return refreshFrom;
}
}
public static class UnmappedIdentity {
public UnmappedIdentity(String reason)
{
this.reason = UnmappedIdentityReason.fromString(reason);
this.rawReason = reason;
}
public UnmappedIdentityReason getReason() {
return reason;
}
public String getRawReason() {
return rawReason;
}
private final UnmappedIdentityReason reason;
private final String rawReason;
}
public HashMap<String, MappedIdentity> getMappedIdentities() {
return new HashMap<>(mappedIdentities);
}
public HashMap<String, UnmappedIdentity> getUnmappedIdentities() {
return new HashMap<>(unmappedIdentities);
}
private final String status;
private final HashMap<String, MappedIdentity> mappedIdentities = new HashMap<>();
private final HashMap<String, UnmappedIdentity> unmappedIdentities = new HashMap<>();
}