-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (44 loc) · 1.45 KB
/
main.py
File metadata and controls
61 lines (44 loc) · 1.45 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
import os
import json
from flask import Flask, jsonify
from flagsmith import Flagsmith
SHOP_FLAG_NAME = "shop"
app = Flask(__name__)
flagsmith = Flagsmith(
environment_key=os.environ["FLAGSMITH_KEY"],
enable_local_evaluation=True,
)
games = [
{"id": 1, "title": "Fallout: New Vegas", "price_usd": "4000"},
{"id": 2, "title": "Tomb Raider", "price_usd": "3500"},
{"id": 3, "title": "Stray", "price_usd": "1000"},
]
inventory = {
1: 10,
2: 13,
3: 4,
}
@app.route("/games", methods=["GET"])
def list_games():
flags = flagsmith.get_environment_flags()
try:
metadata = json.loads(flags.get_feature_value(SHOP_FLAG_NAME))
return jsonify(
sorted(
games,
key=lambda i: i[metadata["default_sort_field"]],
reverse=metadata["default_sort_direction"] == "desc",
)
)
except (TypeError, KeyError) as e:
pass
return jsonify(games)
@app.route("/games/<int:game_id>/buy", methods=["POST"])
def buy_game(game_id: int):
flags = flagsmith.get_environment_flags()
if not flags.is_feature_enabled(SHOP_FLAG_NAME):
return jsonify({"result": "error", "message": "Shop not yet available!"})
if game_id not in inventory or inventory[game_id] == 0:
return jsonify({"result": "error", "message": "Game not in stock!"})
inventory[game_id] -= 1
return {"result": "success", "message": "Game bought successfully."}