Skip to content

Commit a8a7b9d

Browse files
committed
Add sample application
1 parent c671893 commit a8a7b9d

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

examples/app/api.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Functions handling API endpoints."""
2+
3+
from models import Employee
4+
from models import db
5+
6+
7+
def search():
8+
"""Get all employees from the database."""
9+
employees = Employee.query.all()
10+
employee_dicts = map(lambda employee: employee.to_dict(), employees)
11+
return list(employee_dicts)
12+
13+
14+
def post(body):
15+
"""Save an employee to the database."""
16+
if Employee.query.filter_by(id=body["id"]).first() is not None:
17+
return ("Employee already exists.", 400)
18+
19+
employee = Employee(**body)
20+
db.session.add(employee)
21+
db.session.commit()
22+
23+
24+
def get(id):
25+
"""Get an employee from the database."""
26+
employee = Employee.query.filter_by(id=id).first()
27+
if employee is None:
28+
return ("Employee not found.", 404)
29+
return employee.to_dict()
30+
31+
32+
def patch(body, id):
33+
"""Update an employee in the dayabase."""
34+
employee = Employee.query.filter_by(id=id).first()
35+
if employee is None:
36+
return ("Employee not found.", 404)
37+
employee.name = body["name"]
38+
employee.division = body["division"]
39+
employee.salary = body["salary"]
40+
db.session.commit()
41+
return 200
42+
43+
44+
def delete(id):
45+
"""Delete an employee from the database."""
46+
result = Employee.query.filter_by(id=id).delete()
47+
if not result:
48+
return ("Employee not found.", 404)
49+
db.session.commit()
50+
return 200

examples/app/api.yaml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
openapi: "3.0.0"
3+
4+
info:
5+
title: Test Schema
6+
description: API to illustrate OpenAPI-SQLALchemy.
7+
version: "1.0"
8+
9+
paths:
10+
/employee:
11+
get:
12+
operationId: api.search
13+
summary: Used to retrieve all employees.
14+
responses:
15+
200:
16+
description: Return all employees from the database.
17+
content:
18+
application/json:
19+
schema:
20+
type: array
21+
items:
22+
$ref: "#/components/schemas/Employee"
23+
post:
24+
operationId: api.post
25+
summary: Used to save an employee to the database.
26+
requestBody:
27+
description: The employee to save.
28+
required: true
29+
content:
30+
application/json:
31+
schema:
32+
$ref: "#/components/schemas/Employee"
33+
responses:
34+
200:
35+
description: Save successful.
36+
400:
37+
description: The Employee already exists.
38+
/employee/{id}:
39+
get:
40+
operationId: api.get
41+
summary: Used to retrieve an Employee from the database.
42+
parameters:
43+
- in: path
44+
name: id
45+
schema:
46+
type: integer
47+
required: true
48+
responses:
49+
200:
50+
description: The Employee.
51+
content:
52+
application/json:
53+
schema:
54+
$ref: "#/components/schemas/Employee"
55+
404:
56+
description: The Employee was not found.
57+
patch:
58+
operationId: api.patch
59+
summary: Update an Employee in the database.
60+
parameters:
61+
- in: path
62+
name: id
63+
schema:
64+
type: integer
65+
required: true
66+
requestBody:
67+
description: The employee to save.
68+
required: true
69+
content:
70+
application/json:
71+
schema:
72+
$ref: "#/components/schemas/Employee"
73+
responses:
74+
200:
75+
description: The Employee was updated.
76+
404:
77+
description: The Employee was not found.
78+
delete:
79+
operationId: api.delete
80+
summary: Delete an Employee from the database.
81+
parameters:
82+
- in: path
83+
name: id
84+
schema:
85+
type: integer
86+
required: true
87+
responses:
88+
200:
89+
description: The Employee was deleted.
90+
404:
91+
description: The Employee was not found.
92+
93+
components:
94+
schemas:
95+
Employee:
96+
description: Person that works for a company.
97+
type: object
98+
x-tablename: employee
99+
properties:
100+
id:
101+
type: integer
102+
description: Unique identifier for the employee.
103+
example: 0
104+
x-primary-key: true
105+
x-autoincrement: true
106+
name:
107+
type: string
108+
description: The name of the employee.
109+
example: David Andersson.
110+
x-index: true
111+
division:
112+
type: string
113+
description: The part of the company the employee works in.
114+
example: Engineering
115+
x-index: true
116+
salary:
117+
type: number
118+
description: The amount of money the employee is paid.
119+
example: 1000000.00
120+
required:
121+
- id
122+
- name
123+
- division

examples/app/app.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Application code."""
2+
3+
import connexion
4+
5+
from models import db
6+
7+
app = connexion.FlaskApp(__name__, specification_dir=".")
8+
app.app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
9+
db.init_app(app.app)
10+
with app.app.app_context():
11+
db.create_all()
12+
app.add_api("api.yaml")
13+
app.run(port=8080)

examples/app/models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Database models."""
2+
3+
from flask_sqlalchemy import SQLAlchemy
4+
from yaml import safe_load
5+
6+
from openapi_sqlalchemy import init_model_factory
7+
8+
db = SQLAlchemy()
9+
10+
with open("api.yaml") as spec_file:
11+
SPEC = safe_load(spec_file)
12+
MODEL_FACTORY = init_model_factory(base=db.Model, spec=SPEC)
13+
14+
15+
class Employee(MODEL_FACTORY(name="Employee")):
16+
"""Employee model with serialization function."""
17+
18+
def to_dict(self):
19+
"""Convert Employee to a dictionary."""
20+
return {
21+
"id": self.id,
22+
"name": self.name,
23+
"division": self.division,
24+
"salary": self.salary,
25+
}

examples/app/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connexion[swagger-ui]==2.4
2+
Flask-SQLAlchemy==2.4

0 commit comments

Comments
 (0)