corpusAPI.py 2.05 KB
Newer Older
1
"""The corpus API. Add it to your REST API to provide users with metadata about specific texts."""
2 3
from flask_restful import Resource, abort, marshal
from flask_restful.reqparse import RequestParser
4 5 6

from mcserver.app import db
from mcserver.app.models import Corpus, corpus_fields
7
from mcserver.app.services import NetworkService
8 9 10 11 12 13 14


class CorpusAPI(Resource):
    """The corpus API resource. It enables some of the CRUD operations for metadata about specific texts."""

    def __init__(self):
        """Initialize possible arguments for calls to the corpus REST API."""
15
        self.reqparse: RequestParser = NetworkService.base_request_parser.copy()
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
        self.reqparse.add_argument("title", type=str, required=False, help="No title provided")
        self.reqparse.add_argument("author", type=str, required=False, help="No author provided")
        self.reqparse.add_argument("source_urn", type=str, required=False, help="No source URN provided")
        super(CorpusAPI, self).__init__()

    def get(self, cid):
        """The GET method for the corpus REST API. It provides metadata for a specific text."""
        corpus: Corpus = Corpus.query.filter_by(cid=cid).first()
        if corpus is None:
            abort(404)
        return {"corpus": marshal(corpus, corpus_fields)}

    def put(self, cid):
        """The PUT method for the corpus REST API. It provides updates metadata for a specific text."""
        corpus: Corpus = Corpus.query.filter_by(cid=cid).first()
        if corpus is None:
            abort(404)
        args = self.reqparse.parse_args()
        for k, v in args.items():
            if v is not None:
                setattr(corpus, k, v)
        db.session.commit()
        return {"corpus": marshal(corpus, corpus_fields)}

    def delete(self, cid):
        """The DELETE method for the corpus REST API. It deletes metadata for a specific text."""
        corpus: Corpus = Corpus.query.filter_by(cid=cid).first()
        if corpus is None:
            abort(404)
        db.session.delete(corpus)
        db.session.commit()
        return {"result": True}