|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import org.gitlab4j.api.models.Topic; |
| 4 | +import org.gitlab4j.api.models.TopicParams; |
| 5 | + |
| 6 | +import javax.ws.rs.core.GenericType; |
| 7 | +import javax.ws.rs.core.Response; |
| 8 | +import java.io.File; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +public class TopicsApi extends AbstractApi{ |
| 14 | + |
| 15 | + public TopicsApi(GitLabApi gitLabApi) { |
| 16 | + super(gitLabApi); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * <p>Get a list of Topics. </p> |
| 21 | + * |
| 22 | + * <strong>WARNING:</strong> Do not use this method to fetch Topics from https://gitlab.com, |
| 23 | + * gitlab.com has many 1,000's of public topics and it will a long time to fetch all of them. |
| 24 | + * Instead use {@link #getTopics(int itemsPerPage)} which will return a Pager of Topic instances. |
| 25 | + * |
| 26 | + * <pre><code>GitLab Endpoint: GET /topics</code></pre> |
| 27 | + * |
| 28 | + * @return the list of topics viewable by the authenticated user |
| 29 | + * @throws GitLabApiException if any exception occurs |
| 30 | + */ |
| 31 | + public List<Topic> getTopics() throws GitLabApiException { |
| 32 | + return (getTopics(getDefaultPerPage()).all()); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get a list of topics in the specified page range. |
| 37 | + * |
| 38 | + * <pre><code>GitLab Endpoint: GET /topics</code></pre> |
| 39 | + * |
| 40 | + * @param page the page to get |
| 41 | + * @param perPage the number of Topic instances per page |
| 42 | + * @return the list of topics |
| 43 | + * @throws GitLabApiException if any exception occurs |
| 44 | + */ |
| 45 | + public List<Topic> getTopics(int page, int perPage) throws GitLabApiException { |
| 46 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "topics"); |
| 47 | + return (response.readEntity(new GenericType<List<Topic>>() {})); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get a Pager of topics. |
| 52 | + * |
| 53 | + * <pre><code>GitLab Endpoint: GET /topics</code></pre> |
| 54 | + * |
| 55 | + * @param itemsPerPage the number of Topic instances that will be fetched per page |
| 56 | + * @return the pager of topics |
| 57 | + * @throws GitLabApiException if any exception occurs |
| 58 | + */ |
| 59 | + public Pager<Topic> getTopics(int itemsPerPage) throws GitLabApiException { |
| 60 | + return (new Pager<Topic>(this, Topic.class, itemsPerPage, null, "topics")); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get a Stream of topics. |
| 65 | + * |
| 66 | + * <pre><code>GitLab Endpoint: GET /topics</code></pre> |
| 67 | + * |
| 68 | + * @return the stream of topics |
| 69 | + * @throws GitLabApiException if any exception occurs |
| 70 | + */ |
| 71 | + public Stream<Topic> getTopicsStream() throws GitLabApiException { |
| 72 | + return (getTopics(getDefaultPerPage()).stream()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get all details of a topic. |
| 77 | + * |
| 78 | + * <pre><code>GitLab Endpoint: GET /topics/:id</code></pre> |
| 79 | + * |
| 80 | + * @param id the topic ID |
| 81 | + * @return the topic for the specified topic id |
| 82 | + * @throws GitLabApiException if any exception occurs |
| 83 | + */ |
| 84 | + public Topic getTopic(Integer id) throws GitLabApiException { |
| 85 | + Response response = get(Response.Status.OK, null, "topics", id); |
| 86 | + return (response.readEntity(Topic.class)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get all details of a topic as an Optional instance. |
| 91 | + * |
| 92 | + * <pre><code>GitLab Endpoint: GET /topics/:id</code></pre> |
| 93 | + * |
| 94 | + * @param id the topic ID |
| 95 | + * @return the Topic for the specified topic id as an Optional instance |
| 96 | + */ |
| 97 | + public Optional<Topic> getOptionalTopic(Integer id) { |
| 98 | + try { |
| 99 | + return (Optional.ofNullable(getTopic(id))); |
| 100 | + } catch (GitLabApiException glae) { |
| 101 | + return (GitLabApi.createOptionalFromException(glae)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Creates a new Topic. Available only for users who can create topics. |
| 107 | + * |
| 108 | + * <pre><code>GitLab Endpoint: POST /topics</code></pre> |
| 109 | + * |
| 110 | + * @param params a TopicParams instance holding the parameters for the topic creation |
| 111 | + * @return the created Topic instance |
| 112 | + * @throws GitLabApiException if any exception occurs |
| 113 | + */ |
| 114 | + public Topic createTopic(TopicParams params) throws GitLabApiException { |
| 115 | + Response response = post(Response.Status.CREATED, params.getForm(true), "topics"); |
| 116 | + return (response.readEntity(Topic.class)); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Update a project topic. |
| 121 | + * |
| 122 | + * <pre><code>GitLab Endpoint: PUT /topics/:id</code></pre> |
| 123 | + * |
| 124 | + * @param id the topic id |
| 125 | + * @param params a TopicParams instance holding the properties to Update |
| 126 | + * @return the updated Topic instance |
| 127 | + * @throws GitLabApiException at any exception |
| 128 | + */ |
| 129 | + public Topic updateTopic(Integer id, TopicParams params) throws GitLabApiException { |
| 130 | + Response response = putWithFormData(Response.Status.OK, |
| 131 | + params.getForm(false), "topics", id); |
| 132 | + return (response.readEntity(Topic.class)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Uploads and sets the topic's avatar for the specified topic. |
| 137 | + * |
| 138 | + * <pre><code>GitLab Endpoint: PUT /topics/:id</code></pre> |
| 139 | + * |
| 140 | + * @param id the topic in the form of an Integer |
| 141 | + * @param avatarFile the File instance of the avatar file to upload |
| 142 | + * @return the updated Topic instance |
| 143 | + * @throws GitLabApiException if any exception occurs |
| 144 | + */ |
| 145 | + public Topic updateTopicAvatar(final Integer id, File avatarFile) throws GitLabApiException { |
| 146 | + Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "topics", id); |
| 147 | + return (response.readEntity(Topic.class)); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Delete the topic's avatar for the specified topic. |
| 152 | + * |
| 153 | + * <pre><code>GitLab Endpoint: PUT /topics/:id</code></pre> |
| 154 | + * |
| 155 | + * @param id the topic in the form of an Integer |
| 156 | + * @return the updated Topic instance |
| 157 | + * @throws GitLabApiException if any exception occurs |
| 158 | + */ |
| 159 | + public Topic deleteTopicAvatar(final Integer id) throws GitLabApiException { |
| 160 | + Response response = putUpload(Response.Status.OK, "avatar", null, "topics", id); |
| 161 | + return (response.readEntity(Topic.class)); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Delete a topic. You must be an administrator to delete a project topic. When you delete a project topic, you also delete the topic assignment for projects. |
| 166 | + * |
| 167 | + * <pre><code>GitLab Endpoint: DELETE /topics/:id</code></pre> |
| 168 | + * |
| 169 | + * @param id the topic to deleted in the form of an Integer |
| 170 | + * @throws GitLabApiException if any exception occurs |
| 171 | + */ |
| 172 | + public void deleteTopic(Integer id) throws GitLabApiException { |
| 173 | + if(isApiVersion(GitLabApi.ApiVersion.V3)){ |
| 174 | + throw new GitLabApiException("Topics need api v4+"); |
| 175 | + } |
| 176 | + delete(Response.Status.NO_CONTENT,null, "topics", id); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Merge two topics together. You must be an administrator to merge a source topic into a target topic. When you merge topics, you delete the source topic and move all assigned projects to the target topic. |
| 181 | + * |
| 182 | + * <pre><code>GitLab Endpoint: POST /topics/merge</code></pre> |
| 183 | + * |
| 184 | + * @param sourceTopicId ID of source project topic |
| 185 | + * @param targetTopicId ID of target project topic |
| 186 | + * @return the merged Topic instance |
| 187 | + * @throws GitLabApiException if any exception occurs |
| 188 | + */ |
| 189 | + public Topic mergeTopics(Integer sourceTopicId, Integer targetTopicId) throws GitLabApiException { |
| 190 | + Response response = post(Response.Status.OK,new GitLabApiForm().withParam("source_topic_id",sourceTopicId).withParam("target_topic_id",targetTopicId),"topics/merge"); |
| 191 | + return (response.readEntity(Topic.class)); |
| 192 | + } |
| 193 | +} |
0 commit comments