2016-02-11 15:27:08 +00:00
|
|
|
from notifications_python_client.base import BaseAPIClient
|
2016-04-15 11:08:19 +01:00
|
|
|
from app.notify_client import _attach_current_user
|
2016-01-20 17:32:55 +00:00
|
|
|
|
|
|
|
|
|
2016-06-27 12:02:16 +01:00
|
|
|
# must match key types in notifications-api/app/models.py
|
|
|
|
|
KEY_TYPE_NORMAL = 'normal'
|
|
|
|
|
KEY_TYPE_TEAM = 'team'
|
2016-07-06 15:10:36 +01:00
|
|
|
KEY_TYPE_TEST = 'test'
|
2016-06-27 12:02:16 +01:00
|
|
|
|
|
|
|
|
|
2016-01-20 17:32:55 +00:00
|
|
|
class ApiKeyApiClient(BaseAPIClient):
|
|
|
|
|
def __init__(self, base_url=None, client_id=None, secret=None):
|
|
|
|
|
super(self.__class__, self).__init__(base_url=base_url or 'base_url',
|
|
|
|
|
client_id=client_id or 'client_id',
|
|
|
|
|
secret=secret or 'secret')
|
|
|
|
|
|
|
|
|
|
def init_app(self, app):
|
|
|
|
|
self.base_url = app.config['API_HOST_NAME']
|
|
|
|
|
self.client_id = app.config['ADMIN_CLIENT_USER_NAME']
|
|
|
|
|
self.secret = app.config['ADMIN_CLIENT_SECRET']
|
|
|
|
|
|
2016-06-27 12:02:16 +01:00
|
|
|
def get_api_keys(self, service_id, key_id=None):
|
2016-01-21 12:28:05 +00:00
|
|
|
if key_id:
|
|
|
|
|
return self.get(url='/service/{}/api-keys/{}'.format(service_id, key_id))
|
|
|
|
|
else:
|
|
|
|
|
return self.get(url='/service/{}/api-keys'.format(service_id))
|
2016-01-20 17:32:55 +00:00
|
|
|
|
2016-07-06 15:10:36 +01:00
|
|
|
def create_api_key(self, service_id, key_name, key_type):
|
2016-06-27 12:02:16 +01:00
|
|
|
data = {
|
|
|
|
|
'name': key_name,
|
2016-07-06 15:10:36 +01:00
|
|
|
'key_type': key_type
|
2016-06-27 12:02:16 +01:00
|
|
|
}
|
2016-04-15 11:08:19 +01:00
|
|
|
_attach_current_user(data)
|
2016-01-20 17:32:55 +00:00
|
|
|
key = self.post(url='/service/{}/api-key'.format(service_id), data=data)
|
|
|
|
|
return key['data']
|
|
|
|
|
|
2016-06-27 12:02:16 +01:00
|
|
|
def revoke_api_key(self, service_id, key_id):
|
2016-04-15 11:08:19 +01:00
|
|
|
data = _attach_current_user({})
|
|
|
|
|
return self.post(
|
|
|
|
|
url='/service/{0}/api-key/revoke/{1}'.format(service_id, key_id),
|
|
|
|
|
data=data)
|