File size: 3,369 Bytes
7616a39 c1b303d | 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # app/models.py
"""
Pydantic models for request/response validation.
"""
from pydantic import BaseModel, HttpUrl, Field
from typing import Optional, Dict, Any, List
class PageSpeedRequest(BaseModel):
"""Request model for fetching PageSpeed data."""
url: HttpUrl = Field(
...,
description="The URL to analyze for PageSpeed insights",
example="https://www.example.com"
)
class Config:
json_schema_extra = {
"example": {
"url": "https://www.ocoya.com/"
}
}
class PageSpeedDataResponse(BaseModel):
"""Response model that returns only the raw PageSpeed data."""
success: bool = Field(
...,
description="Whether the PageSpeed fetch was successful"
)
url: str = Field(
...,
description="The analyzed URL"
)
pagespeed_data: Optional[Dict[Any, Any]] = Field(
None,
description="Raw PageSpeed Insights data"
)
error: Optional[str] = Field(
None,
description="Error message if fetching failed"
)
class ReportRequest(BaseModel):
"""
Request model for generating a Gemini report.
Expects the entire raw PageSpeed JSON payload in the body.
"""
pagespeed_data: Dict[Any, Any] = Field(
...,
description="Raw PageSpeed Insights data (JSON) previously fetched",
)
class Config:
schema_extra = {
"example": {
"pagespeed_data": {
# (Truncated example; in practice this would be
# the full runPagespeed v5 JSON structure)
"lighthouseResult": {
"audits": {
"first-contentful-paint": {"numericValue": 1234},
"largest-contentful-paint": {"numericValue": 2345}
}
},
"loadingExperience": {
"metrics": {
"FIRST_CONTENTFUL_PAINT_MS": {"percentile": 1200, "category": "FAST"}
}
}
# …etc.
}
}
}
class ReportResponse(BaseModel):
"""Response model that returns only the Gemini-generated report."""
success: bool = Field(
...,
description="Whether report generation was successful"
)
report: Optional[str] = Field(
None,
description="Gemini-generated performance optimization report"
)
error: Optional[str] = Field(
None,
description="Error message if report generation failed"
)
class HealthResponse(BaseModel):
"""Health check response model."""
status: str = Field(
...,
description="Health status of the API"
)
version: str = Field(
...,
description="API version"
)
uptime: str = Field(
...,
description="API uptime"
)
class PriorityRequest(BaseModel):
report: str
class PriorityResponse(BaseModel):
success: bool
priorities: Optional[Dict[str, List[str]]] = None
error: Optional[str] = None
class AnalyzeRequest(BaseModel):
url: HttpUrl
class AnalyzeResponse(BaseModel):
success: bool
url: HttpUrl
report: str | None
priorities: dict | None
error: str | None |