Flask - inested rest api - use something other than a method, or did I make a bad design?

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.

+6
source share
1 answer

I think the design is fine. MethodView should be pretty awesome for him. You can combine routes as follows:

 class SymptomDiagnosisAPI(MethodView): """ /<symptom_id>/diagnoses/ GET - list diags for symptoms POST - {id: diagid} - create relation with diagnosis /<symptom_id>/diagnoses/<diagnosis_id> GET - probability symptom given diag PUT - update probability of symptom given diag DELETE - remove diag - symptom relation """ def get(self, symptom_id, diagnosis_id): if diagnosis_id is None: return self.list_diagnoses(symptom_id) else: return self.symptom_diagnosis_detail(symptom_id, diagnosis_id) def list_diagnoses(self, symptom_id): # ... def post(self, symptom_id): # ... def symptom_diagnosis_detail(self, symptom_id, diagnosis_id): # ... def put(self, symptom_id, diagnosis_id): # ... def delete(self, symptom_id, diagnosis_id): # ... @classmethod def register(cls, mod): url = "/symptoms/<int:symptom_id>/diagnoses/" f = cls.as_view("symptom_diagnosis_api") mod.add_url_rule(url, view_func=f, methods=["GET"], defaults={"diagnosis_id": None}) mod.add_url_rule(url, view_func=f, methods=["POST"]) mod.add_url_rule('%s<int:diagnosis_id>' % url, view_func=f, methods=['GET', 'PUT', 'DELETE']) SymptomDiagnosisAPI.register(mod) 
+7
source

Source: https://habr.com/ru/post/925982/


All Articles