Skip to content

Commit cfc5ad3

Browse files
committed
chore: update example 01 docs.
1 parent 34178ce commit cfc5ad3

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,5 @@ cython_debug/
159159
# and can be added to the global gitignore or merged into this file. For a more nuclear
160160
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
161161
#.idea/
162+
163+
cookies.txt

examples/01_authentication/README.md

+73-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,80 @@ curl to store/use cookies in order to test authenticated requests.
2525
## CURL Commands
2626

2727
### Login
28-
`curl -X POST --location "http://127.0.0.1:5000/api/v1/login"`
28+
```
29+
curl -X POST -c cookies.txt --location "http://127.0.0.1:5000/auth/login"
30+
```
2931

3032
### Logout
31-
`curl -X POST --location "http://127.0.0.1:5000/api/v1/logout"`
33+
```
34+
curl -X POST -c cookies.txt --location "http://127.0.0.1:5000/auth/logout"
35+
```
3236

37+
### Create a ToDo item
38+
```
39+
curl -X POST -b cookies.txt --location "http://127.0.0.1:5000/todos/" \
40+
-H "Content-Type: application/json" \
41+
-d "{
42+
\"text\": \"take out garbage again\"
43+
}"
44+
```
3345

46+
### List all ToDo items (flat)
47+
```
48+
curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/" \
49+
-d "Accept: application/json"
50+
```
51+
52+
### List all ToDo items (paginated)
53+
```
54+
curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?limit=2&offset=1" \
55+
-d "Accept: application/json"
56+
```
57+
58+
### Search ToDo items
59+
```
60+
curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?search=garbage" \
61+
-d "Accept: application/json"
62+
```
63+
64+
### Filter ToDo items
65+
```
66+
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" \
67+
-d "Accept: application/json"
68+
```
69+
_querystring urldecodes to `filters={"text": "take out garbage again"}`_
70+
71+
### Sort ToDo items
72+
```
73+
curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/?sort=text" \
74+
-d "Accept: application/json"
75+
```
76+
77+
### Fetch ToDo item
78+
```
79+
curl -X GET -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" \
80+
-d "Accept: application/json"
81+
```
82+
83+
### Update ToDo item
84+
```
85+
curl -X PUT -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" \
86+
-H "Content-Type: application/json" \
87+
-d "{
88+
\"text\": \"Updated todo item\"
89+
}"
90+
```
91+
92+
### Patch ToDo item
93+
```
94+
curl -X PATCH -b cookies.txt --location "http://127.0.0.1:5000/todos/1/" \
95+
-H "Content-Type: application/json" \
96+
-d "{
97+
\"text\": \"Updated todo item\"
98+
}"
99+
```
100+
101+
### Delete ToDo Item
102+
```
103+
curl -X DELETE -b cookies.txt --location "http://127.0.0.1:5000/todos/1/"
104+
```

examples/01_authentication/app.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
from marshmallow import fields as mf
1212
from sqlalchemy.orm import DeclarativeBase
1313

14+
from flask_muck import FlaskMuck
1415
from flask_muck.views import FlaskMuckApiView
1516

1617
# Create a Flask app
1718
app = Flask(__name__)
1819
app.config["SECRET_KEY"] = "super-secret"
20+
muck = FlaskMuck()
21+
muck.init_app(app)
1922

2023

2124
# Init Flask-SQLAlchemy and set database to a local sqlite file.
@@ -50,8 +53,8 @@ class TodoSchema(ma.Schema):
5053
text = mf.String(required=True)
5154

5255

53-
# Add a Flask blueprint for the base of the REST API and register it with the app.
54-
api_blueprint = Blueprint("v1_api", __name__, url_prefix="/api/v1/")
56+
# Add a Flask blueprint to organize authentication views.
57+
auth_blueprint = Blueprint("auth", __name__, url_prefix="/auth")
5558

5659

5760
# Init Flask-Login for user authentication and add login/logout endpoints.
@@ -64,7 +67,7 @@ def load_user(user_id):
6467
return UserModel.query.get(user_id)
6568

6669

67-
@api_blueprint.route("login", methods=["POST"])
70+
@auth_blueprint.route("login", methods=["POST"])
6871
def login_view():
6972
"""Dummy login view that creates a User and authenticates them."""
7073
user = UserModel()
@@ -74,7 +77,7 @@ def login_view():
7477
return {}, 200
7578

7679

77-
@api_blueprint.route("logout", methods=["POST"])
80+
@auth_blueprint.route("logout", methods=["POST"])
7881
def logout_view():
7982
logout_user()
8083
return {}, 200
@@ -102,13 +105,11 @@ class TodoApiView(BaseApiView):
102105
searchable_columns = [TodoModel.text]
103106

104107

105-
# Add all url rules to the blueprint.
106-
TodoApiView.add_rules_to_blueprint(api_blueprint)
107-
108-
# Register api blueprint with the app.
109-
app.register_blueprint(api_blueprint)
108+
# Register auth blueprint with the app.
109+
app.register_blueprint(auth_blueprint)
110110

111111
if __name__ == "__main__":
112112
with app.app_context():
113113
db.create_all()
114+
muck.register_muck_views([TodoApiView])
114115
app.run(debug=True)

0 commit comments

Comments
 (0)