-
Notifications
You must be signed in to change notification settings - Fork 0
/
pet_shop.py
460 lines (397 loc) · 9.43 KB
/
pet_shop.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
from brontosaurus import API
desc = """
This is a sample Petstore server. You can find out more about Brontosaurus
at http://spacejam.com. For this sample, you can use the api key
special-key to test the authorization filters.
"""
api = API("Brontosaurus Petstore", desc, doc_path='test/examples/docs/pet_shop_api.md')
# Pet status type
status = {
'type': 'string',
'enum': ['available', 'pending', 'sold']
}
# Saved pet schema
pet = api.register({
'$id': '#pet',
'type': 'object',
'additionalProperties': False,
'required': ['id', 'category', 'name', 'status'],
'properties': {
'id': {
'type': 'integer',
'examples': [0, 1, 2]
},
'category': {
'$ref': '#category'
},
'name': {
'type': 'string',
'examples': ['Buster', 'Lil Doof']
},
'photoUrls': {
'type': 'array',
'items': {
'type': 'string',
'format': 'uri'
},
'examples': [['https://spacejam.com/img/p-jamlogo.gif']]
},
'tags': {
'type': 'array',
'items': {
'$ref': '#tag'
}
},
'status': status
}
})
# Saved order schema
order = api.register({
'$id': '#order',
'type': 'object',
'additionalProperties': False,
'required': ['id', 'petId', 'quantity', 'shipDate', 'status', 'complete'],
'properties': {
'id': {
'type': 'integer'
},
'petId': {
'type': 'integer'
},
'quantity': {
'type': 'integer',
'minimum': 1
},
'shipDate': {
'type': 'string',
'format': 'date-time'
},
'status': status,
'complete': {
'type': 'boolean'
}
}
})
# Params for updating or creating an order
new_order = {
'type': 'object',
'required': ['petId', 'quantity'],
'properties': {
'petId': {
'type': 'integer',
'minimum': 0
},
'quantity': {
'type': 'integer',
'minimum': 1
}
}
}
# Schema for an existing user
user = api.register({
'$id': '#user',
'type': 'object',
'additionalProperties': False,
'required': ['id', 'username', 'email', 'password', 'userStatus'],
'properties': {
'id': {
'type': 'integer'
},
'username': {
'type': 'string'
},
'firstName': {
'type': 'string'
},
'lastName': {
'type': 'string'
},
'email': {
'type': 'string',
'format': 'email'
},
'password': {
'type': 'string',
'minLength': 7
},
'phone': {
'type': 'string'
},
'userStatus': {
'type': 'integer'
}
}
})
# Schema for an existing category with an ID
category = api.register({
'$id': '#category',
'type': 'object',
'additionalProperties': False,
'required': ['id', 'name'],
'properties': {
'id': {
'type': 'integer'
},
'name': {
'type': 'string'
}
}
})
# Schema for an existing tag with an ID
tag = api.register({
'$id': '#tag',
'type': 'object',
'additionalProperties': False,
'required': ['id', 'name'],
'properties': {
'id': {
'type': 'integer'
},
'name': {
'type': 'string'
}
}
})
# Generic schema for fetching a pet, order, or user using an ID
id_schema = {
'type': 'object',
'additionalProperties': False,
'required': ['id'],
'properties': {
'id': {
'type': 'integer'
}
}
}
@api.method('get_pet', 'Fetch a pet by ID')
@api.params(id_schema)
@api.result(pet)
def get_pet(params, header):
pass
# Fields you can update for a pet
pet_update = {
'type': 'object',
'additionalProperties': False,
'properties': {
'name': {
'type': 'string'
},
'category': {
'$ref': '#category'
},
'photoUrls': {
'type': 'array',
'items': {
'type': 'string',
'format': 'uri'
}
},
'tags': {
'type': 'array',
'items': {
'$ref': '#tag'
}
},
'status': status
}
}
# Fields for creating a new pet
pet_new = {
**pet_update,
'required': ['name', 'category', 'status'],
} # type: dict
# Fields for updating a pet by ID
update_pet_params = {
'type': 'object',
'additionalProperties': False,
'required': ['id', 'pet'],
'properties': {
'id': {
'description': 'ID of the pet you want to update.',
'type': 'integer'
},
'pet': pet_update
}
}
@api.method('update_pet', 'Update a pet by ID')
@api.params(update_pet_params)
@api.result(pet)
def update_pet(params, headers):
pass
@api.method('create_pet', 'Create a new pet entry')
@api.params(pet_new)
@api.result(pet)
def create_pet(params, headers):
pass
@api.method('delete_pet', 'Delete a pet entry by ID')
@api.params(id_schema)
@api.require_header('Authorization', r'^token .+$')
def delete_pet(params, headers):
pass
statuses = {
'type': 'object',
'additionalProperties': False,
'required': 'any_status',
'properties': {
'any_status': {
'type': 'array',
'minLength': 1,
'items': status
}
}
}
pet_list = {
'type': 'array',
'items': {'$ref': '#pet'}
}
@api.method('find_pet_by_status', 'Find a pet that has any of the given statuses.')
@api.params(statuses)
@api.result(pet_list)
def find_pet_by_status(params, headers):
pass
tag_query = {
'type': 'object',
'additionalProperties': False,
'required': 'any_tag',
'properties': {
'any_tag': {
'type': 'array',
'minLength': 1,
'items': {
'type': 'string',
'title': 'Tag Name'
}
}
}
}
@api.deprecated('This method is no longer supported for some reason or another.')
@api.method('find_pet_by_tags', 'Find a pet that has any of the given tags.')
@api.params(tag_query)
@api.result(pet_list)
def find_by_tags(parasms, headers):
pass
store_inventory_statuses = {
'type': 'object',
'required': ['available', 'pending', 'sold'],
'properties': {
'available': {
'type': 'integer',
'minimum': 0
},
'pending': {
'type': 'integer',
'minimum': 0
},
'sold': {
'type': 'integer',
'minimum': 0
}
}
}
@api.method('get_store_inventory', 'Return a map of statuses to quantities')
@api.result(store_inventory_statuses)
def pet_store_inventory(params, headers):
pass
@api.method('get_order', 'Get a purchase order by ID')
@api.params(id_schema)
@api.result(order)
def get_order(params, headers):
pass
@api.method('delete_order', 'Delete a purchase order by ID')
@api.params(id_schema)
def delete_order(params, headers):
pass
@api.method('create_order', 'Place an order for a pet.')
@api.params(new_order)
@api.result(order)
def create_order(params, headers):
pass
username_query = {
'type': 'object',
'required': ['username'],
'additionalProperties': False,
'properties': {
'username': {
'type': 'string',
'minLength': 3
}
}
}
@api.method('get_user', 'Fetch a user by username')
@api.params(username_query)
@api.result(user)
def get_user(params, headers):
pass
new_user = {
'description': 'Properties to set for the user',
'type': 'object',
'properties': {
'username': {
'type': 'string'
},
'firstName': {
'type': 'string'
},
'lastName': {
'type': 'string'
},
'email': {
'type': 'string',
'format': 'email'
},
'password': {
'type': 'string',
'minLength': 7
},
'phone': {
'type': 'string'
}
}
}
user_update_params = {
'type': 'object',
'required': ['username_to_update', 'user'],
'properties': {
'username_to_update': {
'type': 'string',
'description': "Username for the account we'd like to update"
},
'user': new_user
}
}
@api.method('update_user', 'Update a user')
@api.params(user_update_params)
@api.result(user)
def update_user(params, headers):
pass
@api.method('delete_user', 'Delete a user')
@api.params(id_schema)
def delete_user(params, headers):
pass
login_params = {
'type': 'object',
'required': ['username', 'password'],
'additionalProperties': False,
'properties': {
'username': {
'type': 'string',
'minLength': 3
},
'password': {
'type': 'password',
'minLength': 7
}
}
}
@api.method('login', 'Logs user into the system')
@api.params(login_params)
def login(params, headers):
pass
@api.method('logout', 'Logs out a currently logged-in user')
def logout(params, headers):
pass
@api.method('create_user', 'Create a new user account')
@api.params(new_user)
@api.result(user)
def create_user(params, headers):
pass