forked from bodbdigr/restea
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
63 lines (48 loc) · 1.54 KB
/
example.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
import random
from flask import Flask
from restea import errors
from restea.resource import Resource
from restea.adapters.flaskwrap import FlaskResourceWrapper
from restea import fields
app = Flask(__name__)
# Dummy data for the Resource
sites = [
{
'id': i,
'name': 'my__site_{}'.format(i),
'title': 'my site #{}'.format(i),
'rating': random.randint(1, 5),
'domain': 'www.my_domain_for_site_{}.com'.format(i),
'anoher_field_out_of_scope': 'this one shouldn\'t be seen'
} for i in range(1, 20)
]
def add_dummy_data(func):
def wrapper(self, *args, **kwargs):
res = func(self, *args, **kwargs)
if isinstance(res, dict):
res['dummy_key'] = 'dummy value'
return res
return wrapper
class SiteResource(Resource):
decorators = [add_dummy_data]
fields = fields.FieldSet(
id=fields.Integer(required=True, range=(1, 100)),
name=fields.String(max_length=50, required=True),
title=fields.String(max_length=150),
created_at=fields.DateTime(null=True),
)
def list(self):
return sites
def show(self, iden):
self.set_header('Access-Control-Allow-Origin', '*')
try:
return sites[int(iden)]
except IndexError:
raise errors.NotFoundError('Site doesn\'t exist', code=10)
def edit(self, iden):
return self.payload
with app.app_context():
FlaskResourceWrapper(SiteResource).get_routes('/v1/sites')
if __name__ == '__main__':
app.debug = True
app.run()