GitLabApi.java 16.27 KiB
package org.gitlab4j.api;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.Version;
/**
 * This class is provides a simplified interface to a GitLab API server, and divides the API up into
 * a separate API class for each concern.
public class GitLabApi {
    /** GitLab4J default per page.  GitLab will ignore anything over 100. */
    public static final int DEFAULT_PER_PAGE = 100;
    /** Specifies the version of the GitLab API to communicate with. */
    public enum ApiVersion {
        V3, V4;
        public String getApiNamespace() {
            return ("/api/" + name().toLowerCase());
    GitLabApiClient apiClient;
    private ApiVersion apiVersion;
    private int defaultPerPage = DEFAULT_PER_PAGE;
    private CommitsApi commitsApi;
    private DeployKeysApi deployKeysApi;
    private GroupApi groupApi;
    private MergeRequestApi mergeRequestApi;
    private NamespaceApi namespaceApi;
    private PipelineApi pipelineApi;
    private ProjectApi projectApi;
    private RepositoryApi repositoryApi;
    private RepositoryFileApi repositoryFileApi;
    private ServicesApi servicesApi;
    private SessionApi sessoinApi;
    private UserApi userApi;
    private JobApi jobApi;
    private NotesApi notesApi;
    private EventsApi eventsApi;
    private Session session;
    /**
     * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
     * using returned private token and the specified GitLab API version.
     * @param apiVersion the ApiVersion specifying which version of the API to use
     * @param url GitLab URL
     * @param username user name for which private token should be obtained
     * @param password password for a given {@code username}
     * @return new {@code GitLabApi} instance configured for a user-specific token
     * @throws GitLabApiException GitLabApiException if any exception occurs during execution
    public static GitLabApi login(ApiVersion apiVersion, String url, String username, String password) throws GitLabApiException {
        SessionApi sessionApi = new SessionApi(new GitLabApi(apiVersion, url, (String)null));
        Session session = sessionApi.login(username, null, password);
        return (new GitLabApi(apiVersion, url, session));
    /**
     * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
     * using returned private token using GitLab API version 4.
     * @param url GitLab URL
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* @param username user name for which private token should be obtained * @param password password for a given {@code username} * @return new {@code GitLabApi} instance configured for a user-specific token * @throws GitLabApiException GitLabApiException if any exception occurs during execution */ public static GitLabApi login(String url, String username, String password) throws GitLabApiException { return (login(ApiVersion.V4, url, username, password)); } /** * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance * using returned private token and specified GitLab API version. * * @param url GitLab URL * @param username user name for which private token should be obtained * @param password password for a given {@code username} * @return new {@code GitLabApi} instance configured for a user-specific token * @throws GitLabApiException GitLabApiException if any exception occurs during execution * @deprecated As of release 4.2.0, replaced by {@link #login(String, String, String)} */ @Deprecated public static GitLabApi create(String url, String username, String password) throws GitLabApiException { return (login(url, username, password)); } /** * If this instance was created with {@link #login(String, String, String)} this method will * return the Session instance returned by the GitLab API on login, otherwise returns null. * * @return the Session instance */ public Session getSession() { return session; } /** * Constructs a GitLabApi instance set up to interact with the GitLab server using the specified GitLab API version. * * @param apiVersion the ApiVersion specifying which version of the API to use * @param hostUrl the URL of the GitLab server * @param privateToken to private token to use for access to the API */ public GitLabApi(ApiVersion apiVersion, String hostUrl, String privateToken) { this(apiVersion, hostUrl, privateToken, null); } /** * Constructs a GitLabApi instance set up to interact with the GitLab server using GitLab API version 4. * * @param hostUrl the URL of the GitLab server * @param privateToken to private token to use for access to the API */ public GitLabApi(String hostUrl, String privateToken) { this(ApiVersion.V4, hostUrl, privateToken, null); } /** * Constructs a GitLabApi instance set up to interact with the GitLab server using the specified GitLab API version. * * @param apiVersion the ApiVersion specifying which version of the API to use * @param hostUrl the URL of the GitLab server * @param session the Session instance obtained by logining into the GitLab server */ public GitLabApi(ApiVersion apiVersion, String hostUrl, Session session) { this(apiVersion, hostUrl, session.getPrivateToken(), null); this.session = session; } /** * Constructs a GitLabApi instance set up to interact with the GitLab server using GitLab API version 4.