-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (53 loc) · 1.93 KB
/
main.py
File metadata and controls
73 lines (53 loc) · 1.93 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from typing import List, Optional
from fastapi import FastAPI, Depends, HTTPException, APIRouter, Query
from sqlalchemy.orm import Session
from starlette.middleware.cors import CORSMiddleware
from src.gazette_scraper import crud, schemas
from src.gazette_scraper.database import get_db
# -- App Initialization --
app = FastAPI(
title="Gazette Scraper API",
description="API for storing and retrieving gazette URLs.",
version="1.0.0",
root_path="/api"
)
# -- CORS Middleware --
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["POST", "GET"],
allow_headers=["*"],
)
# -- API Router --
router = APIRouter()
# -- Route Declarations --
@router.post("/gazettes/", response_model=schemas.GazetteResponse, tags=["Gazettes"])
def create_gazette(gazette: schemas.GazetteCreate, db: Session = Depends(get_db)):
"""
Create a new gazette entry.
"""
return crud.create_gazette(db=db, gazette=gazette)
@router.get("/gazettes/", response_model=List[schemas.GazetteResponse], tags=["Gazettes"])
def read_gazettes(
month: Optional[int] = Query(None, ge=1, le=12),
year: Optional[int] = Query(None),
skip: int = 0,
limit: int = 100,
db: Session = Depends(get_db),
):
"""
Retrieve gazette entries.
- If **month** and **year** are provided, it filters entries by publication date.
- Otherwise, it returns a paginated list of all entries.
- **skip**: Number of entries to skip (for pagination).
- **limit**: Maximum number of entries to return (for pagination).
"""
if month and year:
return crud.get_gazettes_by_month_year(db, month=month, year=year)
if (month and not year) or (not month and year):
raise HTTPException(
status_code=400, detail="Both month and year must be provided for filtering."
)
return crud.get_gazettes(db, skip=skip, limit=limit)
app.include_router(router)