2026-02-27 12:28:16 -05:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2026-03-02 13:59:50 -05:00
|
|
|
import 'package:encrypter/encrypter/xor.dart';
|
|
|
|
|
import 'package:frontend/utils/pref_utils.dart';
|
|
|
|
|
import '../model/notification.dart';
|
2026-02-27 12:28:16 -05:00
|
|
|
|
|
|
|
|
class BackendAPI {
|
2026-03-02 13:59:50 -05:00
|
|
|
static final BackendAPI _instance = BackendAPI._internal();
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
static BackendAPI get instance => _instance;
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
BackendAPI._internal() {
|
|
|
|
|
// _baseUrl =
|
|
|
|
|
// PrefUtil.getValue("base_url", "http://localhost:3000/api") as String;
|
|
|
|
|
// _apiKey = XOR().xorDecode(PrefUtil.getValue("api_key", "") as String);
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
_baseUrl = "http://localhost:3000/api";
|
|
|
|
|
_apiKey = "super_secret";
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
print('Base URL: $_baseUrl');
|
|
|
|
|
print('API KEY $_apiKey');
|
|
|
|
|
|
|
|
|
|
_dio = Dio(
|
|
|
|
|
BaseOptions(
|
|
|
|
|
baseUrl: _baseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
HttpHeaders.contentTypeHeader: 'application/json',
|
|
|
|
|
HttpHeaders.authorizationHeader: 'Bearer $_apiKey',
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
late String _baseUrl;
|
|
|
|
|
late String _apiKey;
|
|
|
|
|
late Dio _dio;
|
|
|
|
|
|
|
|
|
|
Future<List<Notification>> listNotifications() async {
|
2026-02-27 12:28:16 -05:00
|
|
|
print('About to call /notifications');
|
|
|
|
|
|
|
|
|
|
Response response;
|
|
|
|
|
response = await _dio.get('/notifications');
|
|
|
|
|
|
|
|
|
|
print('Received: ' + response.data.toString());
|
|
|
|
|
|
|
|
|
|
List<dynamic> list = response.data;
|
|
|
|
|
print('List contains ' + list.length.toString() + ' items');
|
|
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
List<Notification> events = List<Notification>.from(
|
|
|
|
|
list.map((item) => Notification.fromJson(item)),
|
2026-02-27 12:28:16 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
print('Parsed ' + events.length.toString() + ' events');
|
|
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
}
|