简体   繁体   English

如何在Flask-Restplus响应类中处理多个“GET”端点

[英]How to handle multiple 'GET' endpoints in a Flask-Restplus response class

I'm just starting learning Flask and are trying to create a simple API dealing with a data processing backend (named ATExplorer). 我刚开始学习Flask并且正在尝试创建一个处理数据处理后端的简单API(名为ATExplorer)。

I started by creating a 'backend' namespace, ie backendAPI: 我开始创建一个'后端'命名空间,即backendAPI:

from flask import Flask, Blueprint, redirect, url_for
from flask_restplus import Resource, Api
from werkzeug.routing import Rule

app = Flask(__name__)
app.config.SWAGGER_UI_DOC_EXPANSION = 'list'
blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(blueprint, version=0.1, title='AT Pipeline API', doc='/swagger/')
app.register_blueprint(blueprint)
backendAPI = api.namespace('backend', description='Manage ATExplorer backend API.')

I want two endpoints, version , returning the version of the backend, eg 0.1 and status , returning the status of the backend, eg 'stopped', or 'running' 我想要两个端点, 版本 ,返回后端的版本,例如0.1和status ,返回后端的状态,例如'stopped'或'running'

I created a response class and routes like this: 我创建了一个响应类和这样的路由:

@backendAPI.route('/version', methods=['GET'])
@backendAPI.route('/status',  methods=['GET'])

class Backend(Resource):

     def get(self):
        #How to differentiate between the ../backend/version call and a 
        # ../backend/status call here??
        #if call is for status:
        #     return {'status': 'running'}
        #elif call is for version
        #     return {'version': '0.1'}

Guess this is a question of design. 猜猜这是一个设计问题。 What 'design' is preferable for this case? 对于这种情况,什么“设计”更可取?

---- EDIT ----- The above code can be rewritten, achieving a desired end (client) result by the following: ----编辑-----上面的代码可以重写,通过以下方式实现所需的结束(客户端)结果:

class Backend(Resource):
    def __init__(self, api=None, *args, **kwargs):
         #The 'actual' backend resource
         self.backend = atp.backend

@backend_api.route('/status',  methods=['GET'])
class Dummy(Backend):
     def get(self):
         return {'status':self.backend.getStatus()}

@backend_api.route('/version',  methods=['GET'])
class Dummy(Backend):
     def get(self):
         return {'status':self.backend.getVersion()}

It seem flask allow multiple classes with the same name, doing different things based on the 'route' decorator, ie the class name is irrelevant . 似乎烧瓶允许多个具有相同名称的类,根据'route'装饰器执行不同的操作,即类名无关紧要

Is there a 'text-book' way to express the above logic? 是否有表达上述逻辑的“教科书”方式?

I believe it's more of a flask_restplus question, because in Flask you usually use methods as routes. 我相信它更像是一个flask_restplus问题,因为在Flask中你通常使用方法作为路径。

Anyway, have you considered using two different classes for two different actions? 无论如何,您是否考虑过两个不同的类用于两个不同的操作?

@backendAPI.route('/version', methods=['GET'])
class Version(Resource):
    def get(self):
         return flask.jsonify({"version_number": "0.1"})

@backendAPI.route('/status', methods=['GET'])
class Status(Resource):
    def get(self):
         return flask.jsonify({"status": "running"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM