|
10 | 10 | from sdk.exceptions import CoolsmsSystemException |
11 | 11 | from sdk.exceptions import CoolsmsServerException |
12 | 12 |
|
13 | | -# class Image |
| 13 | +## @class Image |
| 14 | +# @brief management image, using Rest API |
14 | 15 | class Image: |
15 | | - # |
| 16 | + # Coolsms Object |
| 17 | + cool = None |
| 18 | + |
| 19 | + ## @brief initialize |
| 20 | + # @param string api_key [required] |
| 21 | + # @param string api_secret [required] |
16 | 22 | def __init__(self, api_key, api_secret): |
17 | | - Coolsms(api_key, api_secret) |
| 23 | + self.cool = Coolsms(api_key, api_secret) |
| 24 | + |
| 25 | + # @brief get image list( HTTP Method GET ) |
| 26 | + # @param string offset [optional] |
| 27 | + # @param string limit [optional] |
| 28 | + # @return JSONObject |
| 29 | + def get_image_list(self, offset=None, limit=None): |
| 30 | + params = dict() |
| 31 | + |
| 32 | + if offset: |
| 33 | + params['offset'] = offset |
| 34 | + if limit: |
| 35 | + params['limit'] = limit |
| 36 | + |
| 37 | + response = self.cool.request_get('image_list', params) |
| 38 | + |
| 39 | + return response |
| 40 | + |
| 41 | + # @brief get image info ( HTTP Method GET ) |
| 42 | + # @param string image_id [required] |
| 43 | + # @return JSONObject |
| 44 | + # @throws CoolsmsException |
| 45 | + def get_image_info(self, image_id): |
| 46 | + if image_id == None: |
| 47 | + raise CoolsmsSDKException("'image_id' is required", 201); |
| 48 | + |
| 49 | + resource = "images/" + image_id; |
| 50 | + response = self.cool.request_get(resource) |
| 51 | + |
| 52 | + return response |
| 53 | + |
| 54 | + # @brief upload image ( HTTP Method POST ) |
| 55 | + # @param string image [required] |
| 56 | + # @param string encoding [optional] |
| 57 | + # @return JSONobject |
| 58 | + def upload_image(self, image, encoding=None): |
| 59 | + if image == None: |
| 60 | + raise CoolsmsSDKException("'image' is required", 201); |
| 61 | + |
| 62 | + params = dict() |
| 63 | + params = {'image':image} |
| 64 | + if encoding: |
| 65 | + params['encoding'] = encoding |
| 66 | + |
| 67 | + files = {} |
| 68 | + |
| 69 | + try: |
| 70 | + with open(params['image'], 'rb') as content_file: |
| 71 | + content = content_file.read() |
| 72 | + except Exception as e: |
| 73 | + raise CoolsmsSystemException(e, 399) |
| 74 | + |
| 75 | + files = {'image': {'filename': image, 'content': content}} |
18 | 76 |
|
19 | | - # |
20 | | - def get_image_list(self): |
21 | | - return |
| 77 | + response = self.cool.request_post_multipart("upload_image", params, files) |
22 | 78 |
|
23 | | - # |
24 | | - def get_image_info(self): |
25 | | - return |
| 79 | + return response |
26 | 80 |
|
27 | | - # |
28 | | - def upload_image(self): |
29 | | - return |
| 81 | + # @brief delete images ( HTTP Method POST ) |
| 82 | + # @param string image_ids [required] |
| 83 | + # @return JSONObject |
| 84 | + def delete_images(self, image_ids): |
| 85 | + if image_ids == None: |
| 86 | + raise CoolsmsSDKException("'image_ids' is required", 201); |
| 87 | + |
| 88 | + params = dict() |
| 89 | + params = {'image_ids':image_ids} |
| 90 | + response = self.cool.request_post('delete_images', params) |
30 | 91 |
|
31 | | - # |
32 | | - def delete_images(self): |
33 | | - return |
| 92 | + return response |
0 commit comments