Completed
Pull Request — master (#341)
by
unknown
02:03
created

BaseAction._convert_slug()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 5
rs 9.4286
1
import httplib
2
import requests
3
4
from st2actions.runners.pythonrunner import Action
5
6
7
class BaseAction(Action):
8
    def __init__(self, config):
9
        super(BaseAction, self).__init__(config)
10
11
        self._email = self.config.get('email')
12
        self._token = self.config.get('api_token')
13
        self._brand = self.config.get('brand')
14
        self._api_root = 'https://{}.reamaze.com/api/v1'.format(self._brand)
15
        self._headers = {'Accept': 'application/json'}
16
17
        if not self._email:
18
            raise ValueError('Missing "email" config option')
19
        if not self._brand:
20
            raise ValueError('Missing "brand" config option')
21
        if not self._token:
22
            raise ValueError('Missing "api_token" config option')
23
24
    def _api_get(self, endpoint, headers={}, params=None):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

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.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
25
        _url = self._api_root + endpoint
26
        _headers = self._headers.update(headers)
27
28
        r = requests.get(url=_url, auth=(self._email, self._token),
29
                         params=params, headers=_headers)
30
31
        if r.status_code not in [httplib.OK]:
32
            self.logger.error('GET failed. HTTP status: %s, Body: %s.',
33
                              r.status_code, r.text)
34
35
        r.raise_for_status()
36
        return r.json()
37
38
    def _api_post(self, endpoint, headers={}, data=None, json=None):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

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.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
39
        _url = self._api_root + endpoint
40
        _headers = self._headers.update(headers)
41
42
        r = requests.post(url=_url, auth=(self._email, self._token),
43
                          data=data, headers=_headers, json=json)
44
45
        if r.status_code not in [httplib.OK, httplib.CREATED]:
46
            self.logger.error('POST failed. HTTP status: %s, Body: %s.',
47
                              r.status_code, r.text)
48
49
        return r.json()
50
51
    def _api_put(self, endpoint, headers={}, data=None, json=None):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

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.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
52
        _url = self._api_root + endpoint
53
        _headers = self._headers.update(headers)
54
55
        r = requests.put(url=_url, auth=(self._email, self._token),
56
                         data=data, headers=_headers, json=json)
57
58
        if r.status_code not in [httplib.OK, httplib.ACCEPTED]:
59
            self.logger.error('PUT failed. HTTP status: %s, Body: %s.',
60
                              r.status_code, r.text)
61
62
        r.raise_for_status()
63
        return r.json()
64
65
    def _convert_slug(self, slug_name):
66
        if not slug_name:
67
            return None
68
69
        return slug_name.lower().replace(' ', '-')
70