forked from thomaxxl/safrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_relationship.py
executable file
·78 lines (58 loc) · 2.21 KB
/
demo_relationship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""
This demo application demonstrates the functionality of the safrs documented REST API
When safrs is installed, you can run this app:
$ python3 demo_relationship.py [Listener-IP]
This will run the example on http://Listener-Ip:5000
- An sqlite database is created and populated
- A jsonapi rest API is created
- Swagger documentation is generated
"""
import sys
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from safrs import SAFRSBase, SAFRSAPI
db = SQLAlchemy()
# Example sqla database objects
class User(SAFRSBase, db.Model):
"""
description: User description
"""
__tablename__ = "Users"
id = db.Column(db.String, primary_key=True)
name = db.Column(db.String, default="")
email = db.Column(db.String, default="")
books = db.relationship("Book", back_populates="user", lazy="dynamic")
class Book(SAFRSBase, db.Model):
"""
description: Book description
"""
__tablename__ = "Books"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, default="")
user_id = db.Column(db.String, db.ForeignKey("Users.id"))
user = db.relationship("User", back_populates="books")
# create the api endpoints
def create_api(app, host="localhost", port=5000, api_prefix=""):
api = SAFRSAPI(app, host=host, port=port, prefix=api_prefix)
api.expose_object(User)
api.expose_object(Book)
print(f"Created API: http://{host}:{port}/{api_prefix}")
def create_app(config_filename=None, host="localhost"):
app = Flask("demo_app")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://")
db.init_app(app)
with app.app_context():
db.create_all()
# Populate the db with users and a books and add the book to the user.books relationship
for i in range(200):
user = User(name=f"user{i}", email=f"email{i}@email.com")
book = Book(name=f"test book {i}")
user.books.append(book)
create_api(app, host)
return app
# address where the api will be hosted, change this if you're not running the app on localhost!
host = sys.argv[1] if sys.argv[1:] else "127.0.0.1"
app = create_app(host=host)
if __name__ == "__main__":
app.run(host=host)