-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (58 loc) · 2.5 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
69
70
71
72
73
import json
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/')
def index():
return '''
<!DOCTYPE html>
<head>
<title>webbrowser11 - recipe api</title>
</head>
<body>
<h1>endpoint</h1>
<p>endpoint for the thanks-giving-recipe-api by webbrowser11</p>
</body>
'''
def recipe_is_valid(recipe):
return (
"name" in recipe and isinstance(recipe["name"], str) and
"ingredients" in recipe and isinstance(recipe["ingredients"], list) and
"steps" in recipe and isinstance(recipe["steps"], list)
)
thanksgivingrecipes = {
"best-turkey-gravy": {
"ingredients": ["pan drippings", "chicken or turkey stock", "unsalted butter", "flour", "thyme", "salt and pepper"],
"steps": [
'''strain pan drippings trough a fine mesh sieve; discard soils and reserve 2 1/2 cups pan drippings; set aside.
melt butter in a medium saucepan over medium heat. whisk in flour and thyme until lightly browned, about 1 minute.
gradually whisk in reserved pan drippings. bring to a boil; reduce heat and simmer, whisking constantly until thickened about 5-10 minutes stir in parsley season with salt and pepper to taste
serve warm.'''
]
}
}
@app.route('/thanksgivingrecipes', methods=['GET'])
def get_thanksgivingrecipies():
return jsonify(thanksgivingrecipes)
@app.route("/thanksgivingrecipes", methods=['POST'])
def create_recipie():
recipe = json.loads(request.data)
if not recipe_is_valid(recipe):
return jsonify({'error': 'invalid recipe properties.'}), 400
recipe_name = recipe["name"].lower()
if recipe_name in thanksgivingrecipes:
return jsonify({'error': f'Recipe "{recipe_name}" already exists.'}), 409
thanksgivingrecipes[recipe_name] = {
"ingredients": recipe["ingredients"],
"steps": recipe["steps"]
}
return jsonify({'message': f'recipe "{recipe_name}" created sucsesfully!'}), 201
@app.route('/thanksgivingrecipes/<string:recipe_name>', methods=['DELETE'])
def delete_recipe(recipe_name: str):
global thanksgivingrecipes
recipe_name = recipe_name.lower()
if recipe_name not in thanksgivingrecipes:
return jsonify({'error': 'recipe does not exist'}), 404
deleted_recipe = thanksgivingrecipes.pop(recipe_name)
return jsonify(deleted_recipe), 200
if __name__ == '__main__':
app.run(port=5000)