Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ The API has been broken up into sub APIs classes to make it easier to learn and
&nbsp;&nbsp;[DeployKeysApi](#deploykeysapi)<br/>
&nbsp;&nbsp;[EventsApi](#eventsapi)<br/>
&nbsp;&nbsp;[GroupApi](#groupapi)<br/>
&nbsp;&nbsp;[HealthCheckApi](#healthcheckapi)<br/>
&nbsp;&nbsp;[IssuesApi](#issuesapi)<br/>
&nbsp;&nbsp;[JobApi](#jobapi)<br/>
&nbsp;&nbsp;[LabelsApi](#labelsapi)<br/>
Expand Down Expand Up @@ -191,6 +192,13 @@ List<Event> events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, n
List<Group> groups = gitLabApi.getGroupApi().getGroups();
```

#### HealthCheckApi
```java
// Get the liveness endpoint health check results.
// Assumes ip_whitelisted
LivenessHealthCheck healthCheck = gitLabApi.getHealthCheckApi().getLiveness();
Copy link
Collaborator

@gmessner gmessner Apr 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LivenessHealthCheck should be HealthCheckInfo.

Nevermind, since I will be editing this file to bump the version when I release, I can make the edit then.

```

#### IssuesApi
```java
// Get a list of issues for the specified project ID
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/gitlab4j/api/GitLabApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public String getApiNamespace() {
private CommitsApi commitsApi;
private DeployKeysApi deployKeysApi;
private GroupApi groupApi;
private HealthCheckApi healthCheckApi;
private IssuesApi issuesApi;
private MergeRequestApi mergeRequestApi;
private MilestonesApi milestonesApi;
Expand Down Expand Up @@ -878,6 +879,25 @@ public GroupApi getGroupApi() {
return (groupApi);
}

/**
* Gets the HealthCheckApi instance owned by this GitLabApi instance. The HealthCheckApi is used
* to perform all admin level gitlab health monitoring.
*
* @return the HealthCheckApi instance owned by this GitLabApi instance
*/
public HealthCheckApi getHealthCheckApi() {

if (healthCheckApi == null) {
synchronized (this) {
if (healthCheckApi == null) {
healthCheckApi = new HealthCheckApi(this);
}
}
}

return (healthCheckApi);
}

/**
* Gets the IssuesApi instance owned by this GitLabApi instance. The IssuesApi is used
* to perform all iossue related API calls.
Expand Down
68 changes: 42 additions & 26 deletions src/main/java/org/gitlab4j/api/GitLabApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class GitLabApiClient {

private ClientConfig clientConfig;
private Client apiClient;
private String baseUrl;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a field to keep track of the original url.

private String hostUrl;
private TokenType tokenType = TokenType.PRIVATE;
private String authToken;
Expand Down Expand Up @@ -91,7 +92,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
/**
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
* server URL, private token, and secret token.
*
*
* @param hostUrl the URL to the GitLab API server
* @param privateToken the private token to authenticate with
*/
Expand All @@ -102,7 +103,7 @@ public GitLabApiClient(String hostUrl, String privateToken) {
/**
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
* server URL, private token, and secret token.
*
*
* @param hostUrl the URL to the GitLab API server
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
* @param authToken the token to authenticate with
Expand Down Expand Up @@ -141,7 +142,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
/**
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
* server URL, private token, and secret token.
*
*
* @param hostUrl the URL to the GitLab API server
* @param privateToken the private token to authenticate with
* @param secretToken use this token to validate received payloads
Expand All @@ -153,7 +154,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken)
/**
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
* server URL, private token, and secret token.
*
*
* @param hostUrl the URL to the GitLab API server
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
* @param authToken the token to authenticate with
Expand All @@ -166,7 +167,7 @@ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, St
/**
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
* server URL and private token.
*
*
* @param hostUrl the URL to the GitLab API server
* @param privateToken the private token to authenticate with
* @param secretToken use this token to validate received payloads
Expand All @@ -177,7 +178,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken,
}

/**
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
* server URL and private token.
*
* @param apiVersion the ApiVersion specifying which version of the API to use
Expand All @@ -191,7 +192,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToke
}

/**
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
* server URL and private token.
*
* @param apiVersion the ApiVersion specifying which version of the API to use
Expand All @@ -205,6 +206,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp

// Remove the trailing "/" from the hostUrl if present
this.hostUrl = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl);
this.baseUrl = this.hostUrl;
if (ApiVersion.OAUTH2_CLIENT != apiVersion) {
this.hostUrl += apiVersion.getApiNamespace();
}
Expand Down Expand Up @@ -265,7 +267,6 @@ TokenType getTokenType() {
/**
* Set the ID of the user to sudo as.
*
* @param sudoAsId the ID of the user to sudo as
*/
Integer getSudoAsId() {
return (sudoAsId);
Expand All @@ -282,29 +283,44 @@ void setSudoAsId(Integer sudoAsId) {

/**
* Construct a REST URL with the specified path arguments.
*
*
* @param pathArgs variable list of arguments used to build the URI
* @return a REST URL with the specified path arguments
* @throws IOException if an error occurs while constructing the URL
*/
protected URL getApiUrl(Object... pathArgs) throws IOException {
String url = appendPathArgs(this.hostUrl, pathArgs);
return (new URL(url));
}

/**
* Construct a REST URL with the specified path arguments using
* Gitlab base url.
*
* @param pathArgs variable list of arguments used to build the URI
* @return a REST URL with the specified path arguments
* @throws IOException if an error occurs while constructing the URL
*/
protected URL getUrlWithBase(Object... pathArgs) throws IOException {
String url = appendPathArgs(this.baseUrl, pathArgs);
return (new URL(url));
}

StringBuilder url = new StringBuilder();
url.append(hostUrl);
private String appendPathArgs(String url, Object... pathArgs) {
StringBuilder urlBuilder = new StringBuilder(url);
for (Object pathArg : pathArgs) {
if (pathArg != null) {
url.append("/");
url.append(pathArg.toString());
urlBuilder.append("/");
urlBuilder.append(pathArg.toString());
}
}

return (new URL(url.toString()));
return urlBuilder.toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factored out url building logic so that it can be applied with the baseUrl as well.

}

/**
* Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid,
* otherwise returns false.
*
*
* @param response the Response instance sent from the GitLab server
* @return true if the response's secret token is valid, otherwise returns false
*/
Expand All @@ -323,7 +339,7 @@ protected boolean validateSecretToken(Response response) {
/**
* 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 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
Expand All @@ -337,7 +353,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, Object... pat
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @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
Expand All @@ -349,7 +365,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, URL url) {
/**
* 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 queryParams multivalue map of request parameters
* @param accepts if non-empty will set the Accepts header to this value
* @param pathArgs variable list of arguments used to build the URI
Expand All @@ -364,7 +380,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, St
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @param accepts if non-empty will set the Accepts header to this value
Expand All @@ -377,7 +393,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, UR
/**
* 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 formData the Form containing the name/value pairs
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
Expand All @@ -391,7 +407,7 @@ protected Response post(Form formData, Object... pathArgs) throws IOException {
/**
* 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 queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a Response instance with the data returned from the endpoint
Expand All @@ -405,7 +421,7 @@ protected Response post(MultivaluedMap<String, String> queryParams, Object... pa
/**
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param formData the Form containing the name/value pairs
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
Expand Down Expand Up @@ -501,7 +517,7 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
/**
* 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 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
Expand Down Expand Up @@ -561,7 +577,7 @@ protected Response put(Form formData, URL url) {
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a Response instance with the data returned from the endpoint.
*
*
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a Response instance with the data returned from the endpoint
Expand All @@ -574,7 +590,7 @@ protected Response delete(MultivaluedMap<String, String> queryParams, Object...
/**
* Perform an HTTP DELETE call with the specified form data and URL, returning
* a Response instance with the data returned from the endpoint.
*
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @return a Response instance with the data returned from the endpoint
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/org/gitlab4j/api/HealthCheckApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.gitlab4j.api;

import org.gitlab4j.api.models.HealthCheckInfo;

import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URL;

public class HealthCheckApi extends AbstractApi {
public HealthCheckApi(GitLabApi gitLabApi) {
super(gitLabApi);
}

/**
* Get Health Checks from the liveness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
*
* GET /-/liveness
*
* @return HealthCheckInfo instance
* @throws GitLabApiException if any exception occurs
*/
public HealthCheckInfo getLiveness() throws GitLabApiException, IOException {
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
Response response = get(Response.Status.OK, null, livenessUrl);
return (response.readEntity(HealthCheckInfo.class));
}

/**
* Get Health Checks from the liveness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
*
* GET /-/liveness
*
* @param token Health Status token
* @return HealthCheckInfo instance
* @throws GitLabApiException if any exception occurs
* @deprecated
*/
public HealthCheckInfo getLiveness(String token) throws GitLabApiException, IOException {
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
GitLabApiForm formData = new GitLabApiForm()
.withParam("token", token, false);
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
return (response.readEntity(HealthCheckInfo.class));
}

/**
* Get Health Checks from the readiness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
*
* GET /-/readiness
*
* @return HealthCheckInfo instance
* @throws GitLabApiException if any exception occurs
*/
public HealthCheckInfo getReadiness() throws GitLabApiException, IOException {
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
Response response = get(Response.Status.OK, null, readinessUrl);
return (response.readEntity(HealthCheckInfo.class));
}

/**
* Get Health Checks from the readiness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
*
* GET /-/readiness
*
* @param token Health Status token
* @return HealthCheckInfo instance
* @throws GitLabApiException if any exception occurs
* @deprecated
*/
public HealthCheckInfo getReadiness(String token) throws GitLabApiException, IOException {
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
GitLabApiForm formData = new GitLabApiForm()
.withParam("token", token, false);
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
return (response.readEntity(HealthCheckInfo.class));
}
}
Loading