about summary refs log tree commit diff
path: root/gn3/api/species.py
blob: 440617062d8d59f3e29bc6947ac23e0a9474a6d2 (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
"""Species API endpoints (v1)."""
from MySQLdb.cursors import DictCursor
from gn_libs.mysqldb import Connection, database_connection
from flask import (request,
                   jsonify,
                   url_for,
                   Blueprint,
                   make_response,
                   current_app as app)

from .populations import popbp

speciesbp = Blueprint("species", __name__)
speciesbp.register_blueprint(popbp, url_prefix="/<int:species_id>/populations")


def fetch_species(conn: Connection, detailed: bool = False) -> tuple[dict, ...]:
    """Fetch all the species that the system is aware of."""
    # 'Id' is needed for links to the Species, not very useful to end user.
    _detailed = ("Id", "SpeciesId", "SpeciesName", "Name", "MenuName",
                 "FullName", "TaxonomyId",  "OrderId", "Family",
                 "FamilyOrderId")
    _summary = ("Id", "SpeciesName", "Name", "FullName")
    columns = ", ".join(_detailed if detailed else _summary)
    with conn.cursor(cursorclass=DictCursor) as cursor:
        cursor.execute(f"SELECT {columns} FROM Species ORDER BY Species.Id")
        return tuple(dict(row) for row in cursor.fetchall())


@speciesbp.route("/", methods=["GET"])
def list_species():
    with database_connection(app.config["SQL_URI"]) as conn:
        species_list = fetch_species(
            conn,
            detailed=str(request.args.get("detailed") or "").lower() == "true")
        return make_response(jsonify({
            "status": "success",
            "message": (
                "The listing of species that this system is aware of."
                "\n\nA GET parameter 'detailed=true' can be provided to provide"
                " more detailed, albeit noisy, output."),
            "species": [{
                **{key: val for key,val in spc.items() if key not in ("Id",)},
                "links": {
                    "self": url_for(
                        "v1.species.species_details", species_id=spc["Id"])
                }
            } for spc in species_list],
            "links": {
                "self": url_for("v1.species.list_species")
            }
        }), 200)


@speciesbp.route("/<int:species_id>", methods=["GET"])
def species_details(species_id: int):
    return make_response(jsonify({
        "status": "not implemented",
        "message": (
            "Species details are not yet available under this API version."),
        "links": {
            "self": url_for(
                "v1.species.species_details", species_id=species_id),
            "collection": url_for("v1.species.list_species"),
            "populations": url_for("v1.species.populations.list_populations",
                                   species_id=species_id),
        }
    }), 501)