~qeef/damn-client.py

ref: 008f97255d638613faebc379be8b52f6531aa2e5 damn-client.py/damn_client/api.py -rw-r--r-- 2.6 KiB
008f9725Jiri Vlasak Update requirements 6 months ago
                                                                                
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
"""Damn API class."""
import requests

class DamnAPI:
    DAMN_SERVER = "https://server.damn-project.org"
    areas = None

    def ep(self, endpoint=""):
        return "{}{}".format(self.DAMN_SERVER, endpoint)

    def __init__(self, uri):
        assert uri[0:8] == "https://" or uri[0:7] == "http://"
        if uri[-1] == "/":
            uri = uri[:-1]
        self.DAMN_SERVER = uri
        self.areas = {}

    def get_geometries(self):
        """HTTP GET ``/geometries`` from the `DAMN_SERVER`."""
        r = requests.get(self.ep("/geometries"))
        assert r.status_code == 200
        for g in r.json():
            fc = g["featurecollection"]
            if g["aid"] in self.areas:
                self.areas[g["aid"]]["featurecollection"] = fc
            else:
                self.areas[g["aid"]] = {
                    "aid": g["aid"],
                    "featurecollection": fc,
                }

    def get_areas(self):
        """Get the list of areas from the `DAMN_SERVER`."""
        r = requests.get(self.ep("/areas"))
        assert r.status_code == 200
        for a in r.json():
            if not a["aid"] in self.areas:
                self.areas[a["aid"]] = {"aid": a["aid"]}
            self.areas[a["aid"]]["tags"] = a["tags"]
            self.areas[a["aid"]]["priority"] = a["priority"]
            self.areas[a["aid"]]["created"] = a["created"]
            self.areas[a["aid"]]["squares_to_map"] = a["squares_to_map"]
            self.areas[a["aid"]]["squares_to_review"] = a["squares_to_review"]
            self.areas[a["aid"]]["squares_done"] = a["squares_done"]

    def get_commits(self, aid=False, since=False):
        """Get the commits of the area `aid`.

        Keyword arguments
        -----------------

        - `aid` -- The area ID.
        - `since` -- Request commits `since`.
        """
        if aid is False or since is False:
            return
        r = requests.get(
            self.ep("/area/{}/commits?since={}".format(aid, since))
        )
        assert r.status_code == 200
        if aid in self.areas:
            if not "commits" in self.areas[aid]:
                self.areas[aid]["commits"] = r.json()
        else:
            self.areas[aid] = {"aid": aid, "commits": r.json()}

    def get_squares(self, aid=False):
        """Get area's squares."""
        if aid is False:
            return
        r = requests.get(self.ep("/area/{}/squares".format(aid)))
        assert r.status_code == 200
        if aid in self.areas:
            if not "squares" in self.areas[aid]:
                self.areas[aid]["squares"] = r.json()
        else:
            self.areas[aid] = {"aid": aid, "squares": r.json()}