| 178 | |
| 179 | |
| 180 | class ResourceManager(object): |
| 181 | def __init__( |
| 182 | self, |
| 183 | resource: str, |
| 184 | endpoint: str, |
| 185 | cacert: Optional[str] = None, |
| 186 | debug: bool = False, |
| 187 | basic_auth: Optional[Tuple[str, str]] = None, |
| 188 | ): |
| 189 | """ |
| 190 | :param resource: Name of the resource to operate on. |
| 191 | :param endpoint: API endpoint URL. |
| 192 | :param cacert: Optional path to CA cert to use to validate the server side cert. |
| 193 | :param debug: True to enable debug mode where additional debug information will be logged. |
| 194 | :param basic_auth: Optional additional basic auth credentials in tuple(username, password) |
| 195 | notation. |
| 196 | """ |
| 197 | self.resource = resource |
| 198 | self.endpoint = endpoint |
| 199 | self.cacert = cacert |
| 200 | self.debug = debug |
| 201 | self.basic_auth = basic_auth |
| 202 | self.client = httpclient.HTTPClient( |
| 203 | endpoint, cacert=cacert, debug=debug, basic_auth=basic_auth |
| 204 | ) |
| 205 | |
| 206 | @staticmethod |
| 207 | def handle_error(response): |
| 208 | try: |
| 209 | content = parse_api_response(response) |
| 210 | fault = content.get("faultstring", "") if content else "" |
| 211 | if fault: |
| 212 | response.reason += "\nMESSAGE: %s" % fault |
| 213 | except Exception as e: |
| 214 | response.reason += ( |
| 215 | "\nUnable to retrieve detailed message " |
| 216 | "from the HTTP response. %s\n" % six.text_type(e) |
| 217 | ) |
| 218 | response.raise_for_status() |
| 219 | |
| 220 | @add_auth_token_to_kwargs_from_env |
| 221 | def get_all(self, **kwargs): |
| 222 | # TODO: This is ugly, stop abusing kwargs |
| 223 | url = "/%s" % self.resource.get_url_path_name() |
| 224 | limit = kwargs.pop("limit", None) |
| 225 | pack = kwargs.pop("pack", None) |
| 226 | prefix = kwargs.pop("prefix", None) |
| 227 | user = kwargs.pop("user", None) |
| 228 | offset = kwargs.pop("offset", 0) |
| 229 | |
| 230 | params = kwargs.pop("params", {}) |
| 231 | |
| 232 | if limit: |
| 233 | params["limit"] = limit |
| 234 | |
| 235 | if pack: |
| 236 | params["pack"] = pack |
| 237 | |