|
7 | 7 | # SPDX-License-Identifier: MIT |
8 | 8 | # ------------------------------------------------------------------------------- |
9 | 9 |
|
10 | | -from typing import Any, Dict, Optional, Tuple |
| 10 | +from typing import Any, Dict, Optional, Tuple, Union, List |
11 | 11 |
|
12 | 12 | import requests |
13 | 13 |
|
@@ -73,6 +73,122 @@ def api_get(self, url: str = "") -> Optional[Dict[str, Any]]: |
73 | 73 |
|
74 | 74 | raise SW360Error(response, url) |
75 | 75 |
|
| 76 | + def api_post_multipart(self, url: str = "", files: Dict[str, Any] = {}) -> Optional[requests.Response]: |
| 77 | + """ |
| 78 | + Send a multipart POST request to the specified URL with the provided file data. |
| 79 | +
|
| 80 | + :param url: The URL to send the multipart POST request to. |
| 81 | + :type url: str |
| 82 | + :param files: The dictionary containing file data to be sent in the request. |
| 83 | + :type files: Dict[str, Any] |
| 84 | + :return: The JSON response received from the server, if any. |
| 85 | + :rtype: Optional[Dict[str, Any]] |
| 86 | + :raises SW360Error: If the HTTP response indicates an error. |
| 87 | + """ |
| 88 | + |
| 89 | + if (not self.force_no_session) and self.session is None: |
| 90 | + raise SW360Error(message="login_api needs to be called first") |
| 91 | + |
| 92 | + if self.force_no_session: |
| 93 | + response = requests.post(url, headers=self.api_headers, files=files) |
| 94 | + else: |
| 95 | + if self.session: |
| 96 | + response = self.session.post(url, files=files) |
| 97 | + |
| 98 | + if response.ok: |
| 99 | + if response.status_code == 204: # 204 = no content |
| 100 | + return None |
| 101 | + return response |
| 102 | + |
| 103 | + raise SW360Error(response, url) |
| 104 | + |
| 105 | + def api_post( |
| 106 | + self, |
| 107 | + url: str = "", |
| 108 | + json: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None |
| 109 | + ) -> Optional[requests.Response]: |
| 110 | + |
| 111 | + """ |
| 112 | + Send a POST request to the specified URL with the provided json data. |
| 113 | +
|
| 114 | + :param url: The URL to send the POST request to. |
| 115 | + :type url: str |
| 116 | + :param json: The dictionary containing json data to be sent in the request. |
| 117 | + :type json: Dict[str, Any] |
| 118 | + :return: The JSON response received from the server, if any. |
| 119 | + :rtype: Optional[Dict[str, Any]] |
| 120 | + :raises SW360Error: If the HTTP response indicates an error. |
| 121 | + """ |
| 122 | + |
| 123 | + if (not self.force_no_session) and self.session is None: |
| 124 | + raise SW360Error(message="login_api needs to be called first") |
| 125 | + |
| 126 | + if self.force_no_session: |
| 127 | + response = requests.post(url, headers=self.api_headers, json=json) |
| 128 | + else: |
| 129 | + if self.session: |
| 130 | + response = self.session.post(url, json=json) |
| 131 | + |
| 132 | + if response.ok: |
| 133 | + if response.status_code == 204: # 204 = no content |
| 134 | + return None |
| 135 | + return response |
| 136 | + |
| 137 | + raise SW360Error(response, url) |
| 138 | + |
| 139 | + def api_patch(self, url: str = "", json: Dict[str, Any] = {}) -> Optional[Dict[str, Any]]: |
| 140 | + """ |
| 141 | + Send a PATCH request to the specified URL with the provided json data. |
| 142 | +
|
| 143 | + :param url: The URL to send the PATCH request to. |
| 144 | + :type url: str |
| 145 | + :param json: The dictionary containing json data to be sent in the request. |
| 146 | + :type json: Dict[str, Any] |
| 147 | + :return: The JSON response received from the server, if any. |
| 148 | + :rtype: Optional[Dict[str, Any]] |
| 149 | + :raises SW360Error: If the HTTP response indicates an error. |
| 150 | + """ |
| 151 | + if (not self.force_no_session) and self.session is None: |
| 152 | + raise SW360Error(message="login_api needs to be called first") |
| 153 | + |
| 154 | + if self.force_no_session: |
| 155 | + response = requests.patch(url, headers=self.api_headers, json=json) |
| 156 | + else: |
| 157 | + if self.session: |
| 158 | + response = self.session.patch(url, json=json) |
| 159 | + |
| 160 | + if response.ok: |
| 161 | + if response.status_code == 204: # 204 = no content |
| 162 | + return None |
| 163 | + return response.json() |
| 164 | + |
| 165 | + raise SW360Error(response, url) |
| 166 | + |
| 167 | + def api_delete(self, url: str = "") -> Optional[requests.Response]: |
| 168 | + """Send a DELETE request to the specified `url` of the REST API and return JSON response. |
| 169 | +
|
| 170 | + :param url: The URL to which the DELETE request will be sent. |
| 171 | + :type url: str |
| 172 | + :return: JSON data returned by the API, or None if the response is empty. |
| 173 | + :rtype: Optional[Dict[str, Any]] |
| 174 | + :raises SW360Error: If the API responds with a non-success HTTP status code. |
| 175 | + """ |
| 176 | + if (not self.force_no_session) and self.session is None: |
| 177 | + raise SW360Error(message="login_api needs to be called first") |
| 178 | + |
| 179 | + if self.force_no_session: |
| 180 | + response = requests.delete(url, headers=self.api_headers) |
| 181 | + else: |
| 182 | + if self.session: |
| 183 | + response = self.session.delete(url) |
| 184 | + |
| 185 | + if response.ok: |
| 186 | + if response.status_code == 204: # 204 = no content |
| 187 | + return None |
| 188 | + return response |
| 189 | + |
| 190 | + raise SW360Error(response, url) |
| 191 | + |
76 | 192 | # type checking: not for Python 3.8: tuple[Optional[Any], Dict[str, Dict[str, str]], bool] |
77 | 193 | def _update_external_ids(self, current_data: Dict[str, Any], ext_id_name: str, ext_id_value: str, |
78 | 194 | update_mode: str) -> Tuple[Optional[Any], Dict[str, Dict[str, str]], bool]: |
|
0 commit comments