| 1 | import 'package:built_collection/built_collection.dart'; |
| 2 | import 'package:openapi/openapi.dart'; |
| 3 | import 'package:aero/constants/main.dart'; |
| 4 | import 'package:dio/dio.dart'; |
| 5 | import 'package:aero/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 | String? faFlightId, |
| 37 | }) async { |
| 38 | date = date ?? DateTime.now |
| 39 | final timezone = date.timeZoneName; |
| 40 | final res = await _flightApi. |
| 41 | iata: iata, |
| 42 | icao: icao, |
| 43 | date: |
| 44 | "${date.year} |
| 45 | timezone: timezone, |
| 46 | forceUpdate: forceUpdate ?? false, |
| 47 | faFlightId: faFlightId, |
| 48 | ); |
| 49 | return res.data; |
| 50 | } |
| 51 | |
| 52 | Future<FlightSearchResponseEntity?> searchFlights |
| 53 | String iata, |
| 54 | String icao, { |
| 55 | DateTime? date, |
| 56 | }) async { |
| 57 | try { |
| 58 | final searchDate = date ?? DateTime |
| 59 | final timezone = searchDate.timeZoneName; |
| 60 | final res = await _flightApi. |
| 61 | iata: iata, |
| 62 | icao: icao, |
| 63 | date: "${searchDate.year |
| 64 | timezone: timezone, |
| 65 | ); |
| 66 | return res.data; |
| 67 | } catch (e) { |
| 68 | if (e is DioException) { |
| 69 | throw ApiException(message: getErrorMessage |
| 70 | } |
| 71 | throw ApiException( |
| 72 | message: 'Failed to search flights: $e', |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | Future<BuiltList<FlightEntity |
| 78 | String from, |
| 79 | String to, |
| 80 | String date, |
| 81 | ) async { |
| 82 | final res = await _flightsApi. |
| 83 | from: from, |
| 84 | to: to, |
| 85 | date: date, |
| 86 | ); |
| 87 | return res.data!.flights |
| 88 | } |
| 89 | |
| 90 | Future<FlightsControllerGetFlightsInBoundsV1200Response> getFlightsInBounds |
| 91 | required double minLat, |
| 92 | required double maxLat, |
| 93 | required double minLng, |
| 94 | required double maxLng, |
| 95 | }) async { |
| 96 | try { |
| 97 | final response = await _flightsApi. |
| 98 | minLat: minLat, |
| 99 | maxLat: maxLat, |
| 100 | minLng: minLng, |
| 101 | maxLng: maxLng, |
| 102 | ); |
| 103 | |
| 104 | final data = response.data! |
| 105 | return data; |
| 106 | } catch (e) { |
| 107 | if (e is DioException) { |
| 108 | throw ApiException(message: getErrorMessage |
| 109 | } |
| 110 | throw ApiException( |
| 111 | message: 'Failed to fetch flights in bounds: $e', |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | Future<FlightResponseEntity?> getFlightInfo |
| 117 | try { |
| 118 | final response = await _flightApi. |
| 119 | iata: '', // Empty since we're using ICAO24 |
| 120 | icao: icao24, |
| 121 | date: |
| 122 | "${DateTime.now(). |
| 123 | timezone: DateTime.now() |
| 124 | ); |
| 125 | return response.data; |
| 126 | } catch (e) { |
| 127 | if (e is DioException) { |
| 128 | throw ApiException(message: getErrorMessage |
| 129 | } |
| 130 | throw ApiException( |
| 131 | message: 'Failed to fetch flight information: $e', |
| 132 | ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | Future<FlightTrackResponseEntity?> getFlightTrack |
| 137 | String iata, |
| 138 | String icao, { |
| 139 | DateTime? date, |
| 140 | }) async { |
| 141 | try { |
| 142 | date = date ?? DateTime.now |
| 143 | final response = await _flightApi. |
| 144 | iata: iata, |
| 145 | icao: icao, |
| 146 | date: |
| 147 | "${date.year} |
| 148 | timezone: DateTime.now() |
| 149 | ); |
| 150 | return response.data; |
| 151 | } catch (e, stack) { |
| 152 | print(e); |
| 153 | print(stack); |
| 154 | if (e is DioException) { |
| 155 | throw ApiException(message: getErrorMessage |
| 156 | } |
| 157 | throw ApiException( |
| 158 | message: 'Failed to fetch flight track: $e', |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | Future<BuiltList<FlightResponseEntity |
| 164 | try { |
| 165 | final response = await _flightsApi. |
| 166 | return response.data!.flights |
| 167 | } catch (e,stack) { |
| 168 | print(e); |
| 169 | print(stack); |
| 170 | if (e is DioException) { |
| 171 | throw ApiException(message: getErrorMessage |
| 172 | } |
| 173 | throw ApiException( |
| 174 | message: 'Failed to fetch tracked flights: $e', |
| 175 | ); |
| 176 | } |
| 177 | } |
| 178 | } |