summaryrefslogtreecommitdiff
path: root/morphcacheserver/api_2_0.py
blob: 4e94a9451d6fad10473f15ed5d3485375cb9f9d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright (C) 2015 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


'''The 2.0/ set of HTTP methods.

This is still in development and you can pretty much do what you want with it
right now. Work began on it by Sam Thursfield as part of
<http://wiki.baserock.org/projects/deterministic-builds/>, to provide a way of
doing ongoing measurement.

'''


from bottle import Bottle, request, response

import urllib


def unescape_parameter(param):
    return urllib.unquote(param)


def api_2_0(db):
    app = Bottle()

    @app.put('/builds')
    def put_build():
        '''Record a build.

        Expected parameters:

            - cache_name: artifact cache key plus name
            - builder_name: URL identifying the build worker
            - build_datetime: time artifact build was started
            - hash_sha1: SHA1 of built artifact
            - source_repo: (optional): repo this is built from
            - source_ref: (optional): ref this is built from

        '''
        cache_name = unescape_parameter(request.query.cache_name)
        builder_name = unescape_parameter(request.query.builder_name)
        build_datetime = unescape_parameter(
            request.query.build_datetime)
        hash_sha1 = unescape_parameter(request.query.hash_sha1)

        source_repo = unescape_parameter(
            request.query.get('source_repo'))
        source_ref = unescape_parameter(
            request.query.get('source_ref'))

        db.record_build(cache_name, builder_name, build_datetime,
                        hash_sha1, source_repo, source_ref)

    @app.get('/builds')
    def get_builds():
        '''Return info on all known builds.'''
        return {'builds': db.view_artifact_statistics()}

    # This may not be the right name for this method, as its name is
    # 'builds' but it takes the cache name of an artifact, not a build.
    @app.get('/builds/<cache_name>')
    def get_builds_for_artifact(cache_name):
        '''Return info on builds of a given artifact.'''
        results = sorted(db.iter_builds_for_artifact_file(cache_name))

        if len(results) == 0:
            response.status = 404
        else:
            return {cache_name: results}

    return app