| 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 | |
| 6 | class ApiException implements Exception |
| 7 | final String message; |
| 8 | final int? statusCode; |
| 9 | |
| 10 | ApiException({required this |
| 11 | |
| 12 | @override |
| 13 | String toString() => |
| 14 | } |
| 15 | |
| 16 | class FlightDataService { |
| 17 | static const String |
| 18 | final Dio _dio; |
| 19 | final _flightApi = openapi.getFlightApi |
| 20 | final _flightsApi = openapi.getFlightsApi |
| 21 | |
| 22 | FlightDataService() : _dio = Dio |
| 23 | _dio.options.baseUrl = baseUrl; |
| 24 | // Add interceptors for logging |
| 25 | _dio.interceptors.add(LogInterceptor |
| 26 | requestBody: true, |
| 27 | responseBody: true, |
| 28 | error: true, |
| 29 | )); |
| 30 | } |
| 31 | |
| 32 | Future<FlightResponseEntity?> getFlightInfoWithNumber |
| 33 | String iata, |
| 34 | String icao, { |
| 35 | DateTime? date, |
| 36 | }) async { |
| 37 | date = date ?? DateTime.now |
| 38 | final res = await _flightApi. |
| 39 | iata: iata, |
| 40 | icao: icao, |
| 41 | date: "${date.year |
| 42 | ); |
| 43 | return res.data; |
| 44 | } |
| 45 | |
| 46 | Future<BuiltList<FlightEntity |
| 47 | String from, |
| 48 | String to, |
| 49 | String date, |
| 50 | ) async { |
| 51 | final res = await _flightsApi. |
| 52 | from: from, |
| 53 | to: to, |
| 54 | date: date, |
| 55 | ); |
| 56 | return res.data!.flights |
| 57 | // final List<dynamic> flightList = response.data; |
| 58 | // return flightList.map((flight) => Flight.fromJson(flight)).toList(); |
| 59 | // } else { |
| 60 | // throw ApiException( |
| 61 | // message: 'Failed to load flight data', |
| 62 | // statusCode: response.statusCode, |
| 63 | // ); |
| 64 | // } |
| 65 | // } on DioException catch (e) { |
| 66 | // throw ApiException( |
| 67 | // message: _handleDioError(e), |
| 68 | // statusCode: e.response?.statusCode, |
| 69 | // ); |
| 70 | // } catch (e) { |
| 71 | // throw ApiException(message: 'Unexpected error: $e'); |
| 72 | // } |
| 73 | } |
| 74 | } |