Skip to content

Commit 6655095

Browse files
committed
Added Pager and Optional support.
1 parent ae37475 commit 6655095

File tree

1 file changed

+154
-108
lines changed

1 file changed

+154
-108
lines changed
Lines changed: 154 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.gitlab4j.api;
22

33
import java.util.List;
4+
import java.util.Optional;
5+
46
import javax.ws.rs.core.GenericType;
57
import javax.ws.rs.core.Response;
8+
69
import org.gitlab4j.api.models.Snippet;
710
import org.gitlab4j.api.models.Visibility;
811

@@ -11,147 +14,190 @@
1114
*/
1215
public class SnippetsApi extends AbstractApi {
1316

14-
public SnippetsApi(GitLabApi gitLabApi) {
15-
super(gitLabApi);
16-
}
17+
public SnippetsApi(GitLabApi gitLabApi) {
18+
super(gitLabApi);
19+
}
1720

1821
/**
19-
* Create a new Snippet.
22+
* Get a Pager of the authenticated user's snippets.
2023
*
21-
* @param title the title of the snippet
22-
* @param fileName the file name of the snippet
23-
* @param content the content of the snippet
24-
* @return the created Snippet
24+
* GET /snippets
25+
*
26+
* @param itemsPerPage the number of snippets per page
27+
* @return the Pager of snippets
2528
* @throws GitLabApiException if any exception occurs
2629
*/
27-
public Snippet createSnippet(String title, String fileName, String content) throws GitLabApiException {
28-
GitLabApiForm formData = new GitLabApiForm()
29-
.withParam("title", title, true)
30-
.withParam("file_name", fileName, true)
31-
.withParam("content", content, true);
32-
33-
Response response = post(Response.Status.CREATED, formData, "snippets");
34-
return (response.readEntity(Snippet.class));
35-
}
36-
30+
public Pager<Snippet> getSnippets(int itemsPerPage) throws GitLabApiException {
31+
return (new Pager<Snippet>(this, Snippet.class, itemsPerPage, null, "snippets"));
32+
}
33+
3734
/**
38-
* Create a new Snippet.
35+
* Get a list of the authenticated user's snippets.
3936
*
40-
* @param title the title of the snippet
41-
* @param fileName the file name of the snippet
42-
* @param content the content of the snippet
43-
* @param visibility the visibility (Public, Internal, Private) of the snippet
44-
* @param description the description of the snippet
45-
* @return the created Snippet
37+
* GET /snippets
38+
*
39+
* @param downloadContent indicating whether to download the snippet content
40+
* @return a list of authenticated user's snippets
41+
* @throws GitLabApiException if any exception occurs
42+
*/
43+
public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException {
44+
45+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
46+
List<Snippet> snippets = (response.readEntity(new GenericType<List<Snippet>>(){}));
47+
48+
if (downloadContent) {
49+
for (Snippet snippet : snippets) {
50+
snippet.setContent(getSnippetContent(snippet.getId()));
51+
}
52+
}
53+
54+
return snippets;
55+
}
56+
57+
/**
58+
* Get a list of the authenticated user's snippets.
59+
*
60+
* GET /snippets
61+
*
62+
* @return a list of authenticated user's snippets
4663
* @throws GitLabApiException if any exception occurs
4764
*/
48-
public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException {
49-
GitLabApiForm formData = new GitLabApiForm()
50-
.withParam("title", title, true)
51-
.withParam("file_name", fileName, true)
52-
.withParam("content", content, true)
53-
.withParam("visibility", visibility)
54-
.withParam("description", description);
55-
56-
Response response = post(Response.Status.CREATED, formData, "snippets");
57-
return (response.readEntity(Snippet.class));
58-
}
65+
public List<Snippet> getSnippets() throws GitLabApiException {
66+
return getSnippets(false);
67+
}
5968

6069
/**
61-
* Removes Snippet
70+
* Get the content of a Snippet.
6271
*
63-
* DELETE /snippets/:id
72+
* GET /snippets/:id/raw
6473
*
6574
* @param snippetId the snippet ID to remove
75+
* @return the content of snippet
6676
* @throws GitLabApiException if any exception occurs
6777
*/
68-
public void deleteSnippet(Integer snippetId) throws GitLabApiException {
69-
if (snippetId == null) {
70-
throw new RuntimeException("snippetId can't be null");
71-
}
72-
delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
73-
}
78+
public String getSnippetContent(Integer snippetId) throws GitLabApiException {
79+
Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
80+
return (response.readEntity(String.class));
81+
}
7482

7583
/**
76-
* Get a list of Authenticated User's Snippets.
77-
*
78-
* GET /snippets
84+
* Get a specific Snippet.
7985
*
86+
* @param snippetId the snippet ID to get
8087
* @param downloadContent indicating whether to download the snippet content
81-
* @return a list of authenticated user's snippets
88+
* @return the snippet with the given id
8289
* @throws GitLabApiException if any exception occurs
8390
*/
84-
public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException {
85-
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
86-
List<Snippet> snippets = (response.readEntity(new GenericType<List<Snippet>>() {}));
87-
88-
if(downloadContent) {
89-
for (Snippet snippet : snippets) {
90-
snippet.setContent(getSnippetContent(snippet.getId()));
91-
}
91+
public Snippet getSnippet(Integer snippetId, boolean downloadContent) throws GitLabApiException {
92+
93+
if (snippetId == null) {
94+
throw new RuntimeException("snippetId can't be null");
9295
}
9396

94-
return snippets;
95-
}
96-
97+
Response response = get(Response.Status.OK, null, "snippets", snippetId);
98+
Snippet snippet = response.readEntity(Snippet.class);
99+
100+
if (downloadContent) {
101+
snippet.setContent(getSnippetContent(snippet.getId()));
102+
}
103+
104+
return snippet;
105+
}
106+
97107
/**
98-
* Get a list of Authenticated User's Snippets.
108+
* Get a specific Snippet.
99109
*
100-
* GET /snippets
110+
* @param snippetId the snippet ID to get
111+
* @return the snippet with the given id
112+
* @throws GitLabApiException if any exception occurs
113+
*/
114+
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
115+
return getSnippet(snippetId, false);
116+
}
117+
118+
/**
119+
* Get a specific snippet as an Optional instance.
101120
*
102-
* @return a list of authenticated user's snippets
121+
* GET /snippets/:snippet_id
122+
*
123+
* @param snippetId the ID of the snippet to get the Optional instance for
124+
* @return the specified Snippet as an Optional instance
125+
*/
126+
public Optional<Snippet> getOptionalSnippet( Integer snippetId) {
127+
return (getOptionalSnippet(snippetId, false));
128+
}
129+
130+
/**
131+
* Get a specific snippet as an Optional instance.
132+
*
133+
* GET /snippets/:snippet_id
134+
*
135+
* @param snippetId the ID of the snippet to get the Optional instance for
136+
* @param downloadContent indicating whether to download the snippet content
137+
* @return the specified Snippet as an Optional instance
138+
*/
139+
public Optional<Snippet> getOptionalSnippet(Integer snippetId, boolean downloadContent) {
140+
try {
141+
return (Optional.ofNullable(getSnippet(snippetId, downloadContent)));
142+
} catch (GitLabApiException glae) {
143+
return (GitLabApi.createOptionalFromException(glae));
144+
}
145+
}
146+
147+
/**
148+
* Create a new Snippet.
149+
*
150+
* @param title the title of the snippet
151+
* @param fileName the file name of the snippet
152+
* @param content the content of the snippet
153+
* @return the created Snippet
154+
* @throws GitLabApiException if any exception occurs
155+
*/
156+
public Snippet createSnippet(String title, String fileName, String content) throws GitLabApiException {
157+
GitLabApiForm formData = new GitLabApiForm()
158+
.withParam("title", title, true)
159+
.withParam("file_name", fileName, true)
160+
.withParam("content", content, true);
161+
Response response = post(Response.Status.CREATED, formData, "snippets");
162+
return (response.readEntity(Snippet.class));
163+
}
164+
165+
/**
166+
* Create a new Snippet.
167+
*
168+
* @param title the title of the snippet
169+
* @param fileName the file name of the snippet
170+
* @param content the content of the snippet
171+
* @param visibility the visibility (Public, Internal, Private) of the snippet
172+
* @param description the description of the snippet
173+
* @return the created Snippet
103174
* @throws GitLabApiException if any exception occurs
104175
*/
105-
public List<Snippet> getSnippets() throws GitLabApiException {
106-
return getSnippets(false);
107-
}
108-
176+
public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException {
177+
GitLabApiForm formData = new GitLabApiForm()
178+
.withParam("title", title, true)
179+
.withParam("file_name", fileName, true)
180+
.withParam("content", content, true)
181+
.withParam("visibility", visibility)
182+
.withParam("description", description);
183+
Response response = post(Response.Status.CREATED, formData, "snippets");
184+
return (response.readEntity(Snippet.class));
185+
}
186+
109187
/**
110-
* Get a the content of a Snippet
188+
* Removes Snippet.
111189
*
112-
* GET /snippets/id/raw
190+
* DELETE /snippets/:id
113191
*
114192
* @param snippetId the snippet ID to remove
115-
* @return the content of snippet
116193
* @throws GitLabApiException if any exception occurs
117194
*/
118-
public String getSnippetContent(Integer snippetId) throws GitLabApiException {
119-
Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
120-
return (response.readEntity(String.class));
121-
}
122-
123-
/**
124-
* Get a specific Snippet
125-
*
126-
* @param snippetId the snippet ID to remove
127-
* @param downloadContent indicating whether to download the snippet content
128-
* @return the snippet with the given id
129-
* @throws GitLabApiException if any exception occurs
130-
*/
131-
public Snippet getSnippet(Integer snippetId, boolean downloadContent) throws GitLabApiException {
132-
if (snippetId == null) {
133-
throw new RuntimeException("snippetId can't be null");
134-
}
135-
136-
Response response = get(Response.Status.OK, null, "snippets", snippetId);
137-
Snippet snippet = response.readEntity(Snippet.class);
138-
139-
if(downloadContent) {
140-
snippet.setContent(getSnippetContent(snippet.getId()));
141-
}
142-
143-
return snippet;
144-
}
145-
146-
/**
147-
* Get a specific Snippet
148-
*
149-
* @param snippetId the snippet ID to remove
150-
* @return the snippet with the given id
151-
* @throws GitLabApiException if any exception occurs
152-
*/
153-
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
154-
return getSnippet(snippetId, false);
155-
}
156-
195+
public void deleteSnippet(Integer snippetId) throws GitLabApiException {
196+
197+
if (snippetId == null) {
198+
throw new RuntimeException("snippetId can't be null");
199+
}
200+
201+
delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
202+
}
157203
}

0 commit comments

Comments
 (0)