update Dmaap lib
[modeling/etsicatalog.git] / catalog / pub / Dmaap_lib / dmaap / identity.py
1 # Copyright (c) 2019, CMCC Technologies. Co., Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16 import logging
17
18 import requests
19
20 from catalog.pub.Dmaap_lib.pub.exceptions import DmaapClientException
21
22 logger = logging.getLogger(__name__)
23
24
25 class IdentityClient:
26     def __init__(self, base_url):
27         self.base_url = base_url
28
29     def create_apikey(self, email, description):
30         try:
31             headers = {'content-type': 'application/json;charset=UTF-8'}
32             data = {
33                 'email': email,
34                 'description': description
35             }
36             data = json.JSONEncoder().encode(data)
37             url = self.base_url + "/apiKeys/create"
38             ret = requests.post(url=url, data=data, headers=headers)
39             logger.info('create apiKey, response status_code: %s, body: %s', ret.status_code, ret.json())
40             if ret.status_code != 200:
41                 raise DmaapClientException(ret.json())
42             ret = ret.json()
43             resp_data = {
44                 'apiKey': ret.get('key', ''),
45                 'apiSecret': ret.get('secret', '')
46             }
47             return resp_data
48         except Exception as e:
49             raise DmaapClientException('create apikey from dmaap failed: ' + e.message)
50
51     def get_apikey(self, apikey):
52         try:
53             url = self.base_url + "/apiKeys/%s" % apikey
54             ret = requests.get(url)
55             logger.info('get apiKey, response status_code: %s, body: %s', ret.status_code, ret.json())
56             if ret.status_code != 200:
57                 raise DmaapClientException(ret.json())
58             ret = ret.json()
59             return ret
60         except Exception as e:
61             raise DmaapClientException('get apikey from dmaap failed: ' + e.message)
62
63     def delete_apikey(self):
64         pass
65
66     def update_apikey(self, apikey, email, description):
67         pass