|
| 1 | +# ------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2024 Siemens |
| 3 | +# All Rights Reserved. |
| 4 | +# Authors: thomas.graf@siemens.com |
| 5 | +# |
| 6 | +# Licensed under the MIT license. |
| 7 | +# SPDX-License-Identifier: MIT |
| 8 | +# ------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +from typing import Any, Dict, List, Optional |
| 11 | + |
| 12 | +from .base import BaseMixin |
| 13 | +from .sw360error import SW360Error |
| 14 | + |
| 15 | + |
| 16 | +class PackagesMixin(BaseMixin): |
| 17 | + def get_package(self, package_id: str) -> Optional[Dict[str, Any]]: |
| 18 | + """Get information of about a package |
| 19 | +
|
| 20 | + API endpoint: GET /package/{id} |
| 21 | +
|
| 22 | + :param package_id: the id of the package to be requested |
| 23 | + :type package_id: string |
| 24 | + :return: a package |
| 25 | + :rtype: JSON package object |
| 26 | + :raises SW360Error: if there is a negative HTTP response |
| 27 | + """ |
| 28 | + resp = self.api_get(self.url + "resource/api/packages/" + package_id) |
| 29 | + return resp |
| 30 | + |
| 31 | + def get_packages_by_name(self, name: str) -> List[Any]: |
| 32 | + """Gets a list of packages that match the given name. |
| 33 | +
|
| 34 | + API endpoint: GET /packages?name= |
| 35 | +
|
| 36 | + :param name: the name |
| 37 | + :type name: string |
| 38 | + :return: list of packages |
| 39 | + :rtype: list of JSON package objects |
| 40 | + :raises SW360Error: if there is a negative HTTP response |
| 41 | + """ |
| 42 | + full_url = self.url + "resource/api/packages?name=" + name |
| 43 | + resp = self.api_get(full_url) |
| 44 | + if resp and ("_embedded" in resp) and ("sw360:packages" in resp["_embedded"]): |
| 45 | + return resp["_embedded"]["sw360:packages"] |
| 46 | + |
| 47 | + return [] |
| 48 | + |
| 49 | + # return type List[Dict[str, Any]] | Optional[Dict[str, Any]] for Python 3.11 is good, |
| 50 | + # Union[List[Dict[str, Any]], Optional[Dict[str, Any]]] for lower Python versions is not good |
| 51 | + def get_all_packages(self, fields: str = "", all_details: bool = False, page: int = -1, |
| 52 | + page_size: int = -1, sort: str = "") -> Any: |
| 53 | + """Get information of about all packages |
| 54 | +
|
| 55 | + API endpoint: GET /releases |
| 56 | +
|
| 57 | + :param all_details: retrieve all package details (optional)) |
| 58 | + :type all_details: bool |
| 59 | + :param page: page to retrieve |
| 60 | + :type page: int |
| 61 | + :param page_size: page size to use |
| 62 | + :type page_size: int |
| 63 | + :param sort: sort order for the packages ("name,desc"; "name,asc") |
| 64 | + :type sort: str |
| 65 | + :return: list of packages |
| 66 | + :rtype: list of JSON package objects |
| 67 | + :raises SW360Error: if there is a negative HTTP response |
| 68 | + """ |
| 69 | + full_url = self.url + "resource/api/packages" |
| 70 | + if all_details: |
| 71 | + full_url = self._add_param(full_url, "allDetails=true") |
| 72 | + |
| 73 | + if fields: |
| 74 | + full_url = self._add_param(full_url, "fields=" + fields) |
| 75 | + |
| 76 | + if page > -1: |
| 77 | + full_url = self._add_param(full_url, "page=" + str(page)) |
| 78 | + full_url = self._add_param(full_url, "page_entries=" + str(page_size)) |
| 79 | + |
| 80 | + if sort: |
| 81 | + # ensure HTML encoding |
| 82 | + sort = sort.replace(",", "%2C") |
| 83 | + full_url = self._add_param(full_url, "sort=" + sort) |
| 84 | + |
| 85 | + resp = self.api_get(full_url) |
| 86 | + |
| 87 | + if page == -1 and resp and ("_embedded" in resp) and ("sw360:packages" in resp["_embedded"]): |
| 88 | + return resp["_embedded"]["sw360:packages"] |
| 89 | + |
| 90 | + return resp |
| 91 | + |
| 92 | + def get_packages_by_packagemanager(self, manager: str, page: int = -1, |
| 93 | + page_size: int = -1, sort: str = "") -> Any: |
| 94 | + """Get information of about all packages of a specific package manager |
| 95 | +
|
| 96 | + API endpoint: GET /releases |
| 97 | +
|
| 98 | + :param manager: name of the package manager |
| 99 | + :type manager: str |
| 100 | + :param page: page to retrieve |
| 101 | + :type page: int |
| 102 | + :param page_size: page size to use |
| 103 | + :type page_size: int |
| 104 | + :param sort: sort order for the packages ("name,desc"; "name,asc") |
| 105 | + :type sort: str |
| 106 | + :return: list of packages |
| 107 | + :rtype: list of JSON package objects |
| 108 | + :raises SW360Error: if there is a negative HTTP response |
| 109 | + """ |
| 110 | + full_url = self.url + "resource/api/packages" |
| 111 | + full_url = self._add_param(full_url, "packageManager=" + str(manager)) |
| 112 | + |
| 113 | + if page > -1: |
| 114 | + full_url = self._add_param(full_url, "page=" + str(page)) |
| 115 | + full_url = self._add_param(full_url, "page_entries=" + str(page_size)) |
| 116 | + |
| 117 | + if sort: |
| 118 | + # ensure HTML encoding |
| 119 | + sort = sort.replace(",", "%2C") |
| 120 | + full_url = self._add_param(full_url, "sort=" + sort) |
| 121 | + |
| 122 | + resp = self.api_get(full_url) |
| 123 | + |
| 124 | + if page == -1 and resp and ("_embedded" in resp) and ("sw360:packages" in resp["_embedded"]): |
| 125 | + return resp["_embedded"]["sw360:packages"] |
| 126 | + |
| 127 | + return resp |
| 128 | + |
| 129 | + def create_new_package(self, name: str, version: str, purl: str, |
| 130 | + package_type: str, package_details: Dict[str, Any] = {}) -> Optional[Dict[str, Any]]: |
| 131 | + """Create a new package |
| 132 | +
|
| 133 | + API endpoint: POST /packages |
| 134 | +
|
| 135 | + :param name: name of new package (usually set to component name) |
| 136 | + :param version: version string of new package (e.g. "1.0") |
| 137 | + :param purl: purl / package-url of the package |
| 138 | + :param package_type: CycloneDX package type of the package |
| 139 | + :param package_details: further package details as defined by SW360 REST API |
| 140 | + :type name: string |
| 141 | + :type version: string |
| 142 | + :type purl: string |
| 143 | + :type package_type: string |
| 144 | + :type package_details: dict |
| 145 | + :return: SW360 result |
| 146 | + :rtype: JSON SW360 result object |
| 147 | + :raises SW360Error: if there is a negative HTTP response |
| 148 | + """ |
| 149 | + |
| 150 | + for param in "name", "version": |
| 151 | + package_details[param] = locals()[param] |
| 152 | + package_details["purl"] = purl |
| 153 | + package_details["packageType"] = package_type |
| 154 | + |
| 155 | + url = self.url + "resource/api/packages" |
| 156 | + response = self.api_post( |
| 157 | + url, json=package_details) |
| 158 | + if response is not None: |
| 159 | + if response.ok: |
| 160 | + return response.json() |
| 161 | + return None |
| 162 | + |
| 163 | + def update_package(self, package: Dict[str, Any], package_id: str) -> Optional[Dict[str, Any]]: |
| 164 | + """Update an existing package |
| 165 | +
|
| 166 | + API endpoint: PATCH /packages |
| 167 | +
|
| 168 | + :param release: the new package data |
| 169 | + :param release_id: the id of the package to be updated |
| 170 | + :type package: JSON |
| 171 | + :type package_id: string |
| 172 | + :return: SW360 result |
| 173 | + :rtype: JSON SW360 result object |
| 174 | + :raises SW360Error: if there is a negative HTTP response |
| 175 | + """ |
| 176 | + |
| 177 | + if not package_id: |
| 178 | + raise SW360Error(message="No package id provided!") |
| 179 | + |
| 180 | + url = self.url + "resource/api/packages/" + package_id |
| 181 | + return self.api_patch(url, json=package) |
| 182 | + |
| 183 | + def delete_package(self, package_id: str) -> Optional[Dict[str, Any]]: |
| 184 | + """Delete an existing package |
| 185 | +
|
| 186 | + API endpoint: DELETE /packages |
| 187 | +
|
| 188 | + :param package_id: the id of the package to be deleted |
| 189 | + :type package_id: string |
| 190 | + :return: SW360 result |
| 191 | + :rtype: JSON SW360 result object |
| 192 | + :raises SW360Error: if there is a negative HTTP response |
| 193 | + """ |
| 194 | + |
| 195 | + if not package_id: |
| 196 | + raise SW360Error(message="No package id provided!") |
| 197 | + |
| 198 | + url = self.url + "resource/api/packages/" + package_id |
| 199 | + response = self.api_delete(url) |
| 200 | + if response is not None: |
| 201 | + if response.ok: |
| 202 | + if response.text: |
| 203 | + return response.json() |
| 204 | + return None |
0 commit comments