|
3 | 3 | # This file is part of the Ingram Micro Cloud Blue Connect SDK. |
4 | 4 | # Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved. |
5 | 5 |
|
6 | | -from requests import RequestException |
7 | | - |
8 | 6 | from .base import BaseResource |
9 | | -from connect.exceptions import DeleteResourceError, ServerError, UpdateResourceError |
10 | 7 | from ..models import Marketplace |
11 | 8 |
|
12 | 9 |
|
13 | 10 | class MarketplaceResource(BaseResource): |
14 | 11 | """ Resource to work with :py:class:`connect.models.Marketplace` models. |
15 | 12 | :param Config config: Config object or ``None`` to use environment config (default). |
16 | 13 | """ |
17 | | - DELETE_ERR = 'Error deleting marketplace {}: {}' |
18 | | - UPLOAD_ERR = 'Error uploading icon for Marketplace {}: {}' |
19 | | - RESPONSE_ERR = 'Unexpected server response, returned code {} -- Raw response: {}' |
20 | 14 | resource = 'marketplaces' |
21 | 15 | model_class = Marketplace |
22 | 16 |
|
23 | 17 | def __init__(self, config=None): |
24 | 18 | super(MarketplaceResource, self).__init__(config) |
25 | | - |
26 | | - def set_icon(self, id_, path): |
27 | | - """ Sets or updates the Marketplace icon. |
28 | | -
|
29 | | - :param str id_: Id of the Marketplace. |
30 | | - :param str path: Path to the icon file that will be sent to Connect. |
31 | | - """ |
32 | | - icon = self._load_icon(id_, path) |
33 | | - request_path, headers, multipart = self._setup_icon_request(id_, path, icon) |
34 | | - self._post_icon_request(id_, request_path, headers, multipart) |
35 | | - |
36 | | - def _load_icon(self, id_, path): |
37 | | - # type: (str, str) -> bytes |
38 | | - try: |
39 | | - with open(path, 'rb') as f: |
40 | | - return f.read() |
41 | | - except IOError as ex: |
42 | | - raise UpdateResourceError(self.UPLOAD_ERR.format(id_, ex)) |
43 | | - |
44 | | - def _setup_icon_request(self, id_, path, icon): |
45 | | - # type: (str, str, bytes) -> (str, dict, dict) |
46 | | - request_path = self._api.urljoin(id_, 'icon') |
47 | | - headers = self._api.headers |
48 | | - headers['Accept'] = 'application/json' |
49 | | - del headers['Content-Type'] # This must NOT be set for multipart post requests |
50 | | - multipart = {'icon': (path, icon)} |
51 | | - self.logger.info('HTTP Request: {} - {} - {}'.format(request_path, headers, multipart)) |
52 | | - return request_path, headers, multipart |
53 | | - |
54 | | - def _post_icon_request(self, id_, path, headers, multipart): |
55 | | - # type: (str, str, dict, dict) -> None |
56 | | - try: |
57 | | - content, status = self._api.post( |
58 | | - path=path, |
59 | | - headers=headers, |
60 | | - files=multipart) |
61 | | - except (RequestException, ServerError) as ex: |
62 | | - raise UpdateResourceError(self.UPLOAD_ERR.format(id_, ex)) |
63 | | - self._raise_if_invalid_status(200, status, content, UpdateResourceError) |
64 | | - |
65 | | - def _raise_if_invalid_status(self, required, obtained, content, ex_class): |
66 | | - # type: (int, int, str, type) -> None |
67 | | - self.logger.info('HTTP Code: {}'.format(obtained)) |
68 | | - if obtained != required: |
69 | | - msg = self.RESPONSE_ERR.format(obtained, content) |
70 | | - self.logger.error(msg) |
71 | | - raise ex_class(msg) |
72 | | - |
73 | | - def delete(self, id_): |
74 | | - """ Deletes a Marketplace. |
75 | | -
|
76 | | - :param id_: Id of the Marketplace. |
77 | | - :return: |
78 | | - """ |
79 | | - try: |
80 | | - content, status = self._api.delete(path=id_) |
81 | | - except (RequestException, ServerError) as ex: |
82 | | - raise DeleteResourceError(self.DELETE_ERR.format(id_, ex)) |
83 | | - self._raise_if_invalid_status(204, status, content, DeleteResourceError) |
0 commit comments