-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIdentityMapV3Response.java
More file actions
127 lines (99 loc) · 3.9 KB
/
Copy pathIdentityMapV3Response.java
File metadata and controls
127 lines (99 loc) · 3.9 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
package com.uid2.client;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
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> rawDiis = identityMapInput.getRawDiis(identityType, i);
for (String rawDii : rawDiis) {
if (apiIdentity.error != null) {
unmappedIdentities.put(rawDii, new UnmappedIdentity(apiIdentity));
} else {
mappedIdentities.put(rawDii, new MappedIdentity(apiIdentity));
}
}
}
}
private static IdentityMapV3Input getIdentityMapInput(IdentityMapV3Input identityMapInput) {
return identityMapInput;
}
public boolean isSuccess() {
return "success".equals(status);
}
static public class ApiResponse {
@SerializedName("status")
public String status;
@SerializedName("body")
public Map<String, List<ApiIdentity>> body;
}
static public class ApiIdentity {
@SerializedName("u")
public String currentUid;
@SerializedName("p")
public String previousUid;
@SerializedName("r")
public Long refreshFromSeconds;
@SerializedName("e")
public String error;
}
static public class MappedIdentity {
public MappedIdentity(String currentUid, String previousUid, Long refreshFromSeconds) {
this.currentUid = currentUid;
this.previousUid = previousUid;
this.refreshFromSeconds = refreshFromSeconds;
}
public MappedIdentity(ApiIdentity apiIdentity) {
this(apiIdentity.currentUid, apiIdentity.previousUid, apiIdentity.refreshFromSeconds);
}
private final String currentUid;
private final String previousUid;
private final Long refreshFromSeconds;
public String getCurrentUid() {
return currentUid;
}
public String getPreviousUid() {
return previousUid;
}
public Long getRefreshFromSeconds() {
return refreshFromSeconds;
}
}
static public class UnmappedIdentity {
public UnmappedIdentity(String reason) {
this.reason = reason;
}
public UnmappedIdentity(ApiIdentity apiIdentity) {
this(apiIdentity.error);
}
private final String reason;
public String getReason() {
return reason;
}
}
public HashMap<String, MappedIdentity> getMappedIdentities() {
return mappedIdentities;
}
public HashMap<String, UnmappedIdentity> getUnmappedIdentities() {
return unmappedIdentities;
}
private final String status;
private final HashMap<String, MappedIdentity> mappedIdentities = new HashMap<>();
private final HashMap<String, UnmappedIdentity> unmappedIdentities = new HashMap<>();
}