Proprio partendo con Flask, proseguendo lungo a http://flask.pocoo.org/docs/views/Flask - nested rest api - usa qualcosa di diverso da methodview o ho fatto un cattivo design?
dire che ho un api base REST, in questo caso per i sintomi:
/
GET - list
POST - create
/<symptomid>
GET - detail
PUT - replace
PATCH - patch
DELETE - delete
posso realizzare questa bella pulita con la boccetta di MethodView
come segue:
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)
Ma, diciamo che vorrei allegare un altro api su questi singoli sintomi:
/<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
Avrei quindi 4 GET anziché due come sopra.
- Pensi che si tratti di un design di una cattiva api?
MethodView
appropriato per questo modello? (se il design non è male)- Come implementeresti questi percorsi?
Quindi ... nello scrivere questa domanda, ho trovato una soluzione decente. Finché sono qui, potrei anche pubblicare la domanda e la soluzione che ho. Qualsiasi feedback sarebbe ancora molto apprezzato.