-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-flask.py
More file actions
26 lines (20 loc) · 714 Bytes
/
04-flask.py
File metadata and controls
26 lines (20 loc) · 714 Bytes
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
from flask import Flask, jsonify
from products import products
app = Flask(__name__)
@app.route('/ping')
def ping():
return jsonify({"message":"pong!"})
#Endpoint que retorna todos los productos
@app.route('/products')
def getProducts():
return jsonify({"products": products})
#Endpoint que retorna el producto indicado por id
@app.route('/product/<int:id_product>')
def getProduct(id_product):
productFound = [product for product in products if product['id'] == id_product]
if(len(productFound) > 0):
return jsonify({"product": productFound[0]})
else:
return jsonify({"message": "Product not found"})
if __name__== '__main__':
app.run(debug= True, port= 4000)