|
1 | 1 | package org.gitlab4j.api;
|
2 | 2 |
|
3 | 3 | import java.util.List;
|
| 4 | +import java.util.Optional; |
| 5 | + |
4 | 6 | import javax.ws.rs.core.GenericType;
|
5 | 7 | import javax.ws.rs.core.Response;
|
| 8 | + |
6 | 9 | import org.gitlab4j.api.models.Snippet;
|
7 | 10 | import org.gitlab4j.api.models.Visibility;
|
8 | 11 |
|
|
11 | 14 | */
|
12 | 15 | public class SnippetsApi extends AbstractApi {
|
13 | 16 |
|
14 |
| - public SnippetsApi(GitLabApi gitLabApi) { |
15 |
| - super(gitLabApi); |
16 |
| - } |
| 17 | + public SnippetsApi(GitLabApi gitLabApi) { |
| 18 | + super(gitLabApi); |
| 19 | + } |
17 | 20 |
|
18 | 21 | /**
|
19 |
| - * Create a new Snippet. |
| 22 | + * Get a Pager of the authenticated user's snippets. |
20 | 23 | *
|
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 |
25 | 28 | * @throws GitLabApiException if any exception occurs
|
26 | 29 | */
|
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 | + |
37 | 34 | /**
|
38 |
| - * Create a new Snippet. |
| 35 | + * Get a list of the authenticated user's snippets. |
39 | 36 | *
|
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 |
46 | 63 | * @throws GitLabApiException if any exception occurs
|
47 | 64 | */
|
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 | + } |
59 | 68 |
|
60 | 69 | /**
|
61 |
| - * Removes Snippet |
| 70 | + * Get the content of a Snippet. |
62 | 71 | *
|
63 |
| - * DELETE /snippets/:id |
| 72 | + * GET /snippets/:id/raw |
64 | 73 | *
|
65 | 74 | * @param snippetId the snippet ID to remove
|
| 75 | + * @return the content of snippet |
66 | 76 | * @throws GitLabApiException if any exception occurs
|
67 | 77 | */
|
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 | + } |
74 | 82 |
|
75 | 83 | /**
|
76 |
| - * Get a list of Authenticated User's Snippets. |
77 |
| - * |
78 |
| - * GET /snippets |
| 84 | + * Get a specific Snippet. |
79 | 85 | *
|
| 86 | + * @param snippetId the snippet ID to get |
80 | 87 | * @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 |
82 | 89 | * @throws GitLabApiException if any exception occurs
|
83 | 90 | */
|
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"); |
92 | 95 | }
|
93 | 96 |
|
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 | + |
97 | 107 | /**
|
98 |
| - * Get a list of Authenticated User's Snippets. |
| 108 | + * Get a specific Snippet. |
99 | 109 | *
|
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. |
101 | 120 | *
|
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 |
103 | 174 | * @throws GitLabApiException if any exception occurs
|
104 | 175 | */
|
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 | + |
109 | 187 | /**
|
110 |
| - * Get a the content of a Snippet |
| 188 | + * Removes Snippet. |
111 | 189 | *
|
112 |
| - * GET /snippets/id/raw |
| 190 | + * DELETE /snippets/:id |
113 | 191 | *
|
114 | 192 | * @param snippetId the snippet ID to remove
|
115 |
| - * @return the content of snippet |
116 | 193 | * @throws GitLabApiException if any exception occurs
|
117 | 194 | */
|
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 | + } |
157 | 203 | }
|
0 commit comments