|
1 | 1 | """Asynchronous Python client for the OpenSky API.""" |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +from dataclasses import dataclass |
| 5 | + |
4 | 6 | from pydantic import BaseModel, Field |
5 | 7 |
|
6 | 8 | from .const import AircraftCategory, PositionSource |
| 9 | +from .exceptions import OpenSkyCoordinateError |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class StatesResponse(BaseModel): |
@@ -60,15 +63,34 @@ class StateVector(BaseModel): |
60 | 63 | category: AircraftCategory = Field(...) |
61 | 64 |
|
62 | 65 |
|
63 | | -class BoundingBox(BaseModel): |
| 66 | +@dataclass |
| 67 | +class BoundingBox: |
64 | 68 | """Bounding box for retrieving state vectors.""" |
65 | 69 |
|
66 | | - min_latitude: float = Field(...) |
67 | | - max_latitude: float = Field(...) |
68 | | - min_longitude: float = Field(...) |
69 | | - max_longitude: float = Field(...) |
| 70 | + min_latitude: float |
| 71 | + max_latitude: float |
| 72 | + min_longitude: float |
| 73 | + max_longitude: float |
| 74 | + |
| 75 | + def validate(self) -> None: |
| 76 | + """Validate if the latitude and longitude are correct.""" |
| 77 | + self._check_latitude(self.min_latitude) |
| 78 | + self._check_latitude(self.max_latitude) |
| 79 | + self._check_longitude(self.min_longitude) |
| 80 | + self._check_longitude(self.max_longitude) |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def _check_latitude(degrees: float) -> None: |
| 84 | + if degrees < -90 or degrees > 90: |
| 85 | + msg = f"Invalid latitude {degrees}! Must be in [-90, 90]." |
| 86 | + raise OpenSkyCoordinateError(msg) |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _check_longitude(degrees: float) -> None: |
| 90 | + if degrees < -180 or degrees > 180: |
| 91 | + msg = f"Invalid longitude {degrees}! Must be in [-180, 180]." |
| 92 | + raise OpenSkyCoordinateError(msg) |
70 | 93 |
|
71 | 94 |
|
72 | 95 | StatesResponse.update_forward_refs() |
73 | 96 | StateVector.update_forward_refs() |
74 | | -BoundingBox.update_forward_refs() |
|
0 commit comments