|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Greg Messner <greg@messners.com> |
| 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 java.util.List; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import javax.ws.rs.core.GenericType; |
| 30 | +import javax.ws.rs.core.Response; |
| 31 | + |
| 32 | +import org.gitlab4j.api.models.Package; |
| 33 | +import org.gitlab4j.api.models.PackageFile; |
| 34 | + |
| 35 | +/** |
| 36 | + * <p>This class implements the client side API for the GitLab Packages API. |
| 37 | + * See <a href="https://docs.gitlab.com/ee/api/packages.html">Packages API at GitLab</a> for more information.</p> |
| 38 | + * |
| 39 | + * NOTE: This API is not available in the Community edition of GitLab. |
| 40 | + */ |
| 41 | +public class PackagesApi extends AbstractApi { |
| 42 | + |
| 43 | + public PackagesApi(GitLabApi gitLabApi) { |
| 44 | + super(gitLabApi); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get a list of project packages. Both Maven and NPM packages are included in results. |
| 49 | + * When accessed without authentication, only packages of public projects are returned. |
| 50 | + * |
| 51 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre> |
| 52 | + * |
| 53 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 54 | + * @return a list of pages in the project's packages |
| 55 | + * @throws GitLabApiException if any exception occurs |
| 56 | + */ |
| 57 | + public List<Package> getPackages(Object projectIdOrPath) throws GitLabApiException { |
| 58 | + return (getPackages(projectIdOrPath, getDefaultPerPage()).all()); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get a list of project packages for the specified page. Both Maven and NPM packages are included in results. |
| 63 | + * When accessed without authentication, only packages of public projects are returned. |
| 64 | + * |
| 65 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre> |
| 66 | + * |
| 67 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 68 | + * @param page the page to get |
| 69 | + * @param perPage the number of Package instances per page |
| 70 | + * @return a list of project packages for the specified range |
| 71 | + * @throws GitLabApiException if any exception occurs |
| 72 | + */ |
| 73 | + public List<Package> getPackages(Object projectIdOrPath, int page, int perPage) throws GitLabApiException { |
| 74 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), |
| 75 | + "projects", getProjectIdOrPath(projectIdOrPath), "packages"); |
| 76 | + return response.readEntity(new GenericType<List<Package>>() {}); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get a Pager of project packages. Both Maven and NPM packages are included in results. |
| 81 | + * When accessed without authentication, only packages of public projects are returned. |
| 82 | + * |
| 83 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre> |
| 84 | + * |
| 85 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 86 | + * @param itemsPerPage the number of Package instances per page |
| 87 | + * @return a Pager of project packages for the specified range |
| 88 | + * @throws GitLabApiException if any exception occurs |
| 89 | + */ |
| 90 | + public Pager<Package> getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { |
| 91 | + return (new Pager<Package>(this, Package.class, itemsPerPage, null, |
| 92 | + "projects", getProjectIdOrPath(projectIdOrPath), "packages")); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get a Stream of project packages. Both Maven and NPM packages are included in results. |
| 97 | + * When accessed without authentication, only packages of public projects are returned. |
| 98 | + * |
| 99 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre> |
| 100 | + * |
| 101 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 102 | + * @return a Stream of pages in the project's packages |
| 103 | + * @throws GitLabApiException if any exception occurs |
| 104 | + */ |
| 105 | + public Stream<Package> getPackagesStream(Object projectIdOrPath) throws GitLabApiException { |
| 106 | + return (getPackages(projectIdOrPath, getDefaultPerPage()).stream()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get a single project package. |
| 111 | + * |
| 112 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id</code></pre> |
| 113 | + * |
| 114 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 115 | + * @param packageId the ID of the package to get |
| 116 | + * @return a Package instance for the specified package ID |
| 117 | + * @throws GitLabApiException if any exception occurs |
| 118 | + */ |
| 119 | + public Package getPackage(Object projectIdOrPath, Integer packageId) throws GitLabApiException { |
| 120 | + Response response = get(Response.Status.OK, null, |
| 121 | + "projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId); |
| 122 | + return (response.readEntity(Package.class)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get a list of package files of a single package. |
| 127 | + * |
| 128 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre> |
| 129 | + * |
| 130 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 131 | + * @param packageId the ID of the package to get the package files for |
| 132 | + * @return a list of PackageFile instances for the specified package ID |
| 133 | + * @throws GitLabApiException if any exception occurs |
| 134 | + */ |
| 135 | + public List<PackageFile> getPackageFiles(Object projectIdOrPath, Integer packageId) throws GitLabApiException { |
| 136 | + return (getPackageFiles(projectIdOrPath, packageId, getDefaultPerPage()).all()); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Get a list of package files of a single package for the specified page. |
| 141 | + * |
| 142 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre> |
| 143 | + * |
| 144 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 145 | + * @param packageId the ID of the package to get the package files for |
| 146 | + * @param page the page to get |
| 147 | + * @param perPage the number of PackageFile instances per page |
| 148 | + * @return a list of PackageFile instances for the specified package ID |
| 149 | + * @throws GitLabApiException if any exception occurs |
| 150 | + */ |
| 151 | + public List<PackageFile> getPackageFiles(Object projectIdOrPath, Integer packageId, int page, int perPage) throws GitLabApiException { |
| 152 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), |
| 153 | + "projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId, "package_files"); |
| 154 | + return response.readEntity(new GenericType<List<PackageFile>>() {}); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Get a Pager of project package files. |
| 159 | + * |
| 160 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre> |
| 161 | + * |
| 162 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 163 | + * @param packageId the ID of the package to get the package files for |
| 164 | + * @param itemsPerPage the number of PackageFile instances per page |
| 165 | + * @return a Pager of PackageFile instances for the specified package ID |
| 166 | + * @throws GitLabApiException if any exception occurs |
| 167 | + */ |
| 168 | + public Pager<PackageFile> getPackageFiles(Object projectIdOrPath, Integer packageId, int itemsPerPage) throws GitLabApiException { |
| 169 | + return (new Pager<PackageFile>(this, PackageFile.class, itemsPerPage, null, |
| 170 | + "projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId, "package_files")); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Get a Stream of project package files. |
| 175 | + * |
| 176 | + * <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre> |
| 177 | + * |
| 178 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 179 | + * @param packageId the ID of the package to get the package files for |
| 180 | + * @return a Stream of PackageFile instances for the specified package ID |
| 181 | + * @throws GitLabApiException if any exception occurs |
| 182 | + */ |
| 183 | + public Stream<PackageFile> getPackagesStream(Object projectIdOrPath, Integer packageId) throws GitLabApiException { |
| 184 | + return (getPackageFiles(projectIdOrPath, packageId, getDefaultPerPage()).stream()); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Deletes a project package. |
| 189 | + * |
| 190 | + * <pre><code>GitLab Endpoint: DELETE /projects/:id/packages/:package_id</code></pre> |
| 191 | + * |
| 192 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 193 | + * @param packageId the ID of the package to delete |
| 194 | + * @throws GitLabApiException if any exception occurs |
| 195 | + */ |
| 196 | + public void deletePackage(Object projectIdOrPath, Integer packageId) throws GitLabApiException { |
| 197 | + |
| 198 | + if (packageId == null) { |
| 199 | + throw new RuntimeException("packageId cannot be null"); |
| 200 | + } |
| 201 | + |
| 202 | + delete(Response.Status.NO_CONTENT, null,"projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId); |
| 203 | + } |
| 204 | +} |
0 commit comments