forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeolocation.py
More file actions
58 lines (47 loc) · 2.37 KB
/
Copy pathgeolocation.py
File metadata and controls
58 lines (47 loc) · 2.37 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
import json
from datetime import datetime
from typing import Literal, Optional
from pydantic import BaseModel, Field
class Geolocation(BaseModel):
google_place_id: Optional[str] = None
latitude: float = Field(ge=-90, le=90)
longitude: float = Field(ge=-180, le=180)
address: Optional[str] = None
location_type: Optional[str] = None
captured_at: Optional[datetime] = None
capture_source: Optional[Literal['current_position', 'last_known_position', 'manual', 'integration']] = None
accuracy: Optional[float] = Field(default=None, ge=0, allow_inf_nan=False)
altitude: Optional[float] = Field(default=None, allow_inf_nan=False)
class GeolocationInput(BaseModel):
"""Released wire contract for coordinates accepted at legacy API boundaries.
Bounds are enforced before any value is cached, geocoded, or persisted. Keeping
this transport shape broad preserves clients released before the bound contract
existed, while ``Geolocation`` remains the only usable server-side value.
"""
google_place_id: Optional[str] = None
latitude: float
longitude: float
address: Optional[str] = None
location_type: Optional[str] = None
captured_at: Optional[datetime] = None
capture_source: Optional[Literal['current_position', 'last_known_position', 'manual', 'integration']] = None
accuracy: Optional[float] = Field(default=None, ge=0, allow_inf_nan=False)
altitude: Optional[float] = Field(default=None, allow_inf_nan=False)
def validated_geolocation_or_none(geolocation: Optional[GeolocationInput]) -> Optional[Geolocation]:
if geolocation is None:
return None
try:
return Geolocation.model_validate(geolocation.model_dump())
except (RecursionError, ValueError):
return None
def geolocation_from_private_header(value: Optional[str]) -> Optional[Geolocation]:
"""Parse a bounded, authenticated transport header without ever logging coordinates."""
# Direct endpoint calls in tests (and internal callers) can leave FastAPI's
# Header sentinel in place. Treat every non-string as absent.
if not isinstance(value, str) or not value or len(value) > 4096:
return None
try:
payload = json.loads(value)
return validated_geolocation_or_none(GeolocationInput.model_validate(payload))
except (RecursionError, json.JSONDecodeError, TypeError, ValueError):
return None