diff --git a/src/main/java/org/gitlab4j/api/AbstractApi.java b/src/main/java/org/gitlab4j/api/AbstractApi.java index b174c6e9f..d4b331b69 100644 --- a/src/main/java/org/gitlab4j/api/AbstractApi.java +++ b/src/main/java/org/gitlab4j/api/AbstractApi.java @@ -13,6 +13,7 @@ import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Label; +import org.gitlab4j.api.models.Namespace; import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.User; import org.gitlab4j.api.utils.UrlEncoder; @@ -169,6 +170,34 @@ public Object getLabelIdOrName(Object obj) throws GitLabApiException { } } + public Object getNamespaceIdOrPath(Object obj) throws GitLabApiException { + + if (obj == null) { + throw (new RuntimeException("Cannot determine ID or path from null object")); + } else if (obj instanceof Long) { + return (obj); + } else if (obj instanceof String) { + return (urlEncode(((String) obj).trim())); + } else if (obj instanceof Namespace) { + + Long id = ((Namespace) obj).getId(); + if (id != null && id.longValue() > 0) { + return (id); + } + + String path = ((Namespace) obj).getFullPath(); + if (path != null && path.trim().length() > 0) { + return (urlEncode(path.trim())); + } + + throw (new RuntimeException("Cannot determine ID or path from provided Namespace instance")); + + } else { + throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() + + " instance, must be Long, String, or a Namespace instance")); + } + } + protected ApiVersion getApiVersion() { return (gitLabApi.getApiVersion()); } diff --git a/src/main/java/org/gitlab4j/api/NamespaceApi.java b/src/main/java/org/gitlab4j/api/NamespaceApi.java index ad6ed2db7..73b8e3ebf 100644 --- a/src/main/java/org/gitlab4j/api/NamespaceApi.java +++ b/src/main/java/org/gitlab4j/api/NamespaceApi.java @@ -1,6 +1,7 @@ package org.gitlab4j.api; import java.util.List; +import java.util.Optional; import java.util.stream.Stream; import javax.ws.rs.core.GenericType; @@ -130,4 +131,35 @@ public Pager findNamespaces(String query, int itemsPerPage) throws Gi public Stream findNamespacesStream(String query) throws GitLabApiException { return (findNamespaces(query, getDefaultPerPage()).stream()); } + + /** + * Get all details of a namespace. + * + *
GitLab Endpoint: GET /namespaces/:id
+ * + * @param namespaceIdOrPath the namespace ID, path of the namespace, or a Namespace instance holding the namespace ID or path + * @return the Namespace instance for the specified path + * @throws GitLabApiException if any exception occurs + */ + + public Namespace getNamespace(Object namespaceIdOrPath) throws GitLabApiException { + Response response = get(Response.Status.OK, null, "namespaces", getNamespaceIdOrPath(namespaceIdOrPath)); + return (response.readEntity(Namespace .class)); + } + + /** + * Get all details of a namespace as an Optional instance. + * + *
GitLab Endpoint: GET /namespaces/:id
+ * + * @param namespaceIdOrPath the namespace ID, path of the namespace, or a Namespace instance holding the namespace ID or path + * @return the Group for the specified group path as an Optional instance + */ + public Optional getOptionalNamespace(Object namespaceIdOrPath) { + try { + return (Optional.ofNullable(getNamespace(namespaceIdOrPath))); + } catch (GitLabApiException glae) { + return (GitLabApi.createOptionalFromException(glae)); + } + } }