|
1 | 1 | """Async version of OFSC Statistics API module.""" |
2 | 2 |
|
3 | | -from typing import Optional |
| 3 | +from typing import Optional, Union |
4 | 4 | from urllib.parse import urljoin |
5 | 5 |
|
6 | 6 | import httpx |
|
17 | 17 | OFSCValidationError, |
18 | 18 | ) |
19 | 19 | from ..models import ( |
| 20 | + ActivityDurationStatRequestList, |
20 | 21 | ActivityDurationStatsList, |
| 22 | + ActivityTravelStatRequestList, |
21 | 23 | ActivityTravelStatsList, |
22 | 24 | AirlineDistanceBasedTravelList, |
| 25 | + AirlineDistanceBasedTravelRequestList, |
23 | 26 | OFSConfig, |
| 27 | + StatisticsPatchResponse, |
24 | 28 | ) |
25 | 29 |
|
26 | 30 |
|
@@ -279,3 +283,119 @@ async def get_airline_distance_based_travel( |
279 | 283 | raise OFSCNetworkError(f"Network error: {str(e)}") from e |
280 | 284 |
|
281 | 285 | # endregion |
| 286 | + |
| 287 | + # region Write Operations |
| 288 | + |
| 289 | + async def update_activity_duration_stats( |
| 290 | + self, |
| 291 | + data: Union[ActivityDurationStatRequestList, dict], |
| 292 | + ) -> StatisticsPatchResponse: |
| 293 | + """Update activity duration statistics overrides. |
| 294 | +
|
| 295 | + Args: |
| 296 | + data: List of activity duration stat overrides to apply. |
| 297 | +
|
| 298 | + Returns: |
| 299 | + StatisticsPatchResponse: Result with status and updatedRecords count. |
| 300 | +
|
| 301 | + Raises: |
| 302 | + OFSCAuthenticationError: If authentication fails (401) |
| 303 | + OFSCAuthorizationError: If authorization fails (403) |
| 304 | + OFSCValidationError: If request data is invalid (400/422) |
| 305 | + OFSCApiError: For other API errors |
| 306 | + OFSCNetworkError: For network/transport errors |
| 307 | + """ |
| 308 | + if isinstance(data, dict): |
| 309 | + data = ActivityDurationStatRequestList.model_validate(data) |
| 310 | + url = urljoin(self.baseUrl, "/rest/ofscStatistics/v1/activityDurationStats") |
| 311 | + try: |
| 312 | + response = await self._client.patch( |
| 313 | + url, |
| 314 | + headers=self.headers, |
| 315 | + json=data.model_dump(mode="python", exclude_none=True), |
| 316 | + ) |
| 317 | + response.raise_for_status() |
| 318 | + return StatisticsPatchResponse.model_validate(response.json()) |
| 319 | + except httpx.HTTPStatusError as e: |
| 320 | + self._handle_http_error(e, "Failed to update activity duration stats") |
| 321 | + raise |
| 322 | + except httpx.TransportError as e: |
| 323 | + raise OFSCNetworkError(f"Network error: {str(e)}") from e |
| 324 | + |
| 325 | + async def update_activity_travel_stats( |
| 326 | + self, |
| 327 | + data: Union[ActivityTravelStatRequestList, dict], |
| 328 | + ) -> StatisticsPatchResponse: |
| 329 | + """Update activity travel statistics overrides. |
| 330 | +
|
| 331 | + Args: |
| 332 | + data: List of activity travel stat overrides to apply. |
| 333 | +
|
| 334 | + Returns: |
| 335 | + StatisticsPatchResponse: Result with status and updatedRecords count. |
| 336 | +
|
| 337 | + Raises: |
| 338 | + OFSCAuthenticationError: If authentication fails (401) |
| 339 | + OFSCAuthorizationError: If authorization fails (403) |
| 340 | + OFSCConflictError: If "Detect activity travel keys automatically" is enabled (409) |
| 341 | + OFSCValidationError: If request data is invalid (400/422) |
| 342 | + OFSCApiError: For other API errors |
| 343 | + OFSCNetworkError: For network/transport errors |
| 344 | + """ |
| 345 | + if isinstance(data, dict): |
| 346 | + data = ActivityTravelStatRequestList.model_validate(data) |
| 347 | + url = urljoin(self.baseUrl, "/rest/ofscStatistics/v1/activityTravelStats") |
| 348 | + try: |
| 349 | + response = await self._client.patch( |
| 350 | + url, |
| 351 | + headers=self.headers, |
| 352 | + json=data.model_dump(mode="python", exclude_none=True), |
| 353 | + ) |
| 354 | + response.raise_for_status() |
| 355 | + return StatisticsPatchResponse.model_validate(response.json()) |
| 356 | + except httpx.HTTPStatusError as e: |
| 357 | + self._handle_http_error(e, "Failed to update activity travel stats") |
| 358 | + raise |
| 359 | + except httpx.TransportError as e: |
| 360 | + raise OFSCNetworkError(f"Network error: {str(e)}") from e |
| 361 | + |
| 362 | + async def update_airline_distance_based_travel( |
| 363 | + self, |
| 364 | + data: Union[AirlineDistanceBasedTravelRequestList, dict], |
| 365 | + ) -> StatisticsPatchResponse: |
| 366 | + """Update airline distance based travel overrides. |
| 367 | +
|
| 368 | + Args: |
| 369 | + data: List of airline distance travel overrides to apply. |
| 370 | +
|
| 371 | + Returns: |
| 372 | + StatisticsPatchResponse: Result with status and updatedRecords count. |
| 373 | +
|
| 374 | + Raises: |
| 375 | + OFSCAuthenticationError: If authentication fails (401) |
| 376 | + OFSCAuthorizationError: If authorization fails (403) |
| 377 | + OFSCConflictError: If "Detect activity travel keys automatically" is enabled (409) |
| 378 | + OFSCValidationError: If request data is invalid (400/422) |
| 379 | + OFSCApiError: For other API errors |
| 380 | + OFSCNetworkError: For network/transport errors |
| 381 | + """ |
| 382 | + if isinstance(data, dict): |
| 383 | + data = AirlineDistanceBasedTravelRequestList.model_validate(data) |
| 384 | + url = urljoin( |
| 385 | + self.baseUrl, "/rest/ofscStatistics/v1/airlineDistanceBasedTravel" |
| 386 | + ) |
| 387 | + try: |
| 388 | + response = await self._client.patch( |
| 389 | + url, |
| 390 | + headers=self.headers, |
| 391 | + json=data.model_dump(mode="python", exclude_none=True), |
| 392 | + ) |
| 393 | + response.raise_for_status() |
| 394 | + return StatisticsPatchResponse.model_validate(response.json()) |
| 395 | + except httpx.HTTPStatusError as e: |
| 396 | + self._handle_http_error(e, "Failed to update airline distance based travel") |
| 397 | + raise |
| 398 | + except httpx.TransportError as e: |
| 399 | + raise OFSCNetworkError(f"Network error: {str(e)}") from e |
| 400 | + |
| 401 | + # endregion |
0 commit comments