|
5 | 5 | import orm
|
6 | 6 | from connexion import NoContent
|
7 | 7 |
|
8 |
| -db_session = None |
9 |
| - |
10 | 8 |
|
11 | 9 | def get_pets(limit, animal_type=None):
|
12 |
| - q = db_session.query(orm.Pet) |
13 |
| - if animal_type: |
14 |
| - q = q.filter(orm.Pet.animal_type == animal_type) |
15 |
| - return [p.dump() for p in q][:limit] |
| 10 | + with db_session_factory() as db_session: |
| 11 | + q = db_session.query(orm.Pet) |
| 12 | + if animal_type: |
| 13 | + q = q.filter(orm.Pet.animal_type == animal_type) |
| 14 | + return [p.dump() for p in q][:limit] |
16 | 15 |
|
17 | 16 |
|
18 | 17 | def get_pet(pet_id):
|
19 |
| - pet = db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).one_or_none() |
20 |
| - return pet.dump() if pet is not None else ("Not found", 404) |
| 18 | + with db_session_factory() as db_session: |
| 19 | + pet = db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).one_or_none() |
| 20 | + return pet.dump() if pet is not None else ("Not found", 404) |
21 | 21 |
|
22 | 22 |
|
23 | 23 | def put_pet(pet_id, pet):
|
24 |
| - p = db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).one_or_none() |
25 |
| - pet["id"] = pet_id |
26 |
| - if p is not None: |
27 |
| - logging.info("Updating pet %s..", pet_id) |
28 |
| - p.update(**pet) |
29 |
| - else: |
30 |
| - logging.info("Creating pet %s..", pet_id) |
31 |
| - pet["created"] = datetime.datetime.utcnow() |
32 |
| - db_session.add(orm.Pet(**pet)) |
33 |
| - db_session.commit() |
34 |
| - return NoContent, (200 if p is not None else 201) |
| 24 | + with db_session_factory() as db_session: |
| 25 | + p = db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).one_or_none() |
| 26 | + pet["id"] = pet_id |
| 27 | + if p is not None: |
| 28 | + logging.info("Updating pet %s..", pet_id) |
| 29 | + p.update(**pet) |
| 30 | + else: |
| 31 | + logging.info("Creating pet %s..", pet_id) |
| 32 | + pet["created"] = datetime.datetime.now(datetime.UTC) |
| 33 | + db_session.add(orm.Pet(**pet)) |
| 34 | + db_session.commit() |
| 35 | + return NoContent, (200 if p is not None else 201) |
35 | 36 |
|
36 | 37 |
|
37 | 38 | def delete_pet(pet_id):
|
38 |
| - pet = db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).one_or_none() |
39 |
| - if pet is not None: |
40 |
| - logging.info("Deleting pet %s..", pet_id) |
41 |
| - db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).delete() |
42 |
| - db_session.commit() |
43 |
| - return NoContent, 204 |
44 |
| - else: |
45 |
| - return NoContent, 404 |
| 39 | + with db_session_factory() as db_session: |
| 40 | + pet = db_session.query(orm.Pet).filter(orm.Pet.id == pet_id).one_or_none() |
| 41 | + if pet is not None: |
| 42 | + logging.info("Deleting pet %s..", pet_id) |
| 43 | + db_session.delete(pet) |
| 44 | + db_session.commit() |
| 45 | + return NoContent, 204 |
| 46 | + else: |
| 47 | + return NoContent, 404 |
46 | 48 |
|
47 | 49 |
|
48 | 50 | logging.basicConfig(level=logging.INFO)
|
49 |
| -db_session = orm.init_db("sqlite:///:memory:") |
| 51 | +db_session_factory = orm.init_db() |
| 52 | +pets = { |
| 53 | + 1: {"name": "Aldo", "animal_type": "cat"}, |
| 54 | + 2: {"name": "Bailey", "animal_type": "dog"}, |
| 55 | + 3: {"name": "Hugo", "animal_type": "cat"}, |
| 56 | +} |
| 57 | +for id_, pet in pets.items(): |
| 58 | + put_pet(id_, pet) |
50 | 59 | app = connexion.FlaskApp(__name__, specification_dir="spec")
|
51 | 60 | app.add_api("openapi.yaml")
|
52 | 61 | app.add_api("swagger.yaml")
|
53 | 62 |
|
54 |
| -application = app.app |
55 |
| - |
56 |
| - |
57 |
| -@application.teardown_appcontext |
58 |
| -def shutdown_session(exception=None): |
59 |
| - db_session.remove() |
60 |
| - |
61 | 63 |
|
62 | 64 | if __name__ == "__main__":
|
63 | 65 | app.run(port=8080, reload=False)
|
0 commit comments