@@ -54,6 +54,7 @@ public class GitLabApiClient {
54
54
55
55
private ClientConfig clientConfig ;
56
56
private Client apiClient ;
57
+ private String baseUrl ;
57
58
private String hostUrl ;
58
59
private TokenType tokenType = TokenType .PRIVATE ;
59
60
private String authToken ;
@@ -91,7 +92,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
91
92
/**
92
93
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
93
94
* server URL, private token, and secret token.
94
- *
95
+ *
95
96
* @param hostUrl the URL to the GitLab API server
96
97
* @param privateToken the private token to authenticate with
97
98
*/
@@ -102,7 +103,7 @@ public GitLabApiClient(String hostUrl, String privateToken) {
102
103
/**
103
104
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
104
105
* server URL, private token, and secret token.
105
- *
106
+ *
106
107
* @param hostUrl the URL to the GitLab API server
107
108
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
108
109
* @param authToken the token to authenticate with
@@ -141,7 +142,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
141
142
/**
142
143
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
143
144
* server URL, private token, and secret token.
144
- *
145
+ *
145
146
* @param hostUrl the URL to the GitLab API server
146
147
* @param privateToken the private token to authenticate with
147
148
* @param secretToken use this token to validate received payloads
@@ -153,7 +154,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken)
153
154
/**
154
155
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
155
156
* server URL, private token, and secret token.
156
- *
157
+ *
157
158
* @param hostUrl the URL to the GitLab API server
158
159
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
159
160
* @param authToken the token to authenticate with
@@ -166,7 +167,7 @@ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, St
166
167
/**
167
168
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
168
169
* server URL and private token.
169
- *
170
+ *
170
171
* @param hostUrl the URL to the GitLab API server
171
172
* @param privateToken the private token to authenticate with
172
173
* @param secretToken use this token to validate received payloads
@@ -177,7 +178,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken,
177
178
}
178
179
179
180
/**
180
- * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
181
+ * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
181
182
* server URL and private token.
182
183
*
183
184
* @param apiVersion the ApiVersion specifying which version of the API to use
@@ -191,7 +192,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToke
191
192
}
192
193
193
194
/**
194
- * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
195
+ * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
195
196
* server URL and private token.
196
197
*
197
198
* @param apiVersion the ApiVersion specifying which version of the API to use
@@ -205,6 +206,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
205
206
206
207
// Remove the trailing "/" from the hostUrl if present
207
208
this .hostUrl = (hostUrl .endsWith ("/" ) ? hostUrl .replaceAll ("/$" , "" ) : hostUrl );
209
+ this .baseUrl = this .hostUrl ;
208
210
if (ApiVersion .OAUTH2_CLIENT != apiVersion ) {
209
211
this .hostUrl += apiVersion .getApiNamespace ();
210
212
}
@@ -265,7 +267,6 @@ TokenType getTokenType() {
265
267
/**
266
268
* Set the ID of the user to sudo as.
267
269
*
268
- * @param sudoAsId the ID of the user to sudo as
269
270
*/
270
271
Integer getSudoAsId () {
271
272
return (sudoAsId );
@@ -282,29 +283,44 @@ void setSudoAsId(Integer sudoAsId) {
282
283
283
284
/**
284
285
* Construct a REST URL with the specified path arguments.
285
- *
286
+ *
286
287
* @param pathArgs variable list of arguments used to build the URI
287
288
* @return a REST URL with the specified path arguments
288
289
* @throws IOException if an error occurs while constructing the URL
289
290
*/
290
291
protected URL getApiUrl (Object ... pathArgs ) throws IOException {
292
+ String url = appendPathArgs (this .hostUrl , pathArgs );
293
+ return (new URL (url ));
294
+ }
295
+
296
+ /**
297
+ * Construct a REST URL with the specified path arguments using
298
+ * Gitlab base url.
299
+ *
300
+ * @param pathArgs variable list of arguments used to build the URI
301
+ * @return a REST URL with the specified path arguments
302
+ * @throws IOException if an error occurs while constructing the URL
303
+ */
304
+ protected URL getUrlWithBase (Object ... pathArgs ) throws IOException {
305
+ String url = appendPathArgs (this .baseUrl , pathArgs );
306
+ return (new URL (url ));
307
+ }
291
308
292
- StringBuilder url = new StringBuilder ();
293
- url . append ( hostUrl );
309
+ private String appendPathArgs ( String url , Object ... pathArgs ) {
310
+ StringBuilder urlBuilder = new StringBuilder ( url );
294
311
for (Object pathArg : pathArgs ) {
295
312
if (pathArg != null ) {
296
- url .append ("/" );
297
- url .append (pathArg .toString ());
313
+ urlBuilder .append ("/" );
314
+ urlBuilder .append (pathArg .toString ());
298
315
}
299
316
}
300
-
301
- return (new URL (url .toString ()));
317
+ return urlBuilder .toString ();
302
318
}
303
319
304
320
/**
305
321
* Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid,
306
322
* otherwise returns false.
307
- *
323
+ *
308
324
* @param response the Response instance sent from the GitLab server
309
325
* @return true if the response's secret token is valid, otherwise returns false
310
326
*/
@@ -323,7 +339,7 @@ protected boolean validateSecretToken(Response response) {
323
339
/**
324
340
* Perform an HTTP GET call with the specified query parameters and path objects, returning
325
341
* a ClientResponse instance with the data returned from the endpoint.
326
- *
342
+ *
327
343
* @param queryParams multivalue map of request parameters
328
344
* @param pathArgs variable list of arguments used to build the URI
329
345
* @return a ClientResponse instance with the data returned from the endpoint
@@ -337,7 +353,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, Object... pat
337
353
/**
338
354
* Perform an HTTP GET call with the specified query parameters and URL, returning
339
355
* a ClientResponse instance with the data returned from the endpoint.
340
- *
356
+ *
341
357
* @param queryParams multivalue map of request parameters
342
358
* @param url the fully formed path to the GitLab API endpoint
343
359
* @return a ClientResponse instance with the data returned from the endpoint
@@ -349,7 +365,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, URL url) {
349
365
/**
350
366
* Perform an HTTP GET call with the specified query parameters and path objects, returning
351
367
* a ClientResponse instance with the data returned from the endpoint.
352
- *
368
+ *
353
369
* @param queryParams multivalue map of request parameters
354
370
* @param accepts if non-empty will set the Accepts header to this value
355
371
* @param pathArgs variable list of arguments used to build the URI
@@ -364,7 +380,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, St
364
380
/**
365
381
* Perform an HTTP GET call with the specified query parameters and URL, returning
366
382
* a ClientResponse instance with the data returned from the endpoint.
367
- *
383
+ *
368
384
* @param queryParams multivalue map of request parameters
369
385
* @param url the fully formed path to the GitLab API endpoint
370
386
* @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
377
393
/**
378
394
* Perform an HTTP POST call with the specified form data and path objects, returning
379
395
* a ClientResponse instance with the data returned from the endpoint.
380
- *
396
+ *
381
397
* @param formData the Form containing the name/value pairs
382
398
* @param pathArgs variable list of arguments used to build the URI
383
399
* @return a ClientResponse instance with the data returned from the endpoint
@@ -391,7 +407,7 @@ protected Response post(Form formData, Object... pathArgs) throws IOException {
391
407
/**
392
408
* Perform an HTTP POST call with the specified form data and path objects, returning
393
409
* a ClientResponse instance with the data returned from the endpoint.
394
- *
410
+ *
395
411
* @param queryParams multivalue map of request parameters
396
412
* @param pathArgs variable list of arguments used to build the URI
397
413
* @return a Response instance with the data returned from the endpoint
@@ -405,7 +421,7 @@ protected Response post(MultivaluedMap<String, String> queryParams, Object... pa
405
421
/**
406
422
* Perform an HTTP POST call with the specified form data and URL, returning
407
423
* a ClientResponse instance with the data returned from the endpoint.
408
- *
424
+ *
409
425
* @param formData the Form containing the name/value pairs
410
426
* @param url the fully formed path to the GitLab API endpoint
411
427
* @return a ClientResponse instance with the data returned from the endpoint
@@ -501,7 +517,7 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
501
517
/**
502
518
* Perform an HTTP PUT call with the specified form data and path objects, returning
503
519
* a ClientResponse instance with the data returned from the endpoint.
504
- *
520
+ *
505
521
* @param queryParams multivalue map of request parameters
506
522
* @param pathArgs variable list of arguments used to build the URI
507
523
* @return a ClientResponse instance with the data returned from the endpoint
@@ -561,7 +577,7 @@ protected Response put(Form formData, URL url) {
561
577
/**
562
578
* Perform an HTTP DELETE call with the specified form data and path objects, returning
563
579
* a Response instance with the data returned from the endpoint.
564
- *
580
+ *
565
581
* @param queryParams multivalue map of request parameters
566
582
* @param pathArgs variable list of arguments used to build the URI
567
583
* @return a Response instance with the data returned from the endpoint
@@ -574,7 +590,7 @@ protected Response delete(MultivaluedMap<String, String> queryParams, Object...
574
590
/**
575
591
* Perform an HTTP DELETE call with the specified form data and URL, returning
576
592
* a Response instance with the data returned from the endpoint.
577
- *
593
+ *
578
594
* @param queryParams multivalue map of request parameters
579
595
* @param url the fully formed path to the GitLab API endpoint
580
596
* @return a Response instance with the data returned from the endpoint
0 commit comments