masterPiece93 / flask-docs

Collection of Best Practices , New Project Design Patterns in Flask , Solutions to the Problems faced .
0 stars 0 forks source link

IDEAS #1

Open masterPiece93 opened 1 year ago

masterPiece93 commented 1 year ago
def not_implemented(*dargs, **dkwargs):
    """
    Works in 2 ways :
    1. [ mentioned paths ]
        @not_implemented('/path/to/view/p1','/path/to/view/p2')
        def view():
            ...
        will block => '/path/to/view/p1' and '/path/to/view/p2' , return -> NotImplemented
    2.  [ ordering ]
        @app.route('/path/to/view/p1')
        @not_implemented()
        @app.route('/path/to/view/p2')
        def view():
            ...
        will block => '/path/to/view/p1' , but will not block => '/path/to/view/p2'
    """
    def _not_implemented(api_func):
        # does not support checking of parameterized routes
        def _wrapper(*args, **kwargs):
            if len(dargs) == 0 or request.path in dargs:
                return (
                    ApiResponse(
                        message=HTTPStatus.NOT_IMPLEMENTED.description,
                        status=HTTPStatus.NOT_IMPLEMENTED.phrase,
                    ).dict(),
                    HTTPStatus.NOT_IMPLEMENTED,
                )
            return api_func(*args, **kwargs)
        _wrapper.__name__ = f"_wrapper_{api_func.__name__}"
        return _wrapper
    return _not_implemented
masterPiece93 commented 1 year ago

@not_implemented not working in every condition , must be tested

meanwhile , we can use :-

def not_implemented(api_func):
    def _wrapper(*args, **kwargs):
        return (
            ApiResponse(
                message=HTTPStatus.NOT_IMPLEMENTED.description,
                status=HTTPStatus.NOT_IMPLEMENTED.phrase,
            ).dict(),
            HTTPStatus.NOT_IMPLEMENTED,
        )
    _wrapper.__name__ = f"_wrapper_{api_func.__name__}"
    return _wrapper