com.makitoo.internal.HttpConfigurationProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feature-flag Show documentation
Show all versions of feature-flag Show documentation
The Java client for Makitoo feature handling.
The newest version!
package com.makitoo.internal;
import com.makitoo.User;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by nicolas on 23/01/17.
*/
public class HttpConfigurationProvider implements ConfigurationProvider {
private static final Logger LOGGER = Logger.getLogger(HttpConfigurationProvider.class.getName());
private final EventSender httpClient;
private final EventBuilder eventBuilder;
public HttpConfigurationProvider(EventBuilder eventBuilder, EventSender httpClient) {
this.httpClient = httpClient;
this.eventBuilder = eventBuilder;
}
private UserConfiguration parseConfiguration(User user, JSONObject object) {
UserConfiguration config = new UserConfiguration(user);
JSONArray features = object.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
config.withConfiguration(features.getJSONObject(i).getString("name"), "ON".equals(features.getJSONObject(i).getString("state")));
}
return config;
}
public UserConfiguration getUserConfiguration(User user) throws IOException {
JSONTokener tokener = new JSONTokener(httpClient.sendEvent(eventBuilder.createRefreshEvent(user)));
JSONObject root = new JSONObject(tokener);
return parseConfiguration(user, root);
}
}