Skip to content

Commit

Permalink
chore: update example 01 docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtiesling committed Feb 15, 2024
1 parent 34178ce commit cfc5ad3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
75 changes: 73 additions & 2 deletions examples/01_authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
```
19 changes: 10 additions & 9 deletions examples/01_authentication/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit cfc5ad3

Please sign in to comment.