-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkers.py
50 lines (38 loc) · 1.23 KB
/
workers.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
from flask import Flask, jsonify, request
from http import HTTPStatus
"""Simple API in Flask to retrieve users with profession"""
app = Flask(__name__)
def get_users():
usersdb = [
{"id": 1, "name": "Charles", "role": "Software Engineer"},
{"id": 2, "name": "Elena", "role": "Saberedowo Tailor"},
{"id": 3, "name": "Deniyi", "role": "Oniroyin"},
{"id": 4, "name": "Brown", "role": "Dokita"},
{"id": 5, "name": "Debbie", "role": "Agbejoro"},
]
return usersdb
@app.route("/")
def index():
return jsonify([]), HTTPStatus.OK
@app.route("/users/")
def get_users_list():
"""Get all users if no name is specified and
specified user is name is provided"""
users_list = get_users()
name = request.args.get("name")
if not name:
return jsonify(users_list), HTTPStatus.OK
if name:
users = next(
(
user
for user in users_list
if str(user["name"]).lower() == name.lower()
),
None,
)
if not users:
return jsonify([]), HTTPStatus.OK
return jsonify(users), HTTPStatus.OK
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001, debug=True)