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