package org.jboss.da.communcation.pnc;

import org.jboss.da.common.json.DAConfig;
import org.jboss.da.common.util.Configuration;
import org.jboss.da.common.util.ConfigurationParseException;
import org.jboss.da.communcation.pnc.authentication.PNCAuthentication;
import org.jboss.da.communcation.pnc.model.BuildConfiguration;
import org.jboss.da.communcation.pnc.model.BuildConfigurationSet;
import org.jboss.da.communcation.pnc.model.Product;
import org.jboss.da.communcation.pnc.model.Project;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;

import javax.ws.rs.core.MediaType;
import java.util.Arrays;
import java.util.List;

public class PNC {

    private Configuration config;
    private DAConfig conf;
    private String pncServer;
    private PNCAuthentication pncAuthenticate;

    public PNC() throws ConfigurationParseException {
        Configuration config = new Configuration();
        DAConfig conf = config.getConfig();
        pncServer = config.getConfig().getPncServer();

        PNCAuthentication pncAuthenticate = new PNCAuthentication();
    }

    public ClientRequest getClient(String endpoint, boolean authenticate) {
        ClientRequest request = new ClientRequest(pncServer + "/pnc-rest/rest/" + endpoint);
        request.accept(MediaType.APPLICATION_JSON);

        // TODO: instead of getting a new token everytime, check if existing
        // TODO: token is expired before asking for a new one
        if (authenticate) {
            String token = pncAuthenticate.authenticate();
            request.header("Authorization", "Bearer " + token);
        }

        return request;
    }

    public ClientRequest getClient(String endpoint) {
        return getClient(endpoint, false);
    }

    public List<BuildConfiguration> getBuildConfigurations() throws Exception {
        ClientResponse<BuildConfiguration[]> response = getClient("build-configurations").get(BuildConfiguration[].class);
        return Arrays.asList(response.getEntity());
    }

    public List<BuildConfigurationSet> getBuildConfigurationSets() throws Exception {
        ClientResponse<BuildConfigurationSet[]> response = getClient("build-configuration-sets").get(BuildConfigurationSet[].class);
        return Arrays.asList(response.getEntity());
    }

    public List<Product> getProducts() throws Exception {
        ClientResponse<Product[]> response = getClient("products").get(Product[].class);
        return Arrays.asList(response.getEntity());
    }

    public List<Project> getProjects() throws Exception {
        ClientResponse<Project[]> response = getClient("projects").get(Project[].class);
        return Arrays.asList(response.getEntity());
    }
}

@@ -37,50 +37,8 @@
 c;%0A%0A
-import org.jboss.da.common.json.DAConfig;%0A
 impo
@@ -685,35 +685,8 @@
 ig;%0A
-    private DAConfig conf;%0A
     
@@ -711,16 +711,16 @@
 Server;%0A
+
     priv
@@ -821,30 +821,16 @@
         
-Configuration 
 config =
@@ -855,52 +855,8 @@
 ();%0A
-        DAConfig conf = config.getConfig();%0A
     
@@ -906,25 +906,24 @@
 rver();%0A
-%0A
         
 PNCAuthe
@@ -914,34 +914,16 @@
         
-PNCAuthentication 
 pncAuthe

Fix bad constructor in PNC class, null values assigned to the fields