Completed
Pull Request — develop (#1)
by Fabian
01:17
created

NewRelicDeploymentException

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 0
dl 0
loc 2
ccs 2
cts 2
cp 1
c 1
b 0
f 0
1 1
from json import dumps
2
3 1
import requests
4
5
6 1
class NewRelicException(Exception):
7 1
    pass
8
9
10 1
class NewRelicDeploymentException(NewRelicException):
11 1
    pass
12
13
14 1
class Deployment(object):
15 1
    ENDPOINT = 'https://api.newrelic.com/v2/applications/%(app_id)s/deployments.json'
16
17 1
    def __init__(self, api_key, app_id, user):
18 1
        self.__api_key = api_key
19 1
        self.__app_id = app_id
20 1
        self.__user = user
21
22 1
    @property
23
    def endpoint(self):
24 1
        return self.ENDPOINT % dict(app_id=self.__app_id)
25
26 1
    @property
27
    def headers(self):
28 1
        return {
29
            'Content-Type': 'application/json',
30
            'X-Api-Key': self.__api_key
31
        }
32
33 1
    def get_payload(self, revision, changelog, description):
34 1
        return {
35
            "deployment" : {
36
                "revision": str(revision),
37
                "changelog": str(changelog),
38
                "description": str(description),
39
                "user": str(self.__user)
40
            }
41
        }
42
43 1
    def deploy(self, revision, changelog, description):
44 1
        payload = self.get_payload(revision, changelog, description)
45 1
        response = requests.post(self.endpoint, headers=self.headers, json=payload)
46
47 1
        if response.status_code != 201:
48 1
            raise NewRelicDeploymentException
49
50
        return response
51