Completed
Push — master ( 889440...e965d2 )
by Dieter
01:16
created

TravisConnector._handle_request()   A

Complexity

Conditions 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
1
# vim: set expandtab sw=4 ts=4:
2
"""
3
Interface to Travis CI API.
4
5
Copyright (C) 2014-2016 Dieter Adriaenssens <[email protected]>
6
7
This file is part of buildtimetrend/python-lib
8
<https://github.com/buildtimetrend/python-lib/>
9
10
This program is free software: you can redistribute it and/or modify
11
it under the terms of the GNU Affero General Public License as published by
12
the Free Software Foundation, either version 3 of the License, or
13
any later version.
14
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU Affero General Public License for more details.
19
20
You should have received a copy of the GNU Affero General Public License
21
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
"""
23
from builtins import str
24
import codecs
25
import json
26
from buildtimetrend import logger
27
from buildtimetrend.tools import check_dict
28
import buildtimetrend
29
try:
30
    # For Python 3.0 and later
31
    from urllib.request import Request, build_opener
32
except ImportError:
33
    # Fall back to Python 2's urllib2
34
    from urllib2 import Request, build_opener
35
36
TRAVIS_ORG_API_URL = 'https://api.travis-ci.org/'
37
38
39
class TravisConnector(object):
40
41
    """Base class to connect to Travis CI API."""
42
43
    def __init__(self):
44
        """Constructor."""
45
        self.api_url = None
46
        self.request_params = {
47
            'user-agent': buildtimetrend.USER_AGENT
48
        }
49
50
    def download_job_log(self, job_id):
51
        """
52
        Retrieve Travis CI job log.
53
54
        Parameters:
55
        - job_id : ID of the job to process
56
        """
57
        request = 'jobs/{}/log'.format(str(job_id))
58
        logger.info("Request build job log #%s", str(job_id))
59
        return self._handle_request(request)
60
61
    def json_request(self, json_request):
62
        """
63
        Retrieve Travis CI data using API.
64
65
        Parameters:
66
        - json_request : json_request to be sent to API
67
        """
68
        result = self._handle_request(
69
            json_request,
70
            {
71
                'accept': 'application/vnd.travis-ci.2+json'
72
            }
73
        )
74
75
        reader = codecs.getreader('utf-8')
76
        return json.load(reader(result))
77
78
    def _handle_request(self, request, params=None):
79
        """
80
        Retrieve Travis CI data using API.
81
82
        Parameters:
83
        - request : request to be sent to API
84
        - params : HTTP request parameters
85
        """
86
        request_url = self.api_url + request
87
88
        request_params = self.request_params.copy()
89
        if params is not None and check_dict(params, "params"):
90
            request_params.update(params)
91
92
        req = Request(
93
            request_url,
94
            None,
95
            request_params
96
        )
97
        opener = build_opener()
98
        logger.info("Request from Travis CI API : %s", request_url)
99
        return opener.open(req)
100
101
102
class TravisOrgConnector(TravisConnector):
103
104
    """Connects to Travis.org API."""
105
106
    def __init__(self):
107
        """Constructor."""
108
        super(TravisOrgConnector, self).__init__()
109
        self.api_url = TRAVIS_ORG_API_URL
110