AbstractApi.java 10.82 KiB
package org.gitlab4j.api;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.net.URL;
/**
 * This class is the base class for all the sub API classes. It provides implementations of
 * delete(), get(), post() and put() that are re-used by all the sub-classes.
public abstract class AbstractApi {
    private GitLabApi gitLabApi;
    public AbstractApi(GitLabApi gitLabApi) {
        this.gitLabApi = gitLabApi;
    protected GitLabApiClient getApiClient() {
        return (gitLabApi.getApiClient());
    /**
     * Perform an HTTP GET call with the specified query parameters and path objects, returning
     * a ClientResponse instance with the data returned from the endpoint.
     * @param expectedStatus the HTTP status that should be returned from the server
     * @param queryParams multivalue map of request parameters
     * @param pathArgs variable list of arguments used to build the URI
     * @return a ClientResponse instance with the data returned from the endpoint
     * @throws GitLabApiException if any exception occurs during execution
    protected Response get(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
        try {
            return validate(getApiClient().get(queryParams, pathArgs), expectedStatus);
        } catch (Exception e) {
            throw handle(e);
    /**
     * Perform an HTTP GET call with the specified query parameters and URL, returning
     * a ClientResponse instance with the data returned from the endpoint.
     * @param expectedStatus the HTTP status that should be returned from the server
     * @param queryParams multivalue map of request parameters
     * @param url the fully formed path to the GitLab API endpoint
     * @return a ClientResponse instance with the data returned from the endpoint
     * @throws GitLabApiException if any exception occurs during execution
    protected Response get(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
        try {
            return validate(getApiClient().get(queryParams, url), expectedStatus);
        } catch (Exception e) {
            throw handle(e);
    /**
     * Perform an HTTP POST call with the specified form data and path objects, returning
     * a ClientResponse instance with the data returned from the endpoint.
     * @param expectedStatus the HTTP status that should be returned from the server
     * @param formData the Form containing the name/value pairs for the POST data
     * @param pathArgs variable list of arguments used to build the URI
     * @return a ClientResponse instance with the data returned from the endpoint
     * @throws GitLabApiException if any exception occurs during execution
    protected Response post(Response.Status expectedStatus, Form formData, Object... pathArgs) throws GitLabApiException {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
try { return validate(getApiClient().post(formData, pathArgs), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Perform an HTTP POST call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. * * @param expectedStatus the HTTP status that should be returned from the server * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a ClientResponse instance with the data returned from the endpoint * @throws GitLabApiException if any exception occurs during execution */ protected Response post(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException { try { return validate(getApiClient().post(queryParams, pathArgs), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Perform an HTTP POST call with the specified form data and URL, returning * a ClientResponse instance with the data returned from the endpoint. * * @param expectedStatus the HTTP status that should be returned from the server * @param formData the Form containing the name/value pairs for the POST data * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws GitLabApiException if any exception occurs during execution */ protected Response post(Response.Status expectedStatus, Form formData, URL url) throws GitLabApiException { try { return validate(getApiClient().post(formData, url), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Perform an HTTP PUT call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. * * @param expectedStatus the HTTP status that should be returned from the server * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a ClientResponse instance with the data returned from the endpoint * @throws GitLabApiException if any exception occurs during execution */ protected Response put(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException { try { return validate(getApiClient().put(queryParams, pathArgs), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Perform an HTTP PUT call with the specified form data and URL, returning * a ClientResponse instance with the data returned from the endpoint. * * @param expectedStatus the HTTP status that should be returned from the server * @param queryParams multivalue map of request parameters * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws GitLabApiException if any exception occurs during execution
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
*/ protected Response put(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException { try { return validate(getApiClient().put(queryParams, url), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Perform an HTTP DELETE call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. * * @param expectedStatus the HTTP status that should be returned from the server * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a ClientResponse instance with the data returned from the endpoint * @throws GitLabApiException if any exception occurs during execution */ protected Response delete(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException { try { return validate(getApiClient().delete(queryParams, pathArgs), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Perform an HTTP DELETE call with the specified form data and URL, returning * a ClientResponse instance with the data returned from the endpoint. * * @param expectedStatus the HTTP status that should be returned from the server * @param queryParams multivalue map of request parameters * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws GitLabApiException if any exception occurs during execution */ protected Response delete(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException { try { return validate(getApiClient().delete(queryParams, url), expectedStatus); } catch (Exception e) { throw handle(e); } } /** * Convenience method for adding query and form parameters to a get() or post() call. * * @param formData the Form containing the name/value pairs * @param name the name of the field/attribute to add * @param value the value of the field/attribute to add */ protected void addFormParam(Form formData, String name, Object value) throws IllegalArgumentException { addFormParam(formData, name, value, false); } /** * Convenience method for adding query and form parameters to a get() or post() call. * If required is true and value is null, will throw an IllegalArgumentException. * * @param formData the Form containing the name/value pairs * @param name the name of the field/attribute to add * @param value the value of the field/attribute to add * @param required the field is required flag * @throws IllegalArgumentException if a required parameter is null or empty */ protected void addFormParam(Form formData, String name, Object value, boolean required) throws IllegalArgumentException { if (value == null) {
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
if (required) { throw new IllegalArgumentException(name + " cannot be empty or null"); } return; } String stringValue = value.toString().trim(); if (required && stringValue.length() == 0) { throw new IllegalArgumentException(name + " cannot be empty or null"); } formData.param(name, stringValue); } /** * Validates response. * * @param response response * @param expected expected respone status * @return original response if the response status is expected * @throws GitLabApiException if HTTP status is not as expected */ protected Response validate(Response response, Response.Status expected) throws GitLabApiException { if (response.getStatus() != expected.getStatusCode()) { throw new GitLabApiException(response); } return (response); } /** * Wraps exception if needed * * @param thrown the exception that should be wrapped * @throws GitLabApiException containing the cause or GitLab API specific message */ protected GitLabApiException handle(Exception thrown) throws GitLabApiException { if (thrown instanceof GitLabApiException) { throw (GitLabApiException) thrown; } throw new GitLabApiException(thrown); } }