io.split.api.resources.IdentityClient Maven / Gradle / Ivy
package io.split.api.resources;
import io.split.api.client.HttpClient;
import io.split.api.client.exceptions.SplitException;
import io.split.api.client.utils.EncodingUtil;
import io.split.api.dtos.Identity;
import io.split.api.dtos.result.ResultDTO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class IdentityClient {
private HttpClient _client;
public IdentityClient(HttpClient client) {
this._client = client;
}
public Identity save(Identity identity) throws SplitException {
String result = _client.put(
identity,
"/v1/trafficTypes/%s/environments/%s/identities/%s",
identity.trafficTypeId(),
identity.environmentId(),
identity.key()
);
return EncodingUtil.parse(result, Identity.class);
}
public ResultDTO save(Collection identities) throws SplitException {
// Group by Traffic Type & Environment
Map> groups = new HashMap<>();
for (Identity identity : identities) {
String key = String.format("%s:%s", identity.trafficTypeId(), identity.environmentId());
if (!groups.containsKey(key)) {
groups.put(key, new ArrayList());
}
groups.get(key).add(identity);
}
// Save each group
ResultDTO result = new ResultDTO<>();
for (Map.Entry> entry : groups.entrySet()) {
if (entry.getValue().size() == 0) {
continue;
}
String trafficTypeId = entry.getValue().get(0).trafficTypeId();
String environmentId = entry.getValue().get(0).environmentId();
ResultDTO groupResult = save(trafficTypeId, environmentId, entry.getValue());
result.objects().addAll(groupResult.objects());
result.failed().addAll(groupResult.failed());
}
return result;
}
public ResultDTO save(String trafficTypeId, String environmentId, List identities) throws SplitException {
String result = _client.post(
identities,
"/v1/trafficTypes/%s/environments/%s/identities",
trafficTypeId,
environmentId
);
return EncodingUtil.parseResult(result, Identity.class);
}
public Identity update(Identity identity) throws SplitException {
String result = _client.post(
identity,
"/v1/trafficTypes/%s/environments/%s/identities/%s/patch",
identity.trafficTypeId(),
identity.environmentId(),
identity.key()
);
return EncodingUtil.parse(result, Identity.class);
}
public boolean delete(Identity identity) throws SplitException {
return delete(identity.trafficTypeId(), identity.environmentId(), identity.key());
}
public boolean delete(String trafficTypeId, String environmentId, String key) throws SplitException {
String result = _client.delete(
"/v1/trafficTypes/%s/environments/%s/identities/%s",
trafficTypeId,
environmentId,
key
);
return EncodingUtil.parse(result, Boolean.class);
}
}