|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Greg Messner <[email protected]> |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * this software and associated documentation files (the "Software"), to deal in |
| 8 | + * the Software without restriction, including without limitation the rights to |
| 9 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + * subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package org.gitlab4j.api; |
| 25 | + |
| 26 | +import org.gitlab4j.api.models.UncycloPage; |
| 27 | + |
| 28 | +import javax.ws.rs.core.GenericType; |
| 29 | +import javax.ws.rs.core.Response; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author shuklaalok7 ([email protected]) |
| 35 | + * @since v4.8.21 2018-06-5 1:26 AM IST |
| 36 | + */ |
| 37 | +public class UncyclosApi extends AbstractApi { |
| 38 | + |
| 39 | + |
| 40 | + public UncyclosApi(GitLabApi gitLabApi) { |
| 41 | + super(gitLabApi); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get a list of pages in project wiki. This only returns the first page of wiki-pages. |
| 46 | + * |
| 47 | + * GET /projects/:id/wikis |
| 48 | + * |
| 49 | + * @param projectId the project ID to get the wiki-pages for |
| 50 | + * @return a list of pages in the project's wiki |
| 51 | + * @throws GitLabApiException if any exception occurs |
| 52 | + */ |
| 53 | + public List<UncycloPage> getPages(Integer projectId) throws GitLabApiException { |
| 54 | + return getPages(projectId, 1, this.getDefaultPerPage()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get a list of project snippets. This only returns the first page of snippets. |
| 59 | + * |
| 60 | + * GET /projects/:id/wikis |
| 61 | + * |
| 62 | + * @param projectId the project ID to get the wiki pages for |
| 63 | + * @param page the page to get |
| 64 | + * @param perPage the number of wiki-pages per page |
| 65 | + * @return a list of pages in project's wiki for the specified range |
| 66 | + * @throws GitLabApiException if any exception occurs |
| 67 | + */ |
| 68 | + public List<UncycloPage> getPages(Integer projectId, int page, int perPage) throws GitLabApiException { |
| 69 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "wikis"); |
| 70 | + return response.readEntity(new GenericType<List<UncycloPage>>() {}); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get a Pager of project's wiki pages. |
| 75 | + * |
| 76 | + * GET /projects/:id/wikis |
| 77 | + * |
| 78 | + * @param projectId the project ID to get the wiki-pages for |
| 79 | + * @param itemsPerPage the number of wiki-pages per page |
| 80 | + * @return the Pager of wiki-pages |
| 81 | + * @throws GitLabApiException if any exception occurs |
| 82 | + */ |
| 83 | + public Pager<UncycloPage> getPages(Integer projectId, int itemsPerPage) throws GitLabApiException { |
| 84 | + return (new Pager<>(this, UncycloPage.class, itemsPerPage, null, "projects", projectId, "wikis")); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get a single page of project wiki. |
| 89 | + * |
| 90 | + * GET /projects/:id/wikis/:slug |
| 91 | + * |
| 92 | + * @param projectId the project ID to get the wiki page for |
| 93 | + * @param slug the slug of the project's wiki page |
| 94 | + * @return the specified project Snippet |
| 95 | + * @throws GitLabApiException if any exception occurs |
| 96 | + */ |
| 97 | + public UncycloPage getPage(Integer projectId, String slug) throws GitLabApiException { |
| 98 | + Response response = get(Response.Status.OK, null, "projects", projectId, "wikis", slug); |
| 99 | + return (response.readEntity(UncycloPage.class)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get a single page of project wiki as an Optional instance. |
| 104 | + * |
| 105 | + * GET /projects/:id/wikis/:slug |
| 106 | + * |
| 107 | + * @param projectId the project ID to get the snippet for |
| 108 | + * @param slug the slug of the project's wiki page |
| 109 | + * @return the specified project Snippet as an Optional instance |
| 110 | + */ |
| 111 | + public Optional<UncycloPage> getOptionalPage(Integer projectId, String slug) { |
| 112 | + try { |
| 113 | + return (Optional.ofNullable(getPage(projectId, slug))); |
| 114 | + } catch (GitLabApiException glae) { |
| 115 | + return (GitLabApi.createOptionalFromException(glae)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Creates a new project wiki page. The user must have permission to create new wiki page. |
| 121 | + * |
| 122 | + * POST /projects/:id/wikis |
| 123 | + * |
| 124 | + * @param projectId the ID of the project owned by the authenticated user, required |
| 125 | + * @param title the title of a snippet, required |
| 126 | + * @param content the content of a wiki page, required |
| 127 | + * @return a UncycloPage instance with info on the created page |
| 128 | + * @throws GitLabApiException if any exception occurs |
| 129 | + */ |
| 130 | + public UncycloPage createPage(Integer projectId, String title, String content) throws GitLabApiException { |
| 131 | + // one of title or content is required |
| 132 | + GitLabApiForm formData = new GitLabApiForm() |
| 133 | + .withParam("title", title) |
| 134 | + .withParam("content", content); |
| 135 | + |
| 136 | + Response response = post(Response.Status.CREATED, formData, "projects", projectId, "wikis"); |
| 137 | + return (response.readEntity(UncycloPage.class)); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Updates an existing project wiki page. The user must have permission to change an existing wiki page. |
| 142 | + * |
| 143 | + * PUT /projects/:id/wikis/:slug |
| 144 | + * |
| 145 | + * @param projectId the ID of the project owned by the authenticated user, required |
| 146 | + * @param slug the slug of the project's wiki page, required |
| 147 | + * @param title the title of a snippet, optional |
| 148 | + * @param content the content of a page, optional. Either title or content must be supplied. |
| 149 | + * @return a UncycloPage instance with info on the updated page |
| 150 | + * @throws GitLabApiException if any exception occurs |
| 151 | + */ |
| 152 | + public UncycloPage updatePage(Integer projectId, String slug, String title, String content) throws GitLabApiException { |
| 153 | + |
| 154 | + GitLabApiForm formData = new GitLabApiForm() |
| 155 | + .withParam("title", title) |
| 156 | + .withParam("slug", slug, true) |
| 157 | + .withParam("content", content); |
| 158 | + |
| 159 | + Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "wikis", slug); |
| 160 | + return (response.readEntity(UncycloPage.class)); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Deletes an existing project wiki page. This is an idempotent function and deleting a non-existent page does |
| 165 | + * not cause an error. |
| 166 | + * |
| 167 | + * DELETE /projects/:id/wikis/:slug |
| 168 | + * |
| 169 | + * @param projectId the project ID |
| 170 | + * @param slug the slug of the project's wiki page |
| 171 | + * @throws GitLabApiException if any exception occurs |
| 172 | + */ |
| 173 | + public void deletePage(Integer projectId, String slug) throws GitLabApiException { |
| 174 | + delete(Response.Status.NO_CONTENT, null, "projects", projectId, "wikis", slug); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments