-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathdemo_autoapi.py
More file actions
executable file
·94 lines (67 loc) · 3.44 KB
/
demo_autoapi.py
File metadata and controls
executable file
·94 lines (67 loc) · 3.44 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
"""To use this demo, try entering the following commands in a terminal:
curl http://localhost:6767/todo/ | python -mjson.tool
curl -H "content-type: application/json" -f -X POST -d '{"id": "111b4bb7-55f5-441b-ba25-c7a4fd99442c", "text": "Watch more bsg", "order": 1}' http://localhost:6767/todo/111b4bb7-55f5-441b-ba25-c7a4fd99442c/ | python -m json.tool
curl -H "content-type: application/json" -f -X POST -d '{"id": "222b4bb7-55f5-441b-ba25-c7a4fd994421", "text": "Watch Blade Runner", "order": 2}' http://localhost:6767/todo/222b4bb7-55f5-441b-ba25-c7a4fd994421/ | python -m json.tool
curl http://localhost:6767/todo/ | python -mjson.tool
curl http://localhost:6767/todo/222b4bb7-55f5-441b-ba25-c7a4fd994421/ | python -mjson.tool
curl -H "content-type: application/json" -f -X DELETE http://localhost:6767/todo/222b4bb7-55f5-441b-ba25-c7a4fd994421/
curl http://localhost:6767/todo/ | python -mjson.tool
curl -H "content-type: application/json" -f -X POST -d '[{"id": "333b4bb7-55f5-441b-ba25-c7a4fd99442c", "text": "Write more Brubeck code", "order": 3},{"id": "444b4bb7-55f5-441b-ba25-c7a4fd994421", "text": "Drink coffee", "order": 4}]' http://localhost:6767/todo/ | python -m json.tool
curl http://localhost:6767/todo/ | python -mjson.tool
curl -H "content-type: application/json" -f -X POST -d '{"id": "b4bb7-55f5-441b-ba25-c7a4fd994421", "text": "Watch Blade Runner", "order": 2}' http://localhost:6767/todo/222b4bb7-55f5-441b-ba25-c7a4fd994421/ | python -m json.tool
curl -H "content-type: application/json" -f -X POST -d '{"id": "b4bb7-55f5-441b-ba25-c7a4fd994421", "text": "Watch Blade Runner", "order": 2}' http://localhost:6767/todo/b4bb7-55f5-441b-ba25-c7a4fd994421/ | python -m json.tool
"""
from brubeck.request_handling import Brubeck
from brubeck.autoapi import AutoAPIBase
from brubeck.queryset import DictQueryset
from brubeck.templating import Jinja2Rendering, load_jinja2_env
from brubeck.connections import Mongrel2Connection
from schematics.models import Model
from schematics.types import (UUIDType,
StringType,
BooleanType)
from schematics.serialize import wholelist
### Todo Model
class Todo(Model):
# status fields
id = UUIDType(auto_fill=True)
completed = BooleanType(default=False)
deleted = BooleanType(default=False)
archived = BooleanType(default=False)
title = StringType(required=True)
class Options:
roles = {
'owner': wholelist(),
}
### Todo API
class TodosAPI(AutoAPIBase):
queries = DictQueryset()
model = Todo
def render(self, **kwargs):
return super(TodosAPI, self).render(hide_status=True, **kwargs)
### Flat page handler
class TodosHandler(Jinja2Rendering):
def get(self):
"""A list display matching the parameters of a user's dashboard. The
parameters essentially map to the variation in how `load_listitems` is
called.
"""
return self.render_template('todos.html')
###
### Configuration
###
# Routing config
handler_tuples = [
(r'^/$', TodosHandler),
]
# Application config
config = {
'msg_conn': Mongrel2Connection('tcp://127.0.0.1:9999', 'tcp://127.0.0.1:9998'),
'handler_tuples': handler_tuples,
'template_loader': load_jinja2_env('./templates/autoapi'),
}
# Instantiate app instance
app = Brubeck(**config)
app.register_api(TodosAPI)
app.run()