Completed
Pull Request — master (#539)
by
unknown
02:36
created

TeamCityAction._api_post()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
1
from st2actions.runners.pythonrunner import Action
2
import base64
3
4
import requests
5
6
7
class TeamCityAction(Action):
8
    """A simple representation of a TeamCity action.
9
10
    :param config: A list, configurations needed for communicating with TeamCity
11
    """
12
    def __init__(self, config):
13
        super(TeamCityAction, self).__init__(config)
14
15
        self._version = self.config['version']
16
        self._username = self.config['username']
17
        self._password = self.config['password']
18
        self._url = self._get_baseurl(self.config['url'])
19
        self._headers = self._get_headers()
20
21
    def _get_baseurl(self, url):
22
        """Configure base url used for all TeamCity api requests
23
24
        Args:
25
            url: The domain name including http/https
26
27
        Returns:
28
            string: URL used for TeamCity api requests
29
        """
30
        if not self.config['url']:
31
            raise ValueError('TeamCity url config must be defined.')
32
33
        baseuri = ""
34
35
        if url[-1] == '/':
36
            baseurl = url.rstrip('/')
37
        else:
38
            baseurl = url
39
40
        if self._username and self._password:
41
            baseuri = 'httpAuth'
42
43
        if self._version:
44
            baseurl += '/' + baseuri + '/app/rest' + self._version
45
        else:
46
            baseurl += '/' + baseuri + '/app/rest'
47
48
        return baseurl
49
50
    def _get_headers(self):
51
        """Return headers used in all requests made to the TeamCity api.
52
53
        If the a username and password has been configured, then a
54
        base64 encoding is created for basic authentication.
55
        """
56
        headers = {
57
            'Accept': 'application/json'
58
        }
59
60
        if self._username and self._password:
61
            auth_header = base64.b64encode('{}:{}'.format(self._username,
62
                                                          self._password))
63
            headers['Authorization'] = 'Basic {}'.format(auth_header)
64
            headers['User-Agent'] = 'Mozilla/5.0'
65
66
        return headers
67
68
    def _api_get(self, endpoint, params=None):
69
        """Make get request to the TeamCity api.
70
71
         Args:
72
             endpoint: A string, endpoint uri request is being made to.
73
             params: A list, parameters to be added as query string members in request
74
        Returns:
75
            string: Json string response
76
        """
77
        if endpoint[0] == '/':
78
            endpoint = endpoint.lstrip('/')
79
80
        url = '/'.join([self._url, endpoint])
81
        r = requests.get(url, params, headers=self._headers)
0 ignored issues
show
Bug introduced by
There seem to be too many positional arguments for this function call.
Loading history...
82
        r.raise_for_status()
83
        return r.json()
84
85
    def _api_post(self, endpoint, data):
86
        """Make post request to the TeamCity api.
87
88
         Args:
89
             endpoint: A string, endpoint uri request is being made to.
90
             data: Json data to be added to the body of the request
91
        Returns:
92
            string: Json string response
93
        """
94
        if endpoint[0] == '/':
95
            endpoint = endpoint.lstrip('/')
96
97
        self._headers['Content-Type'] = 'application/xml'
98
99
        url = '/'.join([self._url, endpoint])
100
        r = requests.post(url, data=data, headers=self._headers)
101
        r.raise_for_status()
102
        return r.json()
103