|
| 1 | +import 'package:dio/dio.dart'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | + |
| 4 | +import '../../config/config.dart'; |
| 5 | +import '../local/storage_service.dart'; |
| 6 | + |
| 7 | +class ApiService { |
| 8 | + /// Service initializer |
| 9 | + static void init() => _channel = Dio(); |
| 10 | + |
| 11 | + // Data |
| 12 | + /// The [Dio] channel through which all requests will be routed. |
| 13 | + static late final Dio _channel; |
| 14 | + |
| 15 | + /// Safe method to send GET request to an endpoint **below** [Environment.baseUrl]. |
| 16 | + static Future<Map<String, dynamic>> get( |
| 17 | + String endpoint, { |
| 18 | + String? baseUrl, |
| 19 | + Map<String, dynamic>? headers, |
| 20 | + Map<String, dynamic>? query, |
| 21 | + }) async { |
| 22 | + Response? response; |
| 23 | + try { |
| 24 | + response = await _channel.get( |
| 25 | + (baseUrl ?? Environment.baseUrl) + endpoint, |
| 26 | + queryParameters: query, |
| 27 | + options: Options( |
| 28 | + validateStatus: (status) { |
| 29 | + return status! < 500; |
| 30 | + }, |
| 31 | + headers: headers, |
| 32 | + ), |
| 33 | + ); |
| 34 | + return { |
| 35 | + 'status_code': response.statusCode ?? 0, |
| 36 | + 'data': response.data ?? 'null', |
| 37 | + }; |
| 38 | + } on Exception catch (exception, stacktrace) { |
| 39 | + debugPrint( |
| 40 | + 'ERROR: Failed during a GET request.\n' |
| 41 | + 'Endpoint: $endpoint\nQuery: $query\n' |
| 42 | + 'Exception: $exception\nStacktrace: $stacktrace', |
| 43 | + ); |
| 44 | + } |
| 45 | + return { |
| 46 | + 'status_code': response?.statusCode ?? 0, |
| 47 | + 'data': response?.data ?? 'null', |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + /// Safe method to send POST request to an endpoint **below** [Environment.baseUrl]. |
| 52 | + static Future<Map<String, dynamic>> post( |
| 53 | + String endpoint, { |
| 54 | + Map<String, dynamic>? headers, |
| 55 | + Map<String, dynamic>? data, |
| 56 | + }) async { |
| 57 | + Response? response; |
| 58 | + try { |
| 59 | + response = await _channel.post( |
| 60 | + Environment.baseUrl + endpoint, |
| 61 | + data: data, |
| 62 | + options: Options( |
| 63 | + validateStatus: (status) { |
| 64 | + return status! < 500; |
| 65 | + }, |
| 66 | + headers: headers, |
| 67 | + ), |
| 68 | + ); |
| 69 | + return { |
| 70 | + 'status_code': response.statusCode ?? 0, |
| 71 | + 'data': response.data ?? 'null', |
| 72 | + }; |
| 73 | + } on Exception catch (exception, stacktrace) { |
| 74 | + debugPrint( |
| 75 | + 'ERROR: Failed during a POST request.\n' |
| 76 | + 'Endpoint: $endpoint\nData: $data\n' |
| 77 | + 'Exception: $exception\nStacktrace: $stacktrace', |
| 78 | + ); |
| 79 | + } |
| 80 | + return { |
| 81 | + 'status_code': response?.statusCode ?? 0, |
| 82 | + 'data': response?.data ?? 'null', |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + /// Safe method to send PUT request to an endpoint **below** [Environment.baseUrl]. |
| 87 | + static Future<Map<String, dynamic>> put( |
| 88 | + String endpoint, { |
| 89 | + Map<String, dynamic>? headers, |
| 90 | + Map<String, dynamic>? data, |
| 91 | + }) async { |
| 92 | + Response? response; |
| 93 | + try { |
| 94 | + response = await _channel.put( |
| 95 | + Environment.baseUrl + endpoint, |
| 96 | + data: data, |
| 97 | + options: Options( |
| 98 | + validateStatus: (status) { |
| 99 | + return status! < 500; |
| 100 | + }, |
| 101 | + headers: headers, |
| 102 | + ), |
| 103 | + ); |
| 104 | + return { |
| 105 | + 'status_code': response.statusCode ?? 0, |
| 106 | + 'data': response.data ?? 'null', |
| 107 | + }; |
| 108 | + } on Exception catch (exception, stacktrace) { |
| 109 | + debugPrint( |
| 110 | + 'ERROR: Failed during a PUT request.\n' |
| 111 | + 'Endpoint: $endpoint\nData: $data\n' |
| 112 | + 'Exception: $exception\nStacktrace: $stacktrace', |
| 113 | + ); |
| 114 | + } |
| 115 | + return { |
| 116 | + 'status_code': response?.statusCode ?? 0, |
| 117 | + 'data': response?.data ?? 'null', |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + /// Adds common tokens to outgoing requests. |
| 122 | + static void addTokenToHeaders(Map<String, dynamic> headers) { |
| 123 | + final token = StorageService.authToken; |
| 124 | + headers.addAll({'authorization': token}); |
| 125 | + } |
| 126 | +} |
0 commit comments