Spaces:
Sleeping
Sleeping
| from sqlalchemy.orm import Session | |
| from typing import List, Optional | |
| from app.models.item import Item | |
| def get_item(db: Session, item_id: int) -> Optional[Item]: | |
| return db.query(Item).filter(Item.id == item_id).first() | |
| def get_items(db: Session, skip: int = 0, limit: int = 100) -> List[Item]: | |
| item_list = db.query(Item).offset(skip).limit(limit).all() | |
| return item_list | |
| from typing import List | |
| from fastapi import Header, HTTPException | |
| from sqlalchemy.orm import Session | |
| API_KEY = "1234" | |
| def get_items( | |
| db: Session, | |
| skip: int = 0, | |
| limit: int = 100, | |
| api_key: str = Header(..., alias="api_key") | |
| ) -> List[Item]: | |
| """ | |
| Get multiple items with pagination. | |
| Args: | |
| db (Session): Database session | |
| skip (int): Number of records to skip (for pagination) | |
| limit (int): Maximum number of records to return | |
| api_key (str): Required API key from request header | |
| Returns: | |
| List[Item]: List of found items | |
| """ | |
| # Validate API key | |
| if api_key != API_KEY: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid API key" | |
| ) | |
| # Query all items with pagination | |
| return db.query(Item).offset(skip).limit(limit).all() |