| 1 | import 'package:built_collection/built_collection.dart'; |
| 2 | import 'package:openapi/openapi.dart'; |
| 3 | import 'package:plane_pal/constants/main.dart'; |
| 4 | import 'package:dio/dio.dart'; |
| 5 | import 'package:plane_pal/utils/error.dart'; |
| 6 | |
| 7 | class ApiException implements Exception |
| 8 | final String message; |
| 9 | final int? statusCode; |
| 10 | |
| 11 | ApiException({required this |
| 12 | |
| 13 | @override |
| 14 | String toString() => |
| 15 | } |
| 16 | |
| 17 | class FlightDataService { |
| 18 | final Dio _dio; |
| 19 | final _flightApi = openapi.getFlightApi |
| 20 | final _flightsApi = openapi.getFlightsApi |
| 21 | |
| 22 | FlightDataService() : _dio = Dio |
| 23 | // Add interceptors for logging |
| 24 | _dio.interceptors.add(LogInterceptor |
| 25 | requestBody: true, |
| 26 | responseBody: true, |
| 27 | error: true, |
| 28 | )); |
| 29 | } |
| 30 | |
| 31 | Future<FlightResponseEntity?> getFlightInfoWithNumber |
| 32 | String iata, |
| 33 | String icao, { |
| 34 | DateTime? date, |
| 35 | bool? forceUpdate, |
| 36 | }) async { |
| 37 | date = date ?? DateTime.now |
| 38 | final timezone = date.timeZoneName; |
| 39 | final res = await _flightApi. |
| 40 | iata: iata, |
| 41 | icao: icao, |
| 42 | date: |
| 43 | "${date.year} |
| 44 | timezone: timezone, |
| 45 | forceUpdate: forceUpdate ?? false, |
| 46 | ); |
| 47 | return res.data; |
| 48 | } |
| 49 | |
| 50 | Future<BuiltList<FlightEntity |
| 51 | String from, |
| 52 | String to, |
| 53 | String date, |
| 54 | ) async { |
| 55 | final res = await _flightsApi. |
| 56 | from: from, |
| 57 | to: to, |
| 58 | date: date, |
| 59 | ); |
| 60 | return res.data!.flights |
| 61 | } |
| 62 | |
| 63 | Future<FlightsControllerGetFlightsInBoundsV1200Response> getFlightsInBounds |
| 64 | required double minLat, |
| 65 | required double maxLat, |
| 66 | required double minLng, |
| 67 | required double maxLng, |
| 68 | }) async { |
| 69 | try { |
| 70 | final response = await _flightsApi. |
| 71 | minLat: minLat, |
| 72 | maxLat: maxLat, |
| 73 | minLng: minLng, |
| 74 | maxLng: maxLng, |
| 75 | ); |
| 76 | |
| 77 | final data = response.data! |
| 78 | return data; |
| 79 | } catch (e) { |
| 80 | if (e is DioException) { |
| 81 | throw ApiException(message: getErrorMessage |
| 82 | } |
| 83 | throw ApiException( |
| 84 | message: 'Failed to fetch flights in bounds: $e', |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | Future<FlightResponseEntity?> getFlightInfo |
| 90 | try { |
| 91 | final response = await _flightApi. |
| 92 | iata: '', // Empty since we're using ICAO24 |
| 93 | icao: icao24, |
| 94 | date: |
| 95 | "${DateTime.now(). |
| 96 | timezone: DateTime.now() |
| 97 | ); |
| 98 | return response.data; |
| 99 | } catch (e) { |
| 100 | if (e is DioException) { |
| 101 | throw ApiException(message: getErrorMessage |
| 102 | } |
| 103 | throw ApiException( |
| 104 | message: 'Failed to fetch flight information: $e', |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Future<FlightTrackResponseEntity?> getFlightTrack |
| 110 | String iata, |
| 111 | String icao, { |
| 112 | DateTime? date, |
| 113 | }) async { |
| 114 | try { |
| 115 | date = date ?? DateTime.now |
| 116 | final response = await _flightApi. |
| 117 | iata: iata, |
| 118 | icao: icao, |
| 119 | date: |
| 120 | "${date.year} |
| 121 | timezone: DateTime.now() |
| 122 | ); |
| 123 | return response.data; |
| 124 | } catch (e, stack) { |
| 125 | print(e); |
| 126 | print(stack); |
| 127 | if (e is DioException) { |
| 128 | throw ApiException(message: getErrorMessage |
| 129 | } |
| 130 | throw ApiException( |
| 131 | message: 'Failed to fetch flight track: $e', |
| 132 | ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | Future<BuiltList<FlightResponseEntity |
| 137 | try { |
| 138 | final response = await _flightsApi. |
| 139 | return response.data!.flights |
| 140 | } catch (e) { |
| 141 | if (e is DioException) { |
| 142 | throw ApiException(message: getErrorMessage |
| 143 | } |
| 144 | throw ApiException( |
| 145 | message: 'Failed to fetch tracked flights: $e', |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | } |