diff options
| author | Frederick Muriuki Muriithi <fmuriit1@uthsc.edu> | 2026-08-27 19:21:50 +0000 |
|---|---|---|
| committer | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-08-27 15:05:04 -0500 |
| commit | d103f87d3cb152ceca4afc90967f289662043a7d (patch) | |
| tree | b709be69a5bc344dcf117125b47e15ee004b6743 /gn3/api | |
| parent | 4268c2f8774e8c8a45e5c596b89dcf62570c12f8 (diff) | |
| download | genenetwork3-d103f87d3cb152ceca4afc90967f289662043a7d.tar.gz | |
api: add v1 blueprint hierarchy for species/populations/case-attributes
Introduces a nested blueprint structure that provides clean RESTful URLs
alongside the existing flat endpoints (which are left unchanged):
/api/v1/species/<s>/populations/<p>/case-attributes/
/api/v1/species/<s>/populations/<p>/case-attributes/edit (POST)
/api/v1/species/<s>/populations/<p>/case-attributes/diffs/<t>/list
/api/v1/species/<s>/populations/<p>/case-attributes/diffs/<id>/approve
/api/v1/species/<s>/populations/<p>/case-attributes/diffs/<id>/reject
New files:
* gn3/api/version01.py — top-level v1 blueprint; registers speciesbp at /species
* gn3/api/species.py — species blueprint; registers popbp; stubs list/detail (501)
* gn3/api/populations.py — populations blueprint; registers caseattrsbp; stubs (501)
Changes to existing files:
* gn3/api/case_attributes.py — adds caseattrsbp Blueprint and v1_ route handlers
that delegate to the existing private helper functions; **kwargs absorbs
species_id so existing logic only needs pop_id (= InbredSet ID)
* gn3/app.py — registers v01 at /api/v1
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Reviewed-By: Frederick M. Muriithi <fredmanglis@gmail.com>
Diffstat (limited to 'gn3/api')
| -rw-r--r-- | gn3/api/case_attributes.py | 194 | ||||
| -rw-r--r-- | gn3/api/populations.py | 38 | ||||
| -rw-r--r-- | gn3/api/species.py | 32 | ||||
| -rw-r--r-- | gn3/api/v1.py | 20 |
4 files changed, 284 insertions, 0 deletions
diff --git a/gn3/api/case_attributes.py b/gn3/api/case_attributes.py index 28337ea..640e17b 100644 --- a/gn3/api/case_attributes.py +++ b/gn3/api/case_attributes.py @@ -10,7 +10,9 @@ from MySQLdb.cursors import DictCursor from authlib.integrations.flask_oauth2.errors import _HTTPException from flask import ( jsonify, + make_response, request, + url_for, Response, Blueprint, current_app) @@ -24,10 +26,13 @@ from gn3.db.case_attributes import ( from gn3.db_utils import Connection, database_connection +from gn_libs.privileges import resources + from gn3.oauth2.authorisation import require_token from gn3.oauth2.errors import AuthorisationError caseattr = Blueprint("case-attribute", __name__) +caseattrsbp = Blueprint("case-attributes", __name__) def required_access( @@ -294,3 +299,192 @@ def reject_case_attributes_diff( return jsonify({ "message": ("You don't have the right privileges to edit this resource.") }), 401 + + +# --------------------------------------------------------------------------- +# v1 blueprint routes (mounted at /api/v1/species/<s>/populations/<p>/case-attributes) +# Flask's blueprint nesting threads species_id and pop_id through to every +# child view function. +# --------------------------------------------------------------------------- + +def __population_privileges__( + auth_token: dict, species_id: int, pop_id: int +) -> tuple[tuple[str, ...], tuple[str, ...]]: + """Return (resource_privs, system_privs) for the given population. + + species_id is taken from the URL — no DB lookup needed. + Returns empty tuples for either set when the auth server reports an error, + so privilege checks fail closed rather than open. + """ + bearer = f"Bearer {auth_token['access_token']}" + auth_url = current_app.config["AUTH_SERVER_URL"] + + resource_privs: tuple[str, ...] = tuple() + res_id_resp = requests.get( + urljoin(auth_url, + f"auth/resource/populations/resource-id/{species_id}/{pop_id}"), + timeout=300) + if res_id_resp.status_code == 200: + resource_id = res_id_resp.json()["resource-id"] + roles_resp = requests.get( + urljoin(auth_url, f"auth/resource/{resource_id}/roles"), + timeout=300) + if roles_resp.status_code == 200: + resource_privs = tuple( + priv["privilege_id"] + for role in roles_resp.json() + for priv in role.get("privileges", [])) + + system_privs: tuple[str, ...] = tuple() + sys_resp = requests.get( + urljoin(auth_url, "auth/system/roles"), + headers={"Authorization": bearer}, + timeout=300) + if sys_resp.status_code == 200: + system_privs = tuple( + priv["privilege_id"] + for role in sys_resp.json() + for priv in role.get("privileges", [])) + + return resource_privs, system_privs + +@caseattrsbp.route("/", methods=["GET"]) +def v1_list_case_attributes(species_id: int, pop_id: int) -> Response: + """List all case-attribute names for the given population.""" + with database_connection(current_app.config["SQL_URI"]) as conn: + data = __case_attribute_labels_by_inbred_set__(conn, pop_id) + return jsonify({ + "data": data, + "links": { + "self": url_for( + "v1.species.populations.case-attributes.v1_list_case_attributes", + species_id=species_id, pop_id=pop_id), + "edit": url_for( + "v1.species.populations.case-attributes.v1_edit_case_attributes", + species_id=species_id, pop_id=pop_id), + "diffs": url_for( + "v1.species.populations.case-attributes.v1_list_diffs", + species_id=species_id, pop_id=pop_id, change_type="review"), + "population": url_for( + "v1.species.populations.population_details", + species_id=species_id, pop_id=pop_id), + } + }) + + +@caseattrsbp.route("/<int:ca_id>", methods=["GET"]) +def v1_case_attribute_details(species_id: int, pop_id: int, ca_id: int) -> Response: + return make_response(jsonify({ + "status": "not implemented", + "message": "Single case-attribute details are not yet available." + }), 501) + + +@caseattrsbp.route("/edit", methods=["POST"]) +@require_token +def v1_edit_case_attributes( + species_id: int, pop_id: int, auth_token=None) -> tuple[Response, int]: + """Queue an edit to the case attributes for the given population.""" + with database_connection(current_app.config["SQL_URI"]) as conn, conn.cursor() as cursor: + data = request.json["edit-data"] # type: ignore + edit = CaseAttributeEdit( + inbredset_id=pop_id, + status=EditStatus.review, + user_id=auth_token["jwt"]["sub"], + changes=data + ) + directory = (Path(current_app.config["LMDB_DATA_PATH"]) / + "case-attributes" / str(pop_id)) + queue_edit(cursor=cursor, directory=directory, edit=edit) + return jsonify({ + "diff-status": "queued", + "message": ("The changes to the case-attributes have been " + "queued for approval."), + }), 201 + + +@caseattrsbp.route("/diffs/<string:change_type>/list", methods=["GET"]) +def v1_list_diffs( + species_id: int, pop_id: int, change_type: str) -> tuple[Response, int]: + """List pending diffs for the given population by change type.""" + with (database_connection(current_app.config["SQL_URI"]) as conn, + conn.cursor(cursorclass=DictCursor) as cursor): + directory = (Path(current_app.config["LMDB_DATA_PATH"]) / + "case-attributes" / str(pop_id)) + return jsonify( + get_changes( + cursor=cursor, + change_type=EditStatus[change_type], + directory=directory + ) + ), 200 + + +@caseattrsbp.route("/diffs/<int:change_id>/approve", methods=["POST"]) +@require_token +def v1_approve_case_attributes_diff( + species_id: int, pop_id: int, change_id: int, auth_token=None +) -> tuple[Response, int]: + """Approve a queued case-attribute diff.""" + try: + resource_privs, system_privs = __population_privileges__( + auth_token, species_id, pop_id) + if not resources.can_apply_or_reject_edit(resource_privs, system_privs): + raise AuthorisationError( + "You don't have the right privileges to approve this edit.") + with (database_connection(current_app.config["SQL_URI"]) as conn, + conn.cursor() as cursor): + directory = (Path(current_app.config["LMDB_DATA_PATH"]) / + "case-attributes" / str(pop_id)) + match apply_change(cursor, change_type=EditStatus.approved, + change_id=change_id, directory=directory): + case True: + return jsonify({ + "diff-status": "approved", + "message": f"Successfully approved # {change_id}" + }), 201 + case _: + return jsonify({ + "diff-status": "queued", + "message": f"Was not able to successfully approve # {change_id}" + }), 200 + except AuthorisationError as __auth_err: + return jsonify({ + "diff-status": "queued", + "message": "You don't have the right privileges to edit this resource." + }), 401 + + +@caseattrsbp.route("/diffs/<int:change_id>/reject", methods=["POST"]) +@require_token +def v1_reject_case_attributes_diff( + species_id: int, pop_id: int, change_id: int, auth_token=None +) -> tuple[Response, int]: + """Reject a queued case-attribute diff.""" + try: + resource_privs, system_privs = __population_privileges__( + auth_token, species_id, pop_id) + if not resources.can_apply_or_reject_edit(resource_privs, system_privs): + raise AuthorisationError( + "You don't have the right privileges to reject this edit.") + with database_connection(current_app.config["SQL_URI"]) as conn, \ + conn.cursor() as cursor: + directory = (Path(current_app.config["LMDB_DATA_PATH"]) / + "case-attributes" / str(pop_id)) + match apply_change(cursor, change_type=EditStatus.rejected, + change_id=change_id, directory=directory): + case True: + return jsonify({ + "diff-status": "rejected", + "message": ("The changes to the case-attributes have been " + "rejected.") + }), 201 + case _: + return jsonify({ + "diff-status": "queued", + "message": "Failed to reject changes" + }), 200 + except AuthorisationError as __auth_err: + return jsonify({ + "message": "You don't have the right privileges to edit this resource." + }), 401 diff --git a/gn3/api/populations.py b/gn3/api/populations.py new file mode 100644 index 0000000..09770db --- /dev/null +++ b/gn3/api/populations.py @@ -0,0 +1,38 @@ +"""Population (InbredSet group) API endpoints (v1).""" +from flask import Blueprint, jsonify, make_response, url_for + +from .case_attributes import caseattrsbp + +popbp = Blueprint("populations", __name__) +popbp.register_blueprint(caseattrsbp, url_prefix="/<int:pop_id>/case-attributes") + + +@popbp.route("/", methods=["GET"]) +def list_populations(species_id: int): + return make_response(jsonify({ + "status": "not implemented", + "message": "Population listing is not yet available under this API version.", + "links": { + "self": url_for("v1.species.populations.list_populations", + species_id=species_id), + "species": url_for("v1.species.species_details", + species_id=species_id), + } + }), 501) + + +@popbp.route("/<int:pop_id>", methods=["GET"]) +def population_details(species_id: int, pop_id: int): + return make_response(jsonify({ + "status": "not implemented", + "message": "Population details are not yet available under this API version.", + "links": { + "self": url_for("v1.species.populations.population_details", + species_id=species_id, pop_id=pop_id), + "species": url_for("v1.species.species_details", + species_id=species_id), + "case-attributes": url_for( + "v1.species.populations.case-attributes.v1_list_case_attributes", + species_id=species_id, pop_id=pop_id), + } + }), 501) diff --git a/gn3/api/species.py b/gn3/api/species.py new file mode 100644 index 0000000..995ee14 --- /dev/null +++ b/gn3/api/species.py @@ -0,0 +1,32 @@ +"""Species API endpoints (v1).""" +from flask import Blueprint, jsonify, make_response, url_for + +from .populations import popbp + +speciesbp = Blueprint("species", __name__) +speciesbp.register_blueprint(popbp, url_prefix="/<int:species_id>/populations") + + +@speciesbp.route("/", methods=["GET"]) +def list_species(): + return make_response(jsonify({ + "status": "not implemented", + "message": "Species listing is not yet available under this API version.", + "links": { + "self": url_for("v1.species.list_species"), + } + }), 501) + + +@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), + "populations": url_for("v1.species.populations.list_populations", + species_id=species_id), + } + }), 501) diff --git a/gn3/api/v1.py b/gn3/api/v1.py new file mode 100644 index 0000000..1d4382d --- /dev/null +++ b/gn3/api/v1.py @@ -0,0 +1,20 @@ +"""Version 1 API blueprint.""" +from flask import Blueprint, url_for, jsonify, make_response + +from .species import speciesbp + +v1 = Blueprint("v1", __name__) +v1.register_blueprint(speciesbp, url_prefix="/species") + + +@v1.route("/", methods=["GET"]) +def index(): + return make_response(jsonify({ + "message": ( + "This is v1 of the API. It's more hierarchical that the unversioned" + " variant."), + "links": { + "self": url_for("v1.index"), + "species": url_for("v1.species.list_species") + } + }), 200) |
