@@ -46,6 +46,7 @@ public class GitLabApiClient {
4646
4747 private ClientConfig clientConfig ;
4848 private Client apiClient ;
49+ private String baseUrl ;
4950 private String hostUrl ;
5051 private TokenType tokenType = TokenType .PRIVATE ;
5152 private String authToken ;
@@ -83,7 +84,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
8384 /**
8485 * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
8586 * server URL, private token, and secret token.
86- *
87+ *
8788 * @param hostUrl the URL to the GitLab API server
8889 * @param privateToken the private token to authenticate with
8990 */
@@ -94,7 +95,7 @@ public GitLabApiClient(String hostUrl, String privateToken) {
9495 /**
9596 * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
9697 * server URL, private token, and secret token.
97- *
98+ *
9899 * @param hostUrl the URL to the GitLab API server
99100 * @param tokenType the type of auth the token is for, PRIVATE or ACCESS
100101 * @param authToken the token to authenticate with
@@ -133,7 +134,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
133134 /**
134135 * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
135136 * server URL, private token, and secret token.
136- *
137+ *
137138 * @param hostUrl the URL to the GitLab API server
138139 * @param privateToken the private token to authenticate with
139140 * @param secretToken use this token to validate received payloads
@@ -145,7 +146,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken)
145146 /**
146147 * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
147148 * server URL, private token, and secret token.
148- *
149+ *
149150 * @param hostUrl the URL to the GitLab API server
150151 * @param tokenType the type of auth the token is for, PRIVATE or ACCESS
151152 * @param authToken the token to authenticate with
@@ -158,7 +159,7 @@ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, St
158159 /**
159160 * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
160161 * server URL and private token.
161- *
162+ *
162163 * @param hostUrl the URL to the GitLab API server
163164 * @param privateToken the private token to authenticate with
164165 * @param secretToken use this token to validate received payloads
@@ -169,7 +170,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken,
169170 }
170171
171172 /**
172- * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
173+ * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
173174 * server URL and private token.
174175 *
175176 * @param apiVersion the ApiVersion specifying which version of the API to use
@@ -183,7 +184,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToke
183184 }
184185
185186 /**
186- * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
187+ * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
187188 * server URL and private token.
188189 *
189190 * @param apiVersion the ApiVersion specifying which version of the API to use
@@ -197,6 +198,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
197198
198199 // Remove the trailing "/" from the hostUrl if present
199200 this .hostUrl = (hostUrl .endsWith ("/" ) ? hostUrl .replaceAll ("/$" , "" ) : hostUrl );
201+ this .baseUrl = this .hostUrl ;
200202 if (ApiVersion .OAUTH2_CLIENT != apiVersion ) {
201203 this .hostUrl += apiVersion .getApiNamespace ();
202204 }
@@ -256,7 +258,6 @@ TokenType getTokenType() {
256258 /**
257259 * Set the ID of the user to sudo as.
258260 *
259- * @param sudoAsId the ID of the user to sudo as
260261 */
261262 Integer getSudoAsId () {
262263 return (sudoAsId );
@@ -273,29 +274,44 @@ void setSudoAsId(Integer sudoAsId) {
273274
274275 /**
275276 * Construct a REST URL with the specified path arguments.
276- *
277+ *
277278 * @param pathArgs variable list of arguments used to build the URI
278279 * @return a REST URL with the specified path arguments
279280 * @throws IOException if an error occurs while constructing the URL
280281 */
281282 protected URL getApiUrl (Object ... pathArgs ) throws IOException {
283+ String url = appendPathArgs (this .hostUrl , pathArgs );
284+ return (new URL (url ));
285+ }
286+
287+ /**
288+ * Construct a REST URL with the specified path arguments using
289+ * Gitlab base url.
290+ *
291+ * @param pathArgs variable list of arguments used to build the URI
292+ * @return a REST URL with the specified path arguments
293+ * @throws IOException if an error occurs while constructing the URL
294+ */
295+ protected URL getUrlWithBase (Object ... pathArgs ) throws IOException {
296+ String url = appendPathArgs (this .baseUrl , pathArgs );
297+ return (new URL (url ));
298+ }
282299
283- StringBuilder url = new StringBuilder ();
284- url . append ( hostUrl );
300+ private String appendPathArgs ( String url , Object ... pathArgs ) {
301+ StringBuilder urlBuilder = new StringBuilder ( url );
285302 for (Object pathArg : pathArgs ) {
286303 if (pathArg != null ) {
287- url .append ("/" );
288- url .append (pathArg .toString ());
304+ urlBuilder .append ("/" );
305+ urlBuilder .append (pathArg .toString ());
289306 }
290307 }
291-
292- return (new URL (url .toString ()));
308+ return urlBuilder .toString ();
293309 }
294310
295311 /**
296312 * Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid,
297313 * otherwise returns false.
298- *
314+ *
299315 * @param response the Response instance sent from the GitLab server
300316 * @return true if the response's secret token is valid, otherwise returns false
301317 */
@@ -314,7 +330,7 @@ protected boolean validateSecretToken(Response response) {
314330 /**
315331 * Perform an HTTP GET call with the specified query parameters and path objects, returning
316332 * a ClientResponse instance with the data returned from the endpoint.
317- *
333+ *
318334 * @param queryParams multivalue map of request parameters
319335 * @param pathArgs variable list of arguments used to build the URI
320336 * @return a ClientResponse instance with the data returned from the endpoint
@@ -328,7 +344,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, Object... pat
328344 /**
329345 * Perform an HTTP GET call with the specified query parameters and URL, returning
330346 * a ClientResponse instance with the data returned from the endpoint.
331- *
347+ *
332348 * @param queryParams multivalue map of request parameters
333349 * @param url the fully formed path to the GitLab API endpoint
334350 * @return a ClientResponse instance with the data returned from the endpoint
@@ -340,7 +356,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, URL url) {
340356 /**
341357 * Perform an HTTP GET call with the specified query parameters and path objects, returning
342358 * a ClientResponse instance with the data returned from the endpoint.
343- *
359+ *
344360 * @param queryParams multivalue map of request parameters
345361 * @param accepts if non-empty will set the Accepts header to this value
346362 * @param pathArgs variable list of arguments used to build the URI
@@ -355,7 +371,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, St
355371 /**
356372 * Perform an HTTP GET call with the specified query parameters and URL, returning
357373 * a ClientResponse instance with the data returned from the endpoint.
358- *
374+ *
359375 * @param queryParams multivalue map of request parameters
360376 * @param url the fully formed path to the GitLab API endpoint
361377 * @param accepts if non-empty will set the Accepts header to this value
@@ -368,7 +384,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, UR
368384 /**
369385 * Perform an HTTP POST call with the specified form data and path objects, returning
370386 * a ClientResponse instance with the data returned from the endpoint.
371- *
387+ *
372388 * @param formData the Form containing the name/value pairs
373389 * @param pathArgs variable list of arguments used to build the URI
374390 * @return a ClientResponse instance with the data returned from the endpoint
@@ -382,7 +398,7 @@ protected Response post(Form formData, Object... pathArgs) throws IOException {
382398 /**
383399 * Perform an HTTP POST call with the specified form data and path objects, returning
384400 * a ClientResponse instance with the data returned from the endpoint.
385- *
401+ *
386402 * @param queryParams multivalue map of request parameters
387403 * @param pathArgs variable list of arguments used to build the URI
388404 * @return a Response instance with the data returned from the endpoint
@@ -396,7 +412,7 @@ protected Response post(MultivaluedMap<String, String> queryParams, Object... pa
396412 /**
397413 * Perform an HTTP POST call with the specified form data and URL, returning
398414 * a ClientResponse instance with the data returned from the endpoint.
399- *
415+ *
400416 * @param formData the Form containing the name/value pairs
401417 * @param url the fully formed path to the GitLab API endpoint
402418 * @return a ClientResponse instance with the data returned from the endpoint
@@ -453,7 +469,7 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
453469 /**
454470 * Perform an HTTP PUT call with the specified form data and path objects, returning
455471 * a ClientResponse instance with the data returned from the endpoint.
456- *
472+ *
457473 * @param queryParams multivalue map of request parameters
458474 * @param pathArgs variable list of arguments used to build the URI
459475 * @return a ClientResponse instance with the data returned from the endpoint
@@ -513,7 +529,7 @@ protected Response put(Form formData, URL url) {
513529 /**
514530 * Perform an HTTP DELETE call with the specified form data and path objects, returning
515531 * a Response instance with the data returned from the endpoint.
516- *
532+ *
517533 * @param queryParams multivalue map of request parameters
518534 * @param pathArgs variable list of arguments used to build the URI
519535 * @return a Response instance with the data returned from the endpoint
@@ -526,7 +542,7 @@ protected Response delete(MultivaluedMap<String, String> queryParams, Object...
526542 /**
527543 * Perform an HTTP DELETE call with the specified form data and URL, returning
528544 * a Response instance with the data returned from the endpoint.
529- *
545+ *
530546 * @param queryParams multivalue map of request parameters
531547 * @param url the fully formed path to the GitLab API endpoint
532548 * @return a Response instance with the data returned from the endpoint
0 commit comments