0

I am creating a proxy with flask-restplus and I want to handle all http methods with one function

for example:

this is my restplus class

@api.route('/', defaults={'path': ''})
@api.route('/<path:path>')
class Access(Resource):
    @ip_limiter
    @path_limiter
    def get(self,path):
        data = request.json
        ip=request.remote_addr
        method=request.method
        response = AccessService.get_new_access(data,path,ip,method)
        return response

    @ip_limiter
    @path_limiter
    def post(self,path):
        data = request.json
        ip=request.remote_addr
        method=request.method0
        response = AccessService.get_new_access(data,path,ip,method)
        return response

    @ip_limiter
    @path_limiter
    def put(self,path):
        data = request.json
        ip=request.remote_addr
        method=request.method
        response = AccessService.get_new_access(data,path,ip,method)
        return response
    
    @ip_limiter
    @path_limiter
    def delete(self,path):
        data = request.json
        ip=request.remote_addr
        method=request.method
        response = AccessService.get_new_access(data,path,ip,method)
        return response

Is there a way to transform that in something like this?

@api.route('/', defaults={'path': ''})
@api.route('/<path:path>')
class Access(Resource):
    @ip_limiter
    @path_limiter
    def all(self,path):
        data = request.json
        ip=request.remote_addr
        method=request.method
        response = AccessService.get_new_access(data,path,ip,method)
        return response

is this possible with flask-restplus or do I have to find a workaround?

  • You may try to add `get = post = put = delete = all` after your `all()` declaration. *Like [this](https://tio.run/##K6gsycjPM7YoKPr/PzknsbhYwdGKSwEIUlLTFBI1ilNz0nQUyhKLNCGiIFBQlJlXogESgwsVpZaUFuUpgFRzgQWTFGwVkoE4BYgTubgcNTT1EjUMNfWSNIw09ZI1jDX1UjRMNP//BwA)*. – Olvin Roght Aug 02 '21 at 19:55
  • This might be relevant: https://stackoverflow.com/questions/45777770/catch-all-routes-for-flask – Alejandro A Aug 02 '21 at 19:58

0 Answers0