我正在使用flask-restful设置API REST。我没有找到任何文档,也没有找到如何在端点之间进行内部调用的示例。换句话说,我想从其他端点的实现内部使用其中一个端点。
我可以将其视为对另一个API的常规外部调用,但是我觉得我将以某种方式违反良好做法犯罪
from flask import Flask, request
from flask_restplus import Api, Resource
app = Flask(__name__)
api = Api(app, version='1.0', title='FooTitle',
description='FooDescription', )
ns_conf = api.namespace('FooName', description='FooDescription')
# Endpoint 1
@ns_conf.route('/endpoint1')
class MyResource(Resource):
def get(self):
return 'Hello '
# Endpoint 2
@ns_conf.route('/endpoint2')
@api.doc(params={'your_name': 'Your name'})
class greeting(Resource):
def get(self):
# Transfer request parameters to variables:
your_name= request.args.get('your_name')
# I WOULD LIKE TO GET THE OUTPUT OF ENDPOINT 1 here:
hello = call_endpoint_1()
return hello + str(your_name)
实现“ call_endpoint_1()”的正确方法是什么?
答案 0 :(得分:0)
非常简单:将公共部分分解为一个普通函数,然后从两个端点调用它:
def say_hello():
return "Hello "
@ns_conf.route('/endpoint1')
class MyResource(Resource):
def get(self):
return say_hello()
# Endpoint 2
@ns_conf.route('/endpoint2')
@api.doc(params={'your_name': 'Your name'})
class greeting(Resource):
def get(self):
# Transfer request parameters to variables:
your_name= request.args.get('your_name')
# I WOULD LIKE TO GET THE OUTPUT OF ENDPOINT 1 here:
hello = say_hello()
return hello + str(your_name)