Commit 6655095e authored by Greg Messner's avatar Greg Messner
Browse files

Added Pager and Optional support.

parent ae37475b
package org.gitlab4j.api; package org.gitlab4j.api;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Snippet; import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.Visibility; import org.gitlab4j.api.models.Visibility;
...@@ -11,147 +14,190 @@ import org.gitlab4j.api.models.Visibility; ...@@ -11,147 +14,190 @@ import org.gitlab4j.api.models.Visibility;
*/ */
public class SnippetsApi extends AbstractApi { public class SnippetsApi extends AbstractApi {
public SnippetsApi(GitLabApi gitLabApi) { public SnippetsApi(GitLabApi gitLabApi) {
super(gitLabApi); super(gitLabApi);
} }
/** /**
* Create a new Snippet. * Get a Pager of the authenticated user's snippets.
* *
* @param title the title of the snippet * GET /snippets
* @param fileName the file name of the snippet *
* @param content the content of the snippet * @param itemsPerPage the number of snippets per page
* @return the created Snippet * @return the Pager of snippets
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public Snippet createSnippet(String title, String fileName, String content) throws GitLabApiException { public Pager<Snippet> getSnippets(int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm() return (new Pager<Snippet>(this, Snippet.class, itemsPerPage, null, "snippets"));
.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. * Get a list of the authenticated user's snippets.
* *
* @param title the title of the snippet * GET /snippets
* @param fileName the file name of the snippet *
* @param content the content of the snippet * @param downloadContent indicating whether to download the snippet content
* @param visibility the visibility (Public, Internal, Private) of the snippet * @return a list of authenticated user's snippets
* @param description the description of the snippet * @throws GitLabApiException if any exception occurs
* @return the created Snippet */
public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
List<Snippet> snippets = (response.readEntity(new GenericType<List<Snippet>>(){}));
if (downloadContent) {
for (Snippet snippet : snippets) {
snippet.setContent(getSnippetContent(snippet.getId()));
}
}
return snippets;
}
/**
* Get a list of the authenticated user's snippets.
*
* GET /snippets
*
* @return a list of authenticated user's snippets
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException { public List<Snippet> getSnippets() throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm() return getSnippets(false);
.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 * Get the content of a Snippet.
* *
* DELETE /snippets/:id * GET /snippets/:id/raw
* *
* @param snippetId the snippet ID to remove * @param snippetId the snippet ID to remove
* @return the content of snippet
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public void deleteSnippet(Integer snippetId) throws GitLabApiException { public String getSnippetContent(Integer snippetId) throws GitLabApiException {
if (snippetId == null) { Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
throw new RuntimeException("snippetId can't be null"); return (response.readEntity(String.class));
} }
delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
}
/** /**
* Get a list of Authenticated User's Snippets. * Get a specific Snippet.
*
* GET /snippets
* *
* @param snippetId the snippet ID to get
* @param downloadContent indicating whether to download the snippet content * @param downloadContent indicating whether to download the snippet content
* @return a list of authenticated user's snippets * @return the snippet with the given id
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException { public Snippet getSnippet(Integer snippetId, boolean downloadContent) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
List<Snippet> snippets = (response.readEntity(new GenericType<List<Snippet>>() {})); if (snippetId == null) {
throw new RuntimeException("snippetId can't be null");
if(downloadContent) {
for (Snippet snippet : snippets) {
snippet.setContent(getSnippetContent(snippet.getId()));
}
} }
return snippets; 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 list of Authenticated User's Snippets. * Get a specific Snippet.
* *
* GET /snippets * @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.
* *
* @return a list of authenticated user's snippets * 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<Snippet> getOptionalSnippet( Integer snippetId) {
return (getOptionalSnippet(snippetId, false));
}
/**
* Get a specific snippet as an Optional instance.
*
* 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<Snippet> 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 * @throws GitLabApiException if any exception occurs
*/ */
public List<Snippet> getSnippets() throws GitLabApiException { public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException {
return getSnippets(false); 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));
}
/** /**
* Get a the content of a Snippet * Removes Snippet.
* *
* GET /snippets/id/raw * DELETE /snippets/:id
* *
* @param snippetId the snippet ID to remove * @param snippetId the snippet ID to remove
* @return the content of snippet
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public String getSnippetContent(Integer snippetId) throws GitLabApiException { public void deleteSnippet(Integer snippetId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
return (response.readEntity(String.class)); if (snippetId == null) {
} throw new RuntimeException("snippetId can't be null");
}
/**
* Get a specific Snippet delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
* }
* @param snippetId the snippet ID to remove
* @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 remove
* @return the snippet with the given id
* @throws GitLabApiException if any exception occurs
*/
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
return getSnippet(snippetId, false);
}
} }
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