org.gitlab4j.api.SnippetsApi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gitlab4j-api Show documentation
Show all versions of gitlab4j-api Show documentation
GitLab4J-API (gitlab4j-api) provides a full featured Java client library for working with GitLab repositories and servers via the GitLab REST API.
package org.gitlab4j.api;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.Visibility;
/**
* This class provides an entry point to all the GitLab Snippets API project calls.
*/
public class SnippetsApi extends AbstractApi {
public SnippetsApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get a list of the authenticated user's snippets.
*
* GitLab Endpoint: GET /snippets
*
* @param downloadContent indicating whether to download the snippet content
* @return a list of authenticated user's snippets
* @throws GitLabApiException if any exception occurs
*/
public List getSnippets(boolean downloadContent) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
List snippets = (response.readEntity(new GenericType>(){}));
if (downloadContent) {
for (Snippet snippet : snippets) {
snippet.setContent(getSnippetContent(snippet.getId()));
}
}
return snippets;
}
/**
* Get a list of the authenticated user's snippets.
*
* GitLab Endpoint: GET /snippets
*
* @return a list of authenticated user's snippets
* @throws GitLabApiException if any exception occurs
*/
public List getSnippets() throws GitLabApiException {
return (getSnippets(getDefaultPerPage()).all());
}
/**
* Get a Pager of the authenticated user's snippets.
*
* GitLab Endpoint: GET /snippets
*
* @param itemsPerPage the number of snippets per page
* @return the Pager of snippets
* @throws GitLabApiException if any exception occurs
*/
public Pager getSnippets(int itemsPerPage) throws GitLabApiException {
return (new Pager(this, Snippet.class, itemsPerPage, null, "snippets"));
}
/**
* Get a Stream of the authenticated user's snippets.
*
* GitLab Endpoint: GET /snippets
*
* @return a Stream of authenticated user's snippets
* @throws GitLabApiException if any exception occurs
*/
public Stream getSnippetsStream() throws GitLabApiException {
return (getSnippets(getDefaultPerPage()).stream());
}
/**
* Get the content of a Snippet.
*
* GitLab Endpoint: GET /snippets/:id/raw
*
* @param snippetId the snippet ID to remove
* @return the content of snippet
* @throws GitLabApiException if any exception occurs
*/
public String getSnippetContent(Integer snippetId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
return (response.readEntity(String.class));
}
/**
* Get a specific Snippet.
*
* @param snippetId the snippet ID to get
* @param downloadContent indicating whether to download the snippet content
* @return the snippet with the given id
* @throws GitLabApiException if any exception occurs
*/
public Snippet getSnippet(Integer snippetId, boolean downloadContent) throws GitLabApiException {
if (snippetId == null) {
throw new RuntimeException("snippetId can't be null");
}
Response response = get(Response.Status.OK, null, "snippets", snippetId);
Snippet snippet = response.readEntity(Snippet.class);
if (downloadContent) {
snippet.setContent(getSnippetContent(snippet.getId()));
}
return snippet;
}
/**
* Get a specific Snippet.
*
* @param snippetId the snippet ID to get
* @return the snippet with the given id
* @throws GitLabApiException if any exception occurs
*/
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
return getSnippet(snippetId, false);
}
/**
* Get a specific snippet as an Optional instance.
*
* GitLab Endpoint: GET /snippets/:snippet_id
*
* @param snippetId the ID of the snippet to get the Optional instance for
* @return the specified Snippet as an Optional instance
*/
public Optional getOptionalSnippet(Integer snippetId) {
return (getOptionalSnippet(snippetId, false));
}
/**
* Get a specific snippet as an Optional instance.
*
* GitLab Endpoint: GET /snippets/:snippet_id
*
* @param snippetId the ID of the snippet to get the Optional instance for
* @param downloadContent indicating whether to download the snippet content
* @return the specified Snippet as an Optional instance
*/
public Optional getOptionalSnippet(Integer snippetId, boolean downloadContent) {
try {
return (Optional.ofNullable(getSnippet(snippetId, downloadContent)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create a new Snippet.
*
* @param title the title of the snippet
* @param fileName the file name of the snippet
* @param content the content of the snippet
* @return the created Snippet
* @throws GitLabApiException if any exception occurs
*/
public Snippet createSnippet(String title, String fileName, String content) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("file_name", fileName, true)
.withParam("content", content, true);
Response response = post(Response.Status.CREATED, formData, "snippets");
return (response.readEntity(Snippet.class));
}
/**
* Create a new Snippet.
*
* @param title the title of the snippet
* @param fileName the file name of the snippet
* @param content the content of the snippet
* @param visibility the visibility (Public, Internal, Private) of the snippet
* @param description the description of the snippet
* @return the created Snippet
* @throws GitLabApiException if any exception occurs
*/
public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("file_name", fileName, true)
.withParam("content", content, true)
.withParam("visibility", visibility)
.withParam("description", description);
Response response = post(Response.Status.CREATED, formData, "snippets");
return (response.readEntity(Snippet.class));
}
/**
* Removes Snippet.
*
* GitLab Endpoint: DELETE /snippets/:id
*
* @param snippetId the snippet ID to remove
* @throws GitLabApiException if any exception occurs
*/
public void deleteSnippet(Integer snippetId) throws GitLabApiException {
if (snippetId == null) {
throw new RuntimeException("snippetId can't be null");
}
delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
}
}