2012-09-22 11 views
6

Sólo partiendo con el frasco, siguiendo a lo largo de http://flask.pocoo.org/docs/views/Frasco: api de descanso anidado: ¿usar algo que no sea methodview o he hecho un diseño incorrecto?

decir que tengo una API básica de reposo, en este caso para los síntomas:

/ 
    GET - list 
    POST - create 

/<symptomid> 
    GET - detail 
    PUT - replace 
    PATCH - patch 
    DELETE - delete 

que pueden poner en práctica esta bastante limpia con el frasco de MethodView de la siguiente manera:

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) 

Pero, digamos que me gustaría adjuntar otro api de estos síntomas individuales:

/<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 

Entonces tendría 4 GET en lugar de dos como los anteriores.

  1. ¿Crees que esto es un mal diseño de API?
  2. ¿Sería apropiado MethodView para este diseño? (si el diseño no está mal)
  3. ¿Cómo implementaría estas rutas?

Así que ... al escribir esta pregunta, he encontrado una solución decente. Mientras esté aquí, también podría publicar la pregunta y la solución que tengo. Cualquier comentario aún sería muy apreciado.

Respuesta

7

Creo que el diseño está bien. MethodView debería ser bastante increíble para eso. Puede poner las rutas juntas de esta manera:

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) 
Cuestiones relacionadas