Bonjour, j'aimerais utiliser docker avec fasapi et redis. Pour ce faire, j'ai déjà, j'ai déjà coder mon API en Python et installé docker ainsi que docker-compose. Mais après, je suis perdu !
Voici mon code :
from fastapi import FastAPI
from redis import StrictRedis
app = FastAPI()
#Products are identified by name, so there cannot be two products with the same
#name !
redis_data = StrictRedis(host="localhost", port=6379, db=0)
products = {}
for key in redis_data.scan_iter():
products[key.decode("utf-8")] = eval(redis_data.get(key))
@app.get("/")
async def get_product_information(product_name: str=""):
"""
product is the name of the product about which we want information (quantity
and price).
Returns a dictionary indicating the price and the quantity disponible of the
product.
If product is an empty string, then returns the dictionary containing
information for every referenced product.
If the product in not referenced, then returns a message error.
"""
if product_name == "":
return products
if product_name in products.keys():
return products[product_name]
return "Error : None of the referenced products has the specified name"
@app.post("/")
async def add_product(product: dict):
"""
Adds a product to the database. The product is representend by a dictionary
with tree items :
one indicating the name (key : "name", value : the product's name) ;
one indicating the price (key : "price", value the price) ;
one indicating the quantity (key : "quantity", value : the quantity).
"""
product = product.copy()
for feature in ("name", "quantity", "price"):
if not feature in product.keys():
return f"Error : The product should have a {feature}."
product_name = product.pop("name")
products[product_name] = product
redis_data.set(product_name.encode("utf-8"), str(product).encode("utf-8"))
return "The product has been added."
@app.delete("/")
async def remove_product(product_name: str):
"""
Removes the product product from the database.
"""
if product_name in products.keys():
del products[product_name]
redis_data.delete(product_name)
return "The product has been removed."
return "Error : None of the referenced products has the specified name."
Pouvez-vous m'aider, s'il vous plaît ?
Merci par avance,
Bonne soirée !
Utiliser docker avec fastapi et redis
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.