diff --git a/.gitignore b/.gitignore index 5cba9ec..3129b8a 100644 --- a/.gitignore +++ b/.gitignore @@ -159,3 +159,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +cookies.txt \ No newline at end of file diff --git a/examples/01_authentication/README.md b/examples/01_authentication/README.md index b349a18..3327f22 100644 --- a/examples/01_authentication/README.md +++ b/examples/01_authentication/README.md @@ -25,9 +25,80 @@ curl to store/use cookies in order to test authenticated requests. ## CURL Commands ### Login -`curl -X POST --location "http://127.0.0.1:5000/api/v1/login"` +``` +curl -X POST -c cookies.txt --location "http://127.0.0.1:5000/auth/login" +``` ### Logout -`curl -X POST --location "http://127.0.0.1:5000/api/v1/logout"` +``` +curl -X POST -c cookies.txt --location "http://127.0.0.1:5000/auth/logout" +``` +### Create a ToDo item +``` +curl -X POST -b cookies.txt --location "http://127.0.0.1:5000/todos/" \ + -H "Content-Type: application/json" \ + -d "{ + \"text\": \"take out garbage again\" + }" +``` +### List all ToDo items (flat) +``` +curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/" \ + -d "Accept: application/json" +``` + +### List all ToDo items (paginated) +``` +curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?limit=2&offset=1" \ + -d "Accept: application/json" +``` + +### Search ToDo items +``` +curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?search=garbage" \ + -d "Accept: application/json" +``` + +### Filter ToDo items +``` +curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?filters=%7B%22text%22%3A+%22take+out+garbage+again%22%7D" \ + -d "Accept: application/json" +``` +_querystring urldecodes to `filters={"text": "take out garbage again"}`_ + +### Sort ToDo items +``` +curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?sort=text" \ + -d "Accept: application/json" +``` + +### Fetch ToDo item +``` +curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" \ + -d "Accept: application/json" +``` + +### Update ToDo item +``` +curl -X PUT -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" \ + -H "Content-Type: application/json" \ + -d "{ + \"text\": \"Updated todo item\" + }" +``` + +### Patch ToDo item +``` +curl -X PATCH -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" \ + -H "Content-Type: application/json" \ + -d "{ + \"text\": \"Updated todo item\" + }" +``` + +### Delete ToDo Item +``` +curl -X DELETE -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" +``` diff --git a/examples/01_authentication/app.py b/examples/01_authentication/app.py index 0e8b392..c0ab8e4 100644 --- a/examples/01_authentication/app.py +++ b/examples/01_authentication/app.py @@ -11,11 +11,14 @@ from marshmallow import fields as mf from sqlalchemy.orm import DeclarativeBase +from flask_muck import FlaskMuck from flask_muck.views import FlaskMuckApiView # Create a Flask app app = Flask(__name__) app.config["SECRET_KEY"] = "super-secret" +muck = FlaskMuck() +muck.init_app(app) # Init Flask-SQLAlchemy and set database to a local sqlite file. @@ -50,8 +53,8 @@ class TodoSchema(ma.Schema): text = mf.String(required=True) -# Add a Flask blueprint for the base of the REST API and register it with the app. -api_blueprint = Blueprint("v1_api", __name__, url_prefix="/api/v1/") +# Add a Flask blueprint to organize authentication views. +auth_blueprint = Blueprint("auth", __name__, url_prefix="/auth") # Init Flask-Login for user authentication and add login/logout endpoints. @@ -64,7 +67,7 @@ def load_user(user_id): return UserModel.query.get(user_id) -@api_blueprint.route("login", methods=["POST"]) +@auth_blueprint.route("login", methods=["POST"]) def login_view(): """Dummy login view that creates a User and authenticates them.""" user = UserModel() @@ -74,7 +77,7 @@ def login_view(): return {}, 200 -@api_blueprint.route("logout", methods=["POST"]) +@auth_blueprint.route("logout", methods=["POST"]) def logout_view(): logout_user() return {}, 200 @@ -102,13 +105,11 @@ class TodoApiView(BaseApiView): searchable_columns = [TodoModel.text] -# Add all url rules to the blueprint. -TodoApiView.add_rules_to_blueprint(api_blueprint) - -# Register api blueprint with the app. -app.register_blueprint(api_blueprint) +# Register auth blueprint with the app. +app.register_blueprint(auth_blueprint) if __name__ == "__main__": with app.app_context(): db.create_all() + muck.register_muck_views([TodoApiView]) app.run(debug=True)