/*
 * This file is part of Dependency-Track.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (c) Steve Springett. All Rights Reserved.
 */
package org.dependencytrack.integration;

import org.apache.commons.io.FileUtils;
import org.datanucleus.util.Base64;
import org.dependencytrack.common.UnirestFactory;
import org.json.JSONObject;
import unirest.HttpResponse;
import unirest.JsonNode;
import unirest.UnirestException;
import unirest.UnirestInstance;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class ApiClient {

    private String baseUrl;
    private String apiKey;

    public ApiClient(String baseUrl, String apiKey) {
        this.baseUrl = baseUrl;
        this.apiKey = apiKey;
    }

    public UUID createProject(String name, String version) throws UnirestException {
        final UnirestInstance ui = UnirestFactory.getUnirestInstance();
        final HttpResponse<JsonNode> response = ui.put(baseUrl + "/api/v1/project")
                .header("Content-Type", "application/json")
                .header("X-API-Key", apiKey)
                .body(new JSONObject()
                        .put("name", name)
                        .put("version", version)
                )
                .asJson();
        if (response.getStatus() == 201) {
            return UUID.fromString(response.getBody().getObject().getString("uuid"));
        }
        System.out.println("Error creating project " + name + " status: " + response.getStatus());
        return null;
    }

    public boolean uploadBom(UUID uuid, File bom) throws IOException, UnirestException {
        final UnirestInstance ui = UnirestFactory.getUnirestInstance();
        final HttpResponse<JsonNode> response = ui.put(baseUrl + "/api/v1/bom")
                .header("Content-Type", "application/json")
                .header("X-API-Key", apiKey)
                .body(new JSONObject()
                        .put("project", uuid.toString())
                        .put("bom", Base64.encode(FileUtils.readFileToByteArray(bom)))
                )
                .asJson();
        return (response.getStatus() == 200);
    }

    public boolean uploadScan(UUID uuid, File scan) throws IOException, UnirestException {
        final UnirestInstance ui = UnirestFactory.getUnirestInstance();
        final HttpResponse<JsonNode> response = ui.put(baseUrl + "/api/v1/scan")
                .header("Content-Type", "application/json")
                .header("X-API-Key", apiKey)
                .body(new JSONObject()
                        .put("project", uuid.toString())
                        .put("scan", Base64.encode(FileUtils.readFileToByteArray(scan)))
                )
                .asJson();
        return (response.getStatus() == 200);
    }

}

@@ -791,73 +791,74 @@
 org.
-dependencytrack.common.UnirestFactory;%0Aimport org.json.JSONObject
+json.JSONObject;%0Aimport org.dependencytrack.util.HttpClientFactory
 ;%0Aim
@@ -909,16 +909,40 @@
 onNode;%0A
+import unirest.Unirest;%0A
 import u
@@ -970,40 +970,8 @@
 on;%0A
-import unirest.UnirestInstance;%0A
 impo
@@ -1065,16 +1065,107 @@
 ient %7B%0A%0A
+    static %7B%0A        Unirest.config().httpClient(HttpClientFactory.createClient());%0A    %7D%0A%0A
     priv
@@ -1424,80 +1424,8 @@
 n %7B%0A
-        final UnirestInstance ui = UnirestFactory.getUnirestInstance();%0A
     
@@ -1460,34 +1460,39 @@
 ode%3E response = 
-ui
+Unirest
 .put(baseUrl + %22
@@ -2149,80 +2149,8 @@
 n %7B%0A
-        final UnirestInstance ui = UnirestFactory.getUnirestInstance();%0A
     
@@ -2185,34 +2185,39 @@
 ode%3E response = 
-ui
+Unirest
 .put(baseUrl + %22
@@ -2711,80 +2711,8 @@
 n %7B%0A
-        final UnirestInstance ui = UnirestFactory.getUnirestInstance();%0A
     
@@ -2755,18 +2755,23 @@
 ponse = 
-ui
+Unirest
 .put(bas

Revert "#266 Changes OSSIndex to OSS Index"