| 1 | import 'package:plane_pal/client/pocketbase.dart'; |
| 2 | import 'package:plane_pal/models/flight/flight.dart'; |
| 3 | import 'package:dio/dio.dart'; |
| 4 | import 'package:plane_pal/models/flight_info/flight_info.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 | |
| 20 | FlightDataService() : _dio = Dio |
| 21 | _dio.options.baseUrl = baseUrl; |
| 22 | // Add interceptors for logging |
| 23 | _dio.interceptors.add(LogInterceptor |
| 24 | requestBody: true, |
| 25 | responseBody: true, |
| 26 | error: true, |
| 27 | )); |
| 28 | } |
| 29 | |
| 30 | Future<FlightInfo?> getFlightInfoWithNumber |
| 31 | String iata, |
| 32 | String icao, { |
| 33 | DateTime? date, |
| 34 | }) async { |
| 35 | date = date ?? DateTime.now |
| 36 | try { |
| 37 | final res = await pb. |
| 38 | "/flight", |
| 39 | query: { |
| 40 | "iata": iata, |
| 41 | "icao": icao, |
| 42 | "date": "${date. |
| 43 | }, |
| 44 | ); |
| 45 | return FlightInfo.fromJson |
| 46 | } catch (e, stack) { |
| 47 | print(e); |
| 48 | print(stack); |
| 49 | return null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | Future<List<Flight |
| 54 | String from, |
| 55 | String to, |
| 56 | String date, |
| 57 | ) async { |
| 58 | try { |
| 59 | final res = await pb. |
| 60 | "/flights", |
| 61 | query: {"from": from, "to": |
| 62 | ); |
| 63 | if (res['flights'] != null) { |
| 64 | return (res['flights'] as List |
| 65 | } |
| 66 | return []; |
| 67 | } catch (e, stack) { |
| 68 | print(e); |
| 69 | print(stack); |
| 70 | } |
| 71 | return []; |
| 72 | // try { |
| 73 | // final response = await _dio.get( |
| 74 | // '/flights/$type', |
| 75 | // queryParameters: { |
| 76 | // 'airport': icao, |
| 77 | // 'begin': beginTime, |
| 78 | // 'end': endTime, |
| 79 | // }, |
| 80 | // options: Options( |
| 81 | // headers: { |
| 82 | // 'Accept': 'application/json', |
| 83 | // }, |
| 84 | // ), |
| 85 | // ); |
| 86 | |
| 87 | // if (response.statusCode == 200) { |
| 88 | // final List<dynamic> flightList = response.data; |
| 89 | // return flightList.map((flight) => Flight.fromJson(flight)).toList(); |
| 90 | // } else { |
| 91 | // throw ApiException( |
| 92 | // message: 'Failed to load flight data', |
| 93 | // statusCode: response.statusCode, |
| 94 | // ); |
| 95 | // } |
| 96 | // } on DioException catch (e) { |
| 97 | // throw ApiException( |
| 98 | // message: _handleDioError(e), |
| 99 | // statusCode: e.response?.statusCode, |
| 100 | // ); |
| 101 | // } catch (e) { |
| 102 | // throw ApiException(message: 'Unexpected error: $e'); |
| 103 | // } |
| 104 | } |
| 105 | |
| 106 | String _handleDioError(DioException |
| 107 | switch (error.type) { |
| 108 | case DioExceptionType.connectionTimeout: |
| 109 | return 'Connection timeout. Please check your internet connection.'; |
| 110 | case DioExceptionType.receiveTimeout: |
| 111 | return 'Receive timeout. Please try again.'; |
| 112 | case DioExceptionType.badResponse: |
| 113 | return 'Server error (${error. |
| 114 | case DioExceptionType.cancel: |
| 115 | return 'Request cancelled'; |
| 116 | default: |
| 117 | return 'Network error occurred: ${error. |
| 118 | } |
| 119 | } |
| 120 | } |