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

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

package com.chavaillaz.client.jenkins.okhttp;

import static com.chavaillaz.client.common.okhttp.OkHttpUtils.formData;

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

import com.chavaillaz.client.jenkins.JenkinsAuthentication;
import com.chavaillaz.client.jenkins.api.UserApi;
import com.chavaillaz.client.jenkins.domain.user.Token;
import com.chavaillaz.client.jenkins.domain.user.User;
import okhttp3.OkHttpClient;

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

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

    @Override
    public CompletableFuture getUser() {
        return sendAsync(requestBuilder(URL_USER, getAuthentication().getUsername()).get(), User.class);
    }

    @Override
    public CompletableFuture generateToken(String tokenName) {
        return sendAsync(requestBuilder(URL_USER_TOKEN_GENERATION, getAuthentication().getUsername())
                .header(HEADER_CONTENT_TYPE, HEADER_CONTENT_FORM)
                .post(formData(Map.of("newTokenName", tokenName))), Token.class);
    }

    @Override
    public CompletableFuture revokeToken(String tokenUuid) {
        return sendAsync(requestBuilder(URL_USER_TOKEN_REVOCATION, getAuthentication().getUsername())
                .header(HEADER_CONTENT_TYPE, HEADER_CONTENT_FORM)
                .post(formData(Map.of("tokenUuid", tokenUuid))), Void.class);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy