Update etsicatalog code to get notification message from sdc
[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 requests.packages.urllib3.disable_warnings()
23 logger = logging.getLogger(__name__)
24
25
26 class IdentityClient:
27     def __init__(self, base_url):
28         self.base_url = base_url
29
30     def create_apikey(self, email, description):
31         try:
32             headers = {'content-type': 'application/json;charset=UTF-8'}
33             data = {
34                 'email': email,
35                 'description': description
36             }
37             data = json.JSONEncoder().encode(data)
38             url = self.base_url + "/apiKeys/create"
39             ret = requests.post(url=url, data=data, headers=headers, verify=False)
40             logger.info('create apiKey, response status_code: %s, body: %s', ret.status_code, ret.json())
41             if ret.status_code != 200:
42                 raise DmaapClientException(ret.json())
43             ret = ret.json()
44             resp_data = {
45                 'apiKey': ret.get('key', ''),
46                 'apiSecret': ret.get('secret', '')
47             }
48             return resp_data
49         except Exception as e:
50             raise DmaapClientException('create apikey from dmaap failed: ' + e.message)
51
52     def get_apikey(self, apikey):
53         try:
54             url = self.base_url + "/apiKeys/%s" % apikey
55             ret = requests.get(url)
56             logger.info('get apiKey, response status_code: %s, body: %s', ret.status_code, ret.json())
57             if ret.status_code != 200:
58                 raise DmaapClientException(ret.json())
59             ret = ret.json()
60             return ret
61         except Exception as e:
62             raise DmaapClientException('get apikey from dmaap failed: ' + e.message)
63
64     def delete_apikey(self):
65         pass
66
67     def update_apikey(self, apikey, email, description):
68         pass