Just getting started with Flask, following http://flask.pocoo.org/docs/views/
Say I have a basic REST api, in this case for symptoms:
/ GET - list POST - create /<symptomid> GET - detail PUT - replace PATCH - patch DELETE - delete
I can implement this pretty cleanly with Flask MethodView as follows:
from flask import Blueprint, request, g from flask.views import MethodView #... mod = Blueprint('api', __name__, url_prefix='/api') class SymptomAPI(MethodView): """ ... """ url = "/symptoms/" def get(self, uid): if uid is None: return self.list() else: return self.detail(uid) def list(self): # ... def post(self): # ... def detail(self, uid): # ... def put(self, uid): # ... def patch(self, uid): # ... def delete(self, uid): # ... @classmethod def register(cls, mod): symfunc = cls.as_view("symptom_api") mod.add_url_rule(cls.url, defaults={"uid": None}, view_func=symfunc, methods=["GET"]) mod.add_url_rule(cls.url, view_func=symfunc, methods=["POST"]) mod.add_url_rule('%s<int:uid>' % cls.url, view_func=symfunc, methods=['GET', 'PUT', 'PATCH', 'DELETE']) SymptomAPI.register(mod)
But, say, I would like to add another api for these individual symptoms:
/<symptomid>/diagnoses/ GET - list diags for symptom POST - {id: diagid} - create relation with diagnosis /<symptomid>/diagnoses/<diagnosisid> GET - probability symptom given diag PUT - update probability of symptom given diag DELETE - remove diag - symptom relation
Then I would have 4 GET instead of two, as mentioned above.
- Do you think this is a bad api design?
- Will the
MethodView fit this design? (if the design is not bad) - How would you implement these routes?
So ... when writing this question, I found a decent solution. While I'm here, I could also ask a question and a solution that I have. Any feedback would be greatly appreciated.