Spaces:
Running
Running
Download app.py from sdacelo/api-ecommerce: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/sdacelo/api-ecommerce/resolve/main/app.py
- Command line
-
hf download hf://spaces/sdacelo/api-ecommerce/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sdacelo/api-ecommerce/resolve/main/app.py
5.01 kB
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import List | |
| from ecommerce_fake_data import EcommerceFakeData | |
| app = FastAPI( | |
| title="E-Commerce API", | |
| description="API for managing customers, products, sellers, orders, and payments in an e-commerce platform.", | |
| version="1.0.0" | |
| ) | |
| efd = EcommerceFakeData() | |
| efd.generate(n_cust=5000, n_prod=300, n_sell=300, n_ord=10000) | |
| class Customer(BaseModel): | |
| customer_id: str | |
| name: str | |
| email: str | |
| lat: float | |
| lon: float | |
| signup_date: str | |
| class Product(BaseModel): | |
| product_id: str | |
| category: str | |
| price: float | |
| class Seller(BaseModel): | |
| seller_id: str | |
| lat: float | |
| lon: float | |
| class Order(BaseModel): | |
| order_id: str | |
| customer_id: str | |
| product_id: str | |
| seller_id: str | |
| order_date: str | |
| quantity: int | |
| total_amount: float | |
| class Payment(BaseModel): | |
| payment_id: str | |
| order_id: str | |
| payment_method: str | |
| payment_status: str | |
| payment_date: str | |
| # GET | |
| def home(): | |
| return {"Hello": "Welcome to the E-Commerce API!"} | |
| def get_customers(): | |
| return efd.customers | |
| def get_customer_by_id(customer_id: str): | |
| customer = next((c for c in efd.customers if c["customer_id"] == customer_id), None) | |
| if customer is None: | |
| raise HTTPException(status_code=404, detail="Customer not found") | |
| return customer | |
| def get_products(): | |
| return efd.products | |
| def get_sellers(): | |
| return efd.sellers | |
| def get_orders(): | |
| return efd.orders | |
| def get_payments(): | |
| return efd.payments | |
| # POST | |
| def create_customer(customer: Customer): | |
| efd.customers.append(customer.model_dump()) | |
| return {"message": "Customer successfully added !"} | |
| def create_product(product: Product): | |
| efd.products.append(product.model_dump()) | |
| return {"message": "Product successfully added !"} | |
| # PUT | |
| def update_customer(customer_id: str, updated_customer: Customer): | |
| for index, customer in enumerate(efd.customers): | |
| if customer["customer_id"] == customer_id: | |
| efd.customers[index] = updated_customer.model_dump() | |
| return {"message": "Customer successfully updated !"} | |
| raise HTTPException(status_code=404, detail="Customer not found") | |
| def update_product(product_id: str, updated_product: Product): | |
| for index, product in enumerate(efd.products): | |
| if product["product_id"] == product_id: | |
| efd.products[index] = updated_product.model_dump() | |
| return {"message": "Product successfully updated !"} | |
| raise HTTPException(status_code=404, detail="Product not found") | |
| #DELETE | |
| # @app.delete("/customers/{customer_id}", summary="Delete a customer", description="Allows you to delete a customer by their ID.", tags=["Customers"]) | |
| # def delete_customer(customer_id: str): | |
| # initial_length = len(efd.customers) | |
| # efd.customers = [c for c in efd.customers if c["customer_id"] != customer_id] | |
| # if len(efd.customers) < initial_length: | |
| # return {"message": "Customer successfully deleted !"} | |
| # raise HTTPException(status_code=404, detail="Customer not found") | |
| # @app.delete("/products/{product_id}", summary="Delete a product", description="Allows you to delete a product by its ID.", tags=["Products"]) | |
| # def delete_product(product_id: str): | |
| # efd.products = [p for p in efd.products if p["product_id"] != product_id] | |
| # return {"message": "Product successfully deleted !"} | |
| #test |