From 1fb90f50886541bdd3a5c833c38f2c7bc0b4b451 Mon Sep 17 00:00:00 2001 From: Yakov Golovanev Date: Wed, 19 Dec 2018 16:06:12 +0300 Subject: [PATCH 1/3] Add possibility to change gitlab rest api namespace --- src/main/java/org/gitlab/api/GitlabAPI.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index c544351d..ca1b5922 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -36,7 +36,7 @@ public class GitlabAPI { public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - private static final String API_NAMESPACE = "/api/v4"; + private static String API_NAMESPACE = "/api/v4"; private static final String PARAM_SUDO = "sudo"; private static final String PARAM_MAX_ITEMS_PER_PAGE = new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).toString(); @@ -377,7 +377,7 @@ public GitlabSSHKey createSSHKey(Integer targetUserId, String title, String key) return dispatch().to(tailUrl, GitlabSSHKey.class); } - + /** * Create a new ssh key for the authenticated user. * @@ -632,7 +632,7 @@ public GitlabGroup createGroup(String name, String path, String ldapCn, GitlabAc return dispatch().to(tailUrl, GitlabGroup.class); } - + /** * Creates a Group * @@ -1513,6 +1513,13 @@ public GitlabMergeRequestApprovals setMergeRequestApprovers(GitlabMergeRequest m } /** + * Set the namespace of gitlab rest api. + */ + public static void setApiNamespace(String version) { + API_NAMESPACE = version; + } + + /** * Cherry picks a commit. * * @param projectId The id of the project From ae72205d030cfb4435619589e0a8a703fb1a612b Mon Sep 17 00:00:00 2001 From: Yakov Golovanev Date: Wed, 19 Dec 2018 16:12:08 +0300 Subject: [PATCH 2/3] Fix indent. --- src/main/java/org/gitlab/api/GitlabAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index ca1b5922..d51a5bfd 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -1519,7 +1519,7 @@ public static void setApiNamespace(String version) { API_NAMESPACE = version; } - /** + /** * Cherry picks a commit. * * @param projectId The id of the project From 60058eae51f901c231d48ef7433afb4562ad736b Mon Sep 17 00:00:00 2001 From: Yakov Golovanev Date: Mon, 25 Feb 2019 11:53:50 +0300 Subject: [PATCH 3/3] Convert API namespace to instance field --- src/main/java/org/gitlab/api/GitlabAPI.java | 30 +++++++++++++-------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index d51a5bfd..c8770ec1 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -36,7 +36,8 @@ public class GitlabAPI { public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - private static String API_NAMESPACE = "/api/v4"; + + private static final String DEFAULT_API_NAMESPACE = "/api/v4"; private static final String PARAM_SUDO = "sudo"; private static final String PARAM_MAX_ITEMS_PER_PAGE = new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).toString(); @@ -45,6 +46,7 @@ public class GitlabAPI { private final String apiToken; private final TokenType tokenType; private AuthMethod authMethod; + private final String apiNamespace; private boolean ignoreCertificateErrors = false; private Proxy proxy; private int defaultTimeout = 0; @@ -52,16 +54,21 @@ public class GitlabAPI { private int connectionTimeout = defaultTimeout; private String userAgent = GitlabAPI.class.getCanonicalName() + "/" + System.getProperty("java.version"); - private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) { + private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method, String apiNamespace) { this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl; this.apiToken = apiToken; this.tokenType = tokenType; this.authMethod = method; + this.apiNamespace = apiNamespace; + } + + private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) { + this(hostUrl, apiToken, tokenType, method, DEFAULT_API_NAMESPACE); } public static GitlabSession connect(String hostUrl, String username, String password) throws IOException { String tailUrl = GitlabSession.URL; - GitlabAPI api = connect(hostUrl, null, null, null); + GitlabAPI api = connect(hostUrl, null, null, (AuthMethod) null); return api.dispatch().with("login", username).with("password", password) .to(tailUrl, GitlabSession.class); } @@ -78,6 +85,14 @@ public static GitlabAPI connect(String hostUrl, String apiToken, TokenType token return new GitlabAPI(hostUrl, apiToken, tokenType, method); } + public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType, String apiNamespace) { + return new GitlabAPI(hostUrl, apiToken, tokenType, AuthMethod.HEADER, apiNamespace); + } + + public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method, String apiNamespace) { + return new GitlabAPI(hostUrl, apiToken, tokenType, method, apiNamespace); + } + public GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) { this.ignoreCertificateErrors = ignoreCertificateErrors; return this; @@ -159,7 +174,7 @@ public URL getAPIUrl(String tailAPIUrl) throws IOException { if (!tailAPIUrl.startsWith("/")) { tailAPIUrl = "/" + tailAPIUrl; } - return new URL(hostUrl + API_NAMESPACE + tailAPIUrl); + return new URL(hostUrl + apiNamespace + tailAPIUrl); } public URL getUrl(String tailAPIUrl) throws IOException { @@ -1512,13 +1527,6 @@ public GitlabMergeRequestApprovals setMergeRequestApprovers(GitlabMergeRequest m .to(tailUrl, GitlabMergeRequestApprovals.class); } - /** - * Set the namespace of gitlab rest api. - */ - public static void setApiNamespace(String version) { - API_NAMESPACE = version; - } - /** * Cherry picks a commit. *