File size: 1,235 Bytes
cce1e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()