Unverified Commit 0028d7e7 authored by ddoleye's avatar ddoleye Committed by GitHub
Browse files

Add NamespaceApi.getNamespace(idOrPath) using /namespaces/:id (#1079)

parent ce461112
......@@ -13,6 +13,7 @@ import javax.ws.rs.core.StreamingOutput;
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 abstract class AbstractApi implements Constants {
}
}
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());
}
......
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 class NamespaceApi extends AbstractApi {
public Stream<Namespace> findNamespacesStream(String query) throws GitLabApiException {
return (findNamespaces(query, getDefaultPerPage()).stream());
}
/**
* Get all details of a namespace.
*
* <pre><code>GitLab Endpoint: GET /namespaces/:id</code></pre>
*
* @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.
*
* <pre><code>GitLab Endpoint: GET /namespaces/:id</code></pre>
*
* @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<Namespace> getOptionalNamespace(Object namespaceIdOrPath) {
try {
return (Optional.ofNullable(getNamespace(namespaceIdOrPath)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment