-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (57 loc) · 2.33 KB
/
app.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
import json
from flask import Flask, request
from redis import Redis
app = Flask(__name__)
app.config["DEBUG"] = True
redis_cache = Redis(host="cache_redis", port=6379)
@app.route("/", methods=["GET"])
def home():
return """<h1>Test technique - Sens Critique</h1>
<p>A flask api.</p>
<p>2 possible requets :</p>
<p>- GET /product/<movie_id></p>
<p>- POST /product/<movie_id></p>
<p> with the following formdata : {"title": "", "grade": "", "type": ""}</p>"""
@app.route("/product/<movie_id>", methods=["GET"])
def api_get(movie_id):
if "movies" not in redis_cache:
return """<h1>Test technique - Sens Critique</h1>
<p>Aucun film n'a été noté, base de donnée vide</p>"""
movies_data = json.loads(redis_cache.get("movies"))
if movie_id in movies_data.keys():
return f"""<h1>Test technique - Sens Critique</h1>
<p>Identifiant du film - {movie_id}:</p>
<p>Titre - {movies_data[movie_id].get("title")}/10</p>
<p>Note - {movies_data[movie_id].get("grade")}/10</p>
<p>Genre - {movies_data[movie_id].get("type")}/10</p>"""
else:
return """<h1>Test technique - Sens Critique</h1>
<p>Ce film n'est pas encore noté</p>"""
@app.route("/product/<movie_id>", methods=["POST"])
def api_post(movie_id):
if "grade" not in request.form.keys():
return json.dumps(
{
"topic": "Test technique Sens critique",
"ERROR": "Please provide a 'grade' in your request",
}
)
else:
data = {}
if "movies" in redis_cache:
data = json.loads(redis_cache.get("movies"))
data.update({movie_id: {
"title": request.form.get("title"),
"grade": request.form.get("grade"),
"type": request.form.get("type"),
}
})
redis_cache.set("movies", json.dumps(data))
return json.dumps(
{
"topic": "Test technique Sens critique",
"new_grade": data,
}
)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)