Ciasto piekarz
Ciasto piekarz

Reputation: 8277

How to have get and post endpoint from same flask-restplus Resource class?

I have the below class that inherits from flask-restplus.Resource.

class Patient(Resource):
    """ Patient endpoint."""
    @clinic_api_ns.route("/patient/add/")
    def post(self):
        # TODO: add a patient.
        return {}

    @clinc_api_ns.route("/patient/<string:name>")
    def get(self, name):
        # TODO: get a patient record
        return {}   

I want to achieve 2 endpoints from the above but it doesnt work it throws error:

/site-packages/flask_restplus/api.py", line 287, in _register_view resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args, AttributeError: 'function' object has no attribute 'as_view'

Upvotes: 1

Views: 1878

Answers (2)

user2076865
user2076865

Reputation: 11

You need to use the decorators at the class instead of the functions.

Your API will hit the same endpoint "/patient" and the method will determine which function is called. Get, post, put, and delete.

If you need different API endpoint you will need 2 Resource classes, one for each path.

Upvotes: 1

alecxe
alecxe

Reputation: 474131

From what I understand, .route() decorators are meant to be used on Resource classes only - not their methods. You have to define two separate resources - similar to how Todo and TodoList are defined in the full example in the docs.

Upvotes: 0

Related Questions