|
1
|
|
|
from st2actions.runners.pythonrunner import Action |
|
2
|
|
|
import requests |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class BaseAction(Action): |
|
6
|
|
|
def __init__(self, config): |
|
7
|
|
|
super(BaseAction, self).__init__(config) |
|
8
|
|
|
|
|
9
|
|
|
self._email = self.config.get('email') |
|
10
|
|
|
self._token = self.config.get('api_token') |
|
11
|
|
|
self._brand = self.config.get('brand') |
|
12
|
|
|
self._api_root = 'https://{}.reamaze.com/api/v1'.format(self._brand) |
|
13
|
|
|
self._headers = {'Accept': 'application/json'} |
|
14
|
|
|
|
|
15
|
|
|
if not self._email: |
|
16
|
|
|
raise ValueError('Missing "email" config option') |
|
17
|
|
|
if not self._brand: |
|
18
|
|
|
raise ValueError('Missing "brand" config option') |
|
19
|
|
|
if not self._token: |
|
20
|
|
|
raise ValueError('Missing "api_token" config option') |
|
21
|
|
|
|
|
22
|
|
|
def _api_get(self, endpoint, headers={}, params=None): |
|
|
|
|
|
|
23
|
|
|
_url = self._api_root + endpoint |
|
24
|
|
|
_headers = self._headers.update(headers) |
|
25
|
|
|
|
|
26
|
|
|
r = requests.get(url=_url, auth=(self._email, self._token), |
|
27
|
|
|
params=params, headers=_headers) |
|
28
|
|
|
|
|
29
|
|
|
r.raise_for_status() |
|
30
|
|
|
return r.json() |
|
31
|
|
|
|
|
32
|
|
|
def _api_post(self, endpoint, headers={}, data=None, json=None): |
|
|
|
|
|
|
33
|
|
|
_url = self._api_root + endpoint |
|
34
|
|
|
_headers = self._headers.update(headers) |
|
35
|
|
|
|
|
36
|
|
|
r = requests.post(url=_url, auth=(self._email, self._token), |
|
37
|
|
|
data=data, headers=_headers, json=json) |
|
38
|
|
|
|
|
39
|
|
|
r.raise_for_status() |
|
40
|
|
|
return r.json() |
|
41
|
|
|
|
|
42
|
|
|
def _convert_slug(self, slug_name): |
|
43
|
|
|
if not slug_name: |
|
44
|
|
|
return None |
|
45
|
|
|
|
|
46
|
|
|
return slug_name.lower().replace(' ', '-') |
|
47
|
|
|
|
Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.