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

com.chavaillaz.client.jenkins.okhttp.OkHttpJobApi Maven / Gradle / Ivy

package com.chavaillaz.client.jenkins.okhttp;

import static com.chavaillaz.client.common.okhttp.OkHttpUtils.formData;
import static com.chavaillaz.client.common.utility.Utils.encodeQuery;
import static com.chavaillaz.client.jenkins.JenkinsConstant.FOLDER_MODE;
import static com.chavaillaz.client.jenkins.JenkinsConstant.LIST_VIEW;
import static okhttp3.RequestBody.create;

import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.common.utility.Utils;
import com.chavaillaz.client.jenkins.JenkinsAuthentication;
import com.chavaillaz.client.jenkins.api.JobApi;
import com.chavaillaz.client.jenkins.domain.folder.Folder;
import com.chavaillaz.client.jenkins.domain.folder.Path;
import com.chavaillaz.client.jenkins.domain.job.JobInfo;
import com.chavaillaz.client.jenkins.domain.job.build.BuildInfo;
import com.chavaillaz.client.jenkins.domain.job.test.CoverageReport;
import com.chavaillaz.client.jenkins.domain.job.test.TestReport;
import com.chavaillaz.client.jenkins.domain.view.ViewCreation;
import com.chavaillaz.client.jenkins.domain.view.ViewInfo;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * Implementation of {@link JobApi} for OkHttp.
 */
public class OkHttpJobApi extends AbstractOkHttpClient implements JobApi {

    /**
     * Creates a new job client based on OkHttp client.
     *
     * @param client         The OkHttp client to use
     * @param baseUrl        The URL of Jenkins
     * @param authentication The authentication method
     */
    public OkHttpJobApi(OkHttpClient client, String baseUrl, JenkinsAuthentication authentication) {
        super(client, baseUrl, authentication);
    }

    @Override
    public CompletableFuture getFolder(Path path) {
        return sendAsync(requestBuilder(URL_FOLDER, path).get(), Folder.class);
    }

    @Override
    public CompletableFuture getView(Path path, String viewName) {
        return sendAsync(requestBuilder(URL_VIEW_INFO, path, viewName).get(), ViewInfo.class);
    }

    @Override
    public CompletableFuture getJob(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_INFO, path, jobName).get(), JobInfo.class);
    }

    @Override
    public CompletableFuture getFolderConfiguration(Path path) {
        return sendAsync(requestBuilder(URL_FOLDER_CONFIGURATION, path).get())
                .thenApply(Utils::readInputStream);
    }

    @Override
    public CompletableFuture getJobConfiguration(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_CONFIGURATION, path, jobName).get())
                .thenApply(Utils::readInputStream);
    }

    @Override
    public CompletableFuture getViewConfiguration(Path path, String viewName) {
        return sendAsync(requestBuilder(URL_VIEW_CONFIGURATION, path, viewName).get())
                .thenApply(Utils::readInputStream);
    }

    @Override
    public CompletableFuture getJobDescription(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_DESCRIPTION, path, jobName).get())
                .thenApply(Utils::readInputStream);
    }

    @Override
    public CompletableFuture getLastBuildNumber(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_LAST_BUILD_NUMBER, path, jobName).get())
                .thenApply(Utils::readInputStream)
                .thenApply(NumberUtils::toInt)
                .exceptionally(exception -> null);
    }

    @Override
    public CompletableFuture getBuild(Path path, String jobName, int buildNumber) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_INFO, path, jobName, buildNumber).get(), BuildInfo.class);
    }

    @Override
    public CompletableFuture getArtifact(Path path, String jobName, int buildNumber, String artifactPath) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_ARTIFACT, path, jobName, buildNumber, artifactPath).get());
    }

    @Override
    public CompletableFuture getTestReport(Path path, String jobName, int buildNumber) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_TESTS, path, jobName, buildNumber).get(), TestReport.class)
                .exceptionally(exception -> null);
    }

    @Override
    public CompletableFuture getTestCoverageReport(Path path, String jobName, int buildNumber) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_COVERAGE, path, jobName, buildNumber).get(), CoverageReport.class)
                .exceptionally(exception -> null);
    }

    @Override
    public CompletableFuture getConsoleOutput(Path path, String jobName, int bufferOffset) {
        return sendAsync(requestBuilder(URL_JOB_LAST_BUILD_CONSOLE, path, jobName, bufferOffset).get())
                .thenApply(Utils::readInputStream);
    }

    @Override
    public CompletableFuture getConsoleOutput(Path path, String jobName, int buildNumber, int bufferOffset) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_CONSOLE, path, jobName, buildNumber, bufferOffset).get())
                .thenApply(Utils::readInputStream);
    }

    @Override
    public CompletableFuture createFolder(Path path, String folderName) {
        return sendAsync(requestBuilder(URL_FOLDER_CREATE, path, folderName, FOLDER_MODE).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture createJob(Path path, String jobName, String configXML) {
        return sendAsync(requestBuilder(URL_JOB_CREATE, path, jobName).post(create(configXML, MEDIA_TYPE_XML)), Void.class);
    }

    @Override
    public CompletableFuture createView(Path path, String viewName) {
        return sendAsync(requestBuilder(URL_VIEW_CREATE, path)
                .header(HEADER_CONTENT_TYPE, HEADER_CONTENT_FORM)
                .post(formData(Map.of(
                        "name", viewName,
                        "mode", LIST_VIEW,
                        "json", serialize(new ViewCreation(viewName, LIST_VIEW))
                ))), Void.class);
    }

    @Override
    public CompletableFuture updateJobConfiguration(Path path, String jobName, String configXML) {
        return sendAsync(requestBuilder(URL_JOB_CONFIGURATION, path, jobName).post(create(configXML, MEDIA_TYPE_XML)), Void.class);
    }

    @Override
    public CompletableFuture updateViewConfiguration(Path path, String viewName, String configXML) {
        return sendAsync(requestBuilder(URL_VIEW_CONFIGURATION, path, viewName).post(create(configXML, MEDIA_TYPE_XML)), Void.class);
    }

    @Override
    public CompletableFuture renameFolder(Path path, String folderName, String newName) {
        return sendAsync(requestBuilder(URL_FOLDER_RENAME, path, folderName, newName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture renameJob(Path path, String jobName, String newName) {
        return sendAsync(requestBuilder(URL_JOB_RENAME, path, jobName, newName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture enableJob(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_ENABLE, path, jobName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture disableJob(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_DISABLE, path, jobName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture deleteFolder(Path path, String folderName) {
        return sendAsync(requestBuilder(URL_FOLDER_DELETE, path, folderName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture deleteJob(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_DELETE, path, jobName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture deleteView(Path path, String viewName) {
        return sendAsync(requestBuilder(URL_VIEW_DELETE, path, viewName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture buildJob(Path path, String jobName) {
        return sendAsync(requestBuilder(URL_JOB_BUILD, path, jobName).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture buildJob(Path path, String jobName, Map properties) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_PARAMETERS, path, jobName, encodeQuery(properties)).post(EMPTY_BODY), Void.class);
    }

    @Override
    public CompletableFuture stopBuild(Path path, String jobName, int buildNumber) {
        return sendAsync(requestBuilder(URL_JOB_BUILD_STOP, path, jobName, buildNumber).post(EMPTY_BODY), Void.class);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy