forked from hjacobs/connexion-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·52 lines (39 loc) · 1.23 KB
/
app.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
#!/usr/bin/env python3
import connexion
import datetime
import logging
from connexion import NoContent
# our memory-only pet storage
PETS = {}
def get_pets(limit, animal_type=None):
return {"pets": [pet for pet in PETS.values() if not animal_type or pet['animal_type'] == animal_type][:limit]}
def get_pet(pet_id):
pet = PETS.get(pet_id)
return pet or ('Not found', 404)
def put_pet(pet_id, pet):
exists = pet_id in PETS
pet['id'] = pet_id
if exists:
logging.info('Updating pet %s..', pet_id)
PETS[pet_id].update(pet)
else:
logging.info('Creating pet %s..', pet_id)
pet['created'] = datetime.datetime.utcnow()
PETS[pet_id] = pet
return NoContent, (200 if exists else 201)
def delete_pet(pet_id):
if pet_id in PETS:
logging.info('Deleting pet %s..', pet_id)
del PETS[pet_id]
return NoContent, 204
else:
return NoContent, 404
logging.basicConfig(level=logging.INFO)
app = connexion.App(__name__)
app.add_api('swagger.yaml')
# set the WSGI application callable to allow using uWSGI:
# uwsgi --http :8080 -w app
application = app.app
if __name__ == '__main__':
# run our standalone gevent server
app.run(port=8080, server='gevent')