-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
492 lines (461 loc) · 12.2 KB
/
Copy pathapp.py
File metadata and controls
492 lines (461 loc) · 12.2 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
from flask import Flask, jsonify, request, render_template
from flasgger import Swagger
from datetime import datetime
import uuid
app = Flask(__name__)
swagger = Swagger(app)
# In-memory storage for users (replace with database later)
users = [
{'id': 1, 'name': 'Alice'},
{'id': 2, 'name': 'Bob'},
{'id': 3, 'name': 'Charlie'}
]
next_user_id = 4
# In-memory storage for async tasks
async_tasks = {}
def generate_unique_id():
return str(uuid.uuid4())
# --- API Endpoints ---
@app.route('/api/users', methods=['GET'])
def get_users():
"""
Returns the list of all users.
---
tags:
- Users
responses:
200:
description: A list of users
schema:
type: array
items:
type: object
properties:
id:
type: integer
description: The user ID
name:
type: string
description: The user name
"""
return jsonify(users)
@app.route('/api/users', methods=['POST'])
def create_user():
"""
Creates a new user.
---
tags:
- Users
parameters:
- name: body
in: body
required: true
schema:
type: object
required:
- name
properties:
name:
type: string
description: Name for the new user
responses:
201:
description: User created successfully
schema:
type: object
properties:
id:
type: integer
name:
type: string
400:
description: Missing 'name' in request body or name is empty/contains only spaces
"""
global next_user_id
if not request.json or not 'name' in request.json:
return jsonify({'error': 'Missing name in request body'}), 400
name = request.json['name'].strip()
if not name:
return jsonify({'error': 'Name cannot be empty or contain only spaces'}), 400
new_user = {
'id': next_user_id,
'name': name
}
users.append(new_user)
next_user_id += 1
return jsonify(new_user), 201
@app.route('/api/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
"""
Returns a specific user by ID.
---
tags:
- Users
parameters:
- name: user_id
in: path
type: integer
required: true
description: ID of the user to retrieve
responses:
200:
description: User details
schema:
type: object
properties:
id:
type: integer
name:
type: string
404:
description: User not found
"""
user = next((u for u in users if u['id'] == user_id), None)
if user is None:
return jsonify({'error': 'User not found'}), 404
return jsonify(user)
@app.route('/api/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
"""
Updates an existing user (replaces the entire user).
---
tags:
- Users
parameters:
- name: user_id
in: path
type: integer
required: true
description: ID of the user to update
- name: body
in: body
required: true
schema:
type: object
required:
- name
properties:
name:
type: string
description: New name for the user
responses:
200:
description: User updated successfully
schema:
type: object
properties:
id:
type: integer
name:
type: string
400:
description: Missing 'name' in request body
404:
description: User not found
"""
user = next((u for u in users if u['id'] == user_id), None)
if user is None:
return jsonify({'error': 'User not found'}), 404
if not request.json or 'name' not in request.json:
return jsonify({'error': 'Missing name in request body'}), 400
# In a real app, you might validate the input further
user['name'] = request.json['name']
return jsonify(user)
@app.route('/api/users/<int:user_id>', methods=['PATCH'])
def patch_user(user_id):
"""
Partially updates an existing user.
---
tags:
- Users
parameters:
- name: user_id
in: path
type: integer
required: true
description: ID of the user to partially update
- name: body
in: body
required: true
schema:
type: object
properties:
name:
type: string
description: New name for the user (optional)
responses:
200:
description: User updated successfully
schema:
type: object
properties:
id:
type: integer
name:
type: string
400:
description: Missing request body
404:
description: User not found
"""
user = next((u for u in users if u['id'] == user_id), None)
if user is None:
return jsonify({'error': 'User not found'}), 404
if not request.json:
return jsonify({'error': 'Missing request body'}), 400
# Update fields present in the request
if 'name' in request.json:
user['name'] = request.json['name']
# Add other fields here if needed
return jsonify(user)
@app.route('/api/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
"""
Deletes a user.
---
tags:
- Users
parameters:
- name: user_id
in: path
type: integer
required: true
description: ID of the user to delete
responses:
200:
description: User deleted successfully
schema:
type: object
properties:
message:
type: string
404:
description: User not found
"""
global users
initial_length = len(users)
users = [u for u in users if u['id'] != user_id]
if len(users) == initial_length:
return jsonify({'error': 'User not found'}), 404
return jsonify({'message': 'User deleted successfully'}), 200
@app.route('/')
def index():
# Serve the main HTML page using render_template
return render_template('index.html')
@app.route('/api-testing')
def api_testing():
# Serve the API testing page
return render_template('api_testing.html')
@app.route('/api/silent-error', methods=['POST'])
def silent_error():
"""
Demonstrates a problematic API pattern where 200 OK is returned with an error in the body.
---
tags:
- Testing
parameters:
- name: body
in: body
required: true
schema:
type: object
required:
- username
properties:
username:
type: string
description: Username to validate
responses:
200:
description: Always returns 200 OK, even with errors
schema:
type: object
properties:
success:
type: boolean
error:
type: string
"""
if not request.json or 'username' not in request.json:
return jsonify({
'success': False,
'error': 'Username is required'
})
username = request.json['username']
if len(username) < 3:
return jsonify({
'success': False,
'error': 'Username must be at least 3 characters long'
})
return jsonify({
'success': True,
'message': 'User created successfully'
})
@app.route('/api/async-tasks', methods=['POST'])
def create_async_task():
"""
Creates a new async task.
---
tags:
- Async Tasks
parameters:
- name: body
in: body
required: true
schema:
type: object
required:
- description
properties:
description:
type: string
description: Description of the task
responses:
200:
description: Task created successfully
schema:
type: object
properties:
task_id:
type: string
description: ID of the created task
"""
if not request.json or 'description' not in request.json:
return jsonify({'error': 'Missing description in request body'}), 400
task_id = generate_unique_id()
task = {
'id': task_id,
'description': request.json['description'],
'status': 'pending',
'created_at': datetime.now().isoformat(),
'error': None
}
async_tasks[task_id] = task
# Simulate async task completion after 5 seconds
def complete_task():
import time
time.sleep(5)
async_tasks[task_id]['status'] = 'completed'
import threading
threading.Thread(target=complete_task).start()
return jsonify({'task_id': task_id}), 200
@app.route('/api/async-tasks/<task_id>', methods=['GET'])
def get_async_task(task_id):
"""
Gets the status of an async task.
---
tags:
- Async Tasks
parameters:
- name: task_id
in: path
type: string
required: true
description: ID of the task to retrieve
responses:
200:
description: Task status
schema:
type: object
properties:
id:
type: string
status:
type: string
created_at:
type: string
error:
type: string
404:
description: Task not found
"""
if task_id not in async_tasks:
return jsonify({'error': 'Task not found'}), 404
return jsonify(async_tasks[task_id])
@app.route('/api/incomplete-data', methods=['GET'])
def get_incomplete_data():
"""
Demonstrates an API that returns 200 OK but with missing or incomplete data.
---
tags:
- Testing
responses:
200:
description: Always returns 200 OK, but data may be missing or incomplete
schema:
type: object
properties:
status:
type: string
data:
type: object
properties:
user:
type: object
properties:
id:
type: integer
name:
type: string
email:
type: string
profile:
type: object
properties:
bio:
type: string
avatar:
type: string
"""
# Simulate different scenarios of incomplete data
import random
scenario = random.randint(1, 4)
if scenario == 1:
# Empty data object
return jsonify({
'status': 'success',
'data': {}
})
elif scenario == 2:
# Partial user data
return jsonify({
'status': 'success',
'data': {
'user': {
'id': 1,
'name': 'John Doe'
# Missing email and profile
}
}
})
elif scenario == 3:
# Nested missing data
return jsonify({
'status': 'success',
'data': {
'user': {
'id': 1,
'name': 'John Doe',
'email': 'john@example.com',
'profile': {} # Empty profile object
}
}
})
else:
# Complete data (rare case)
return jsonify({
'status': 'success',
'data': {
'user': {
'id': 1,
'name': 'John Doe',
'email': 'john@example.com',
'profile': {
'bio': 'Software Developer',
'avatar': 'https://example.com/avatar.jpg'
}
}
}
})
if __name__ == '__main__':
app.run(debug=True)