-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloft_api.py
More file actions
292 lines (229 loc) · 7.08 KB
/
loft_api.py
File metadata and controls
292 lines (229 loc) · 7.08 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
class Loft(Api):
"""
"""
title = 'Loft API'
version = '1.0.1'
description = "Loft api / service for the Loft family of websites."
contact = Contact(
name = "Api Support",
url = "http://help.loft.io",
email = "help@loft.io")
license = License(
name = "Apache 2.0",
url = "https://www.apache.org/licenses/LICENSE-2.0.html")
servers = [
Server(url="api.loft.io", description="Primary Server")
]
paths = {
'/pets': {'get': 'get_pets',
'post': 'add_pet'},
'/pets/{id}': {'put': 'put_pet',
'get': 'get_pet',
'delete': 'delete_pet'},
'/accounts': accounts.Collection
}
schemas = [
]
security = {
'default':
}
@operation(
method="get",
tags=['tag1', 'tag2'],
summary="Short summary",
external_docs={'description': 'Find out more info here', 'url': 'http://help.loft.io/loft-api/pets'},
args = {
'kind': fields.String(in="query", description="kind of pet to filter", deprecated=True),
},
result = http.Ok(schema=Pet, repeated=True, description="all pets matching the filter"),
error =
responses=[
Ok(schema=Pet, repeated=True, description="name of the pet"),
],
responses={
'success': Success(schema=Pet, repeated=True, description="name of the pet")
}
)
def get_pets(kind, pet):
"""
Gets all the pets you want to see.
-- Contract --
kind:
in: query
schema: string
deprecated: true
description: "kind of pets"
-- Operation --
*tags: pets, animals, family
*summary: Get your pets.
*external_docs: http://help.loft.io/loft-api/pets
*responses:
- hello
- hi
"""
return Success([pets])
@put(schema=Pet)
def put_pet(id, pet):
"""
Add or replace a pet resource.
-- Operation --
tags: pets, animals, family
summary: Put a pet
"""
pass
class PetCollection(ChildCollection):
home = ParentCollection('home_id', get_home_by_id)
def get(self):
return self.home.get_pets()
class PetCollection(SimpleCollection):
schema = Pet
def store(self, id, pet):
pass
def load(self, id):
pass
def audit(self, method, id, pet):
pass
Kinds = fields.String(choices=['dog', 'cat', 'bird'])
class Pet(Schema):
id = fields.AutoUUID()
name = fields.String()
age = fields.Integer()
kind = Kinds(required=True)
Pet.examples = [
{'name': 'Fred', 'age': 12, 'kind': 'dog'},
{'name': 'Mr. Puddles', 'age': 4, 'kind': 'cat'},
{'name': 'Birbert Brown', 'age': 6, 'kind': 'bird'},
]
@put(schema=Pet)
def put_pet(id, pet):
"""
Add or replace a pet resource.
-- Operation --
tags: pets, animals, family
summary: Put a pet
"""
pass
def put_pet(id, pet):
"""
Add or replace a pet resource.
-- Operation --
tags: pets, animals, family
summary: Put a pet
servers:
- alt.example.com: 'Alternative Server'
"""
pass
define_operation(
callable=put_pet,
method="put",
operation_id="petput",
tags=["pets", "animals", "family"],
description="Add or replace a pet resource",
summary="Put a pet",
parameters = {
'id': fields.PathString('id of the pet')
},
request_body = {
'*/*':
fields.RequestBody(schema=Pet),
}
},
responses = {
'200': {
http.Response(description="Updated pet object.", schema=Pet)
}
},
callbacks = {
'onData': data_callback
},
deprecated = False,
security = ,
servers = [
{'url': 'alt.example.com', 'description': 'Alternative Server'},
])
class HouseCollection(Collection):
title = "House"
schema = House
storages = {
'google': GoogleStorage('house'),
'elastic': ElasticStorage('house')
}
query_filters = {
'q': fields.QueryString('search term')
}
def query(self, q):
return this.storages['elastic'].search({'query': q})
def enrich(self, house):
return house
class PetCollection(Collection):
title = "Pet"
schema = Pet
parent = HouseCollection
storages = [GoogleStorage('pet'), ElasticStorage('pet')]
query = ElasticQuery('pet')
enrich = {
'toys': ToyCollection,
'owner': UserCollection
}
class Loft_v2(Loft):
paths = {
'/houses': HouseCollection,
'/houses/{house_id}/pets': PetCollection,
}
class Pet(Schema):
toys = fields.Enrichment(Toy, 'id', repeated=True)
class Collection(Reconfigurable):
title = fields.String
schema = fields.InstanceOf(Schema)
parent = fields.InstanceOf(Reconfigurable)
storage = fields.InstanceOf(StorageEngine)
query = fields.InstanceOf(QueryEngine)
enrich = fields.InstanceOf(Reconfigurable, mapping=True)
query_filters = fields.InstanceOf(fields.Field, mapping=True)
paths = fields.InstanceOf(Dict, mapping=True, default={
'/': {'get': 'query_items',
'post': 'post_item'},
'/{id}': {'get': 'get_item',
'put': 'put_item',
'patch': 'patch_item',
'delete': 'delete_item'}
})
def create_operation(self, path, endpoint, **kwargs):
if isinstance(endpoint, str):
endpoint = getattr(self, str)
self.paths.setdefault()
def register(self, app, path, api):
for path, mapping in this.path.items():
if isinstance(mapping, dict):
for method, endpoint in mapping.items():
self.create_operation(path, endpoint, method=method)
else:
self.create_operation(path, mapping)
resource.register(app, path, self)
app.add_url_rule(path, )
def query_items(self, **filters):
pass
def post_item(self, item):
self.audit('write', None, item)
return self.storage.write(self.schema, None, item)
def get_item(self, id):
self.audit('read', id)
return self.storage.load(self.schema, id)
def put_item(self, id, item):
self.audit('write', id, item)
return self.storage.write(self.schema, id, item)
def patch_item(self, id, item):
self.audit('write', id, item)
return self.storage.write(self.schema, id, item, patch=True)
def delete_item(self, id):
self.audit('write', id)
self.storage.remove(self.schema, id)
def audit(self, perm, id, item):
self.security.audit(self, perm, id, item)
class Api(Blueprint):
def register(self, app, options, first_registration=False):
for path, resource in this.path.items():
if isinstance(resource, type):
resource = resource()
resource.register(app, path, self)
super().register(app, options, first_registration)