All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.makitoo.internal.HttpConfigurationProvider Maven / Gradle / Ivy

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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy