-
Notifications
You must be signed in to change notification settings - Fork 486
Health Check Api #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Health Check Api #159
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,7 @@ public class GitLabApiClient { | |
|
|
||
| private ClientConfig clientConfig; | ||
| private Client apiClient; | ||
| private String baseUrl; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -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 | ||
| */ | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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(); | ||
| } | ||
|
|
@@ -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); | ||
|
|
@@ -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(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| */ | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.