From d0d08d513e403f7836da450a1d05eaeb89a5ba30 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 23 Nov 2022 12:19:02 +0300 Subject: Create a new REST endpoint for fetching a phenotype's metadata * gn3/api/general.py: (dataset_metadata) Delete. * gn3/api/metadata.py: Import Blueprint, jsonify, current_app, SPARQLWrapper and get_dataset_metadata. (metadata): New Blueprint (jsonify_dataset_metadata): New function/end-point. * gn3/app.py: Import metadata (create_app): Register metadata blueprint. --- gn3/api/general.py | 5 +---- gn3/api/metadata.py | 22 ++++++++++++++++++++++ gn3/app.py | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 gn3/api/metadata.py diff --git a/gn3/api/general.py b/gn3/api/general.py index 68b128b..f904ffe 100644 --- a/gn3/api/general.py +++ b/gn3/api/general.py @@ -69,7 +69,4 @@ def run_r_qtl(geno_filestr, pheno_filestr): f"{geno_filestr} {pheno_filestr}") return jsonify(run_cmd(cmd)), 201 -@general.route("/dataset/") -def dataset_metadata(accession_id): - """Return info as JSON for dataset with ACCESSION_ID.""" - return jsonify(rdf.get_dataset_metadata(accession_id).data) + diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py new file mode 100644 index 0000000..4fe9e67 --- /dev/null +++ b/gn3/api/metadata.py @@ -0,0 +1,22 @@ +"""API for fetching metadata using an API""" +from flask import Blueprint +from flask import jsonify +from flask import current_app + +from SPARQLWrapper import SPARQLWrapper + +from gn3.db.rdf import get_dataset_metadata + + +metadata = Blueprint("metadata", __name__) + + +@metadata.route("/dataset/", methods=["GET"]) +def jsonify_dataset_metadata(accession_id): + """Fetch a dataset's metadata given it's ACCESSION_ID""" + return jsonify(get_dataset_metadata( + SPARQLWrapper(current_app + .config + .get("SPARQL_ENDPOINT")), + accession_id + ).data) diff --git a/gn3/app.py b/gn3/app.py index 6c7e483..a187e54 100644 --- a/gn3/app.py +++ b/gn3/app.py @@ -18,6 +18,8 @@ from gn3.api.ctl import ctl from gn3.api.async_commands import async_commands from gn3.api.menu import menu from gn3.api.search import search +from gn3.api.metadata import metadata + def create_app(config: Union[Dict, str, None] = None) -> Flask: """Create a new flask object""" @@ -53,4 +55,5 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask: app.register_blueprint(async_commands, url_prefix="/api/async_commands") app.register_blueprint(menu, url_prefix="/api/menu") app.register_blueprint(search, url_prefix="/api/search") + app.register_blueprint(metadata, url_prefix="/api/metadata") return app -- cgit v1.2.3