flask-smorest icon indicating copy to clipboard operation
flask-smorest copied to clipboard

How to implement custom methods

Open BorjaEst opened this issue 4 years ago • 0 comments

I am following this guide to make my API as simple and standard as possible: https://cloud.google.com/apis/design/custom_methods

I see this method is quite common as also I could found it at API Design Patterns and it has sense.

It would be nice to have a simple way to implement this, for example: (In flask only also not easy...)

from flask.views import MethodView
from flask_smorest import Blueprint, abort

blp = Blueprint('benchmarks', 'benchmarks')

@blp.route('')
class Benchmarks(MethodView)

    def get(self, query_args):
        ....
  
    def post(self, body_args):
        ....

    def search(self, query_args):
        ... # GET Custom method here

    @blp.arguments(schemas.Benchmark)
    def move(self, query_args):
        ... # POST Custom method here

Which would allow the follwing methods:

  • GET /benchmarks
  • POST /benchmarks
  • GET /benchmarks:search
  • POST /benchmarks:move

I am open to ideas, basically the main problem I found is at https://github.com/pallets/flask/blob/main/src/flask/blueprints.py#L93 where '/' is the only option availabe to start the prefix.

Why not to use a resource path?

If I configure the following route:

  • GET /benchmarks/search

Then it might conflict with my id resource

  • GET /benchmarks/string:name

Threfore I cannot create a benchmark with name "search". This is a design issue, the ':' convention was created to avoid this type of issues.

BorjaEst avatar Sep 10 '21 08:09 BorjaEst