I am trying to abstract away some of the route class logic (i.e. I am looking to dynamically generate routes). api.add_resource
seemed like the right place to do this.
So this is what I am trying to do:
# app.py
from flask import Flask
from flask_restplus import Api, Resource, fields
from mylib import MyPost
# Define my model
json_model = api.schema_model(...)
api.add_resource(
MyPost,
'/acme',
resource_class_kwargs={"json_model": json_model}
)
And then in mylib:
# mylib.py
def validate_endpoint(f):
def wrapper(*args, **kwargs):
return api.expect(json_fprint)(f(*args, **kwargs))
return wrapper
class MyPost(Resource):
def __init__(self, *args, **kwargs):
# Passed in via api.add_resource
self.api = args[0]
self.json_model = kwargs['json_model']
# I can't do this because I don't have access to 'api' here...
# @api.expect(json_model)
# So I am trying to make this work
@validate_endpoint
def post(self):
return {"data":'some data'}, 200
I don’t have access to the global api object here so I can’t call @api.expect(json_model)
. But I do have access to api
and json_model
inside of the post method. Which is why I am trying to create my own validate_endpoint
decorator.
This does not work though. Is what I am trying to do here even possible? Is there a better approach I should be taking?