aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/api/bot/doc.py
blob: c1d6020c2ff0e57a7f32942c9aa68a6896d918f3 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from flask import jsonify
from schema import Optional, Schema

from pysite.base_route import APIView
from pysite.constants import ValidationTypes
from pysite.decorators import api_key, api_params
from pysite.mixins import DBMixin


GET_SCHEMA = Schema([
    {
        Optional("package"): str
    }
])

POST_SCHEMA = Schema([
    {
        "package": str,
        "base_url": str,
        "inventory_url": str
    }
])

DELETE_SCHEMA = Schema([
    {
        "package": str
    }
])


class DocView(APIView, DBMixin):
    path = "/bot/docs"
    name = "bot.docs"
    table_name = "pydoc_links"

    @api_key
    @api_params(schema=GET_SCHEMA, validation_type=ValidationTypes.params)
    def get(self, params=None):
        """
        Fetches documentation metadata from the database.

        - If `package` parameters are provided, fetch metadata
        for the given packages, or `[]` if none matched.

        - If `package` is not provided, return all
        packages known to the database.

        Data must be provided as params.
        API key must be provided as header.
        """

        if params:
            packages = (param['package'] for param in params if 'package' in param)
            data = self.db.get_all(self.table_name, *packages, index='package') or []
        else:
            data = self.db.pluck(self.table_name, ("package", "base_url", "inventory_url")) or []

        return jsonify(data)

    @api_key
    @api_params(schema=POST_SCHEMA, validation_type=ValidationTypes.json)
    def post(self, json_data):
        """
        Adds one or more new documentation metadata objects.

        If the `package` passed in the data
        already exists, it will be updated instead.

        Data must be provided as JSON.
        API key must be provided as header.
        """

        packages_to_insert = (
            {
                "package": json_object["package"],
                "base_url": json_object["base_url"],
                "inventory_url": json_object["inventory_url"]
            } for json_object in json_data
        )

        self.db.insert(self.table_name, *packages_to_insert, conflict="update")
        return jsonify({"success": True})

    @api_key
    @api_params(schema=DELETE_SCHEMA, validation_type=ValidationTypes.json)
    def delete(self, json_data):
        """
        Deletes a documentation metadata object.
        Expects the `package` to be deleted to
        be specified as a request parameter.

        Data must be provided as params.
        API key must be provided as header.
        """

        packages = (json_object["package"]for json_object in json_data)
        changes = self.db.delete(self.table_name, *packages, return_changes=True)
        return jsonify(changes)