调用flask restful API资源方法

use*_*153 15 python flask flask-restful

我正在使用Flask创建一个用于移动平台的API,但我也希望应用程序本身能够消化API以呈现Web内容.我想知道在Flask中访问API资源方法的最佳方法是什么?例如,如果我将以下类添加为资源:

class FooAPI(Resource):
    def __init__(self):
        # Do some things
        super(FooAPI, self).__init__()
    def post(self, id):
        #return something
    def get(self):
        #return something

api = Api(app)
api.add_resource(FooAPI, '/api/foo', endpoint = 'foo')
Run Code Online (Sandbox Code Playgroud)

然后在我想要的控制器中:

@app.route("/bar")
def bar():
   #Get return value from post() in FooAPI
Run Code Online (Sandbox Code Playgroud)

如何从FooAPI获取post()的返回值?我可以通过api变量以某种方式做到吗?或者我是否必须在控制器中创建FooAPI的实例?似乎必须有一个简单的方法来做到这一点,我只是不理解......

Mig*_*uel 13

应用程序使用API​​的显而易见的方法是像任何其他客户端一样调用它.应用程序同时充当服务器和客户端的事实并不重要,客户端部分可以将请求放入localhost,服务器部分将以与获取外部请求相同的方式获取它们.要生成HTTP请求,您可以使用标准库中的请求或urllib2.

但是虽然上面的方法可以正常工作,但对我来说似乎有些过分.在我看来,更好的方法是以常规应用程序和API都可以调用的方式公开应用程序的常用功能.例如,你可以有一个叫包FooLib实现所有共享的逻辑,则FooAPI成为各地的一个瘦包装FooLib,都FooAPIFooApp呼吁FooLib把事情做完.


Jes*_*sse 5

另一种方法是将应用程序和 API 放在同一个 Flask(-RESTful) 实例中。然后,您可以让应用程序在内部调用 API 方法/函数(不使用 HTTP)。让我们考虑一个管理服务器上文件的简单应用程序:

# API. Returns filename/filesize-pairs of all files in 'path'  
@app.route('/api/files/',methods=['GET'])
def get_files():
    files=[{'name':x,'size':sys.getsizeof(os.path.join(path,x))} for x in os.listdir(path)]
    return jsonify(files)

# app. Gets all files from the API, uses the API data to render a template for the user 
@app.route('/app/files/',methods=['GET'])
def app_get_files():
    response=get_files() # you may verify the status code here before continuing  
    return render_template('files.html',files=response.get_json())
Run Code Online (Sandbox Code Playgroud)

由于 Flask 的请求对象是global ,因此您可以推送所有请求(从 API 到应用程序并返回),而无需将它们包含在您的函数调用中。例如,对于处理文件上传的应用程序资源,您可以简单地调用:

@app.route('/app/files/post',methods=['POST'])
def app_post_file():
   response=post_file()
   flash('Your file was uploaded succesfully') # if status_code==200
   return render_template('home.html')

Run Code Online (Sandbox Code Playgroud)

关联的 API 资源是:

@app.route('/api/files/',methods=['POST'])
def post_file():
   file=request.files['file']
   ....
   ....
   return jsonify({'some info about the file upload'})
Run Code Online (Sandbox Code Playgroud)

但是,对于大量应用程序数据,包装/解包 JSON 的开销使得 Miguel 的第二个解决方案更可取。

在您的情况下,您希望在控制器中调用它:

response=FooAPI().post(id)
Run Code Online (Sandbox Code Playgroud)