import 'dart:async'; import 'package:dartz/dartz.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:shlink_app/API/Classes/ShlinkStats/ShlinkStats.dart'; import 'package:shlink_app/API/Classes/ShortURL/ShortURL.dart'; import 'package:shlink_app/API/Classes/ShortURLSubmission/ShortURLSubmission.dart'; import 'package:shlink_app/API/Methods/connect.dart'; import 'package:shlink_app/API/Methods/getRecentShortUrls.dart'; import 'package:shlink_app/API/Methods/getServerHealth.dart'; import 'package:shlink_app/API/Methods/getShlinkStats.dart'; import 'package:shlink_app/API/Methods/getShortUrls.dart'; import 'Methods/deleteShortUrl.dart'; import 'Methods/submitShortUrl.dart'; class ServerManager { String? _server_url; String? _api_key; static String apiVersion = "3"; String getServerUrl() { return _server_url ?? ""; } String getApiVersion() { return apiVersion; } Future checkLogin() async { await _loadCredentials(); return (_server_url != null); } Future logOut() async { const storage = FlutterSecureStorage(); await storage.delete(key: "shlink_url"); await storage.delete(key: "shlink_apikey"); } Future _loadCredentials() async { const storage = FlutterSecureStorage(); final prefs = await SharedPreferences.getInstance(); if (prefs.getBool('first_run') ?? true) { FlutterSecureStorage storage = FlutterSecureStorage(); await storage.deleteAll(); prefs.setBool('first_run', false); } _server_url = await storage.read(key: "shlink_url"); _api_key = await storage.read(key: "shlink_apikey"); } void _saveCredentials(String url, String apiKey) async { const storage = FlutterSecureStorage(); storage.write(key: "shlink_url", value: url); storage.write(key: "shlink_apikey", value: apiKey); } void _removeCredentials() async { const storage = FlutterSecureStorage(); storage.delete(key: "shlink_url"); storage.delete(key: "shlink_apikey"); } FutureOr> initAndConnect(String url, String apiKey) async { // TODO: convert url to correct format _server_url = url; _api_key = apiKey; _saveCredentials(url, apiKey); final result = await connect(); result.fold((l) => null, (r) { _removeCredentials(); }); return result; } FutureOr> connect() async { _loadCredentials(); return API_connect(_api_key, _server_url, apiVersion); } FutureOr, Failure>> getShortUrls() async { return API_getShortUrls(_api_key, _server_url, apiVersion); } FutureOr> getShlinkStats() async { return API_getShlinkStats(_api_key, _server_url, apiVersion); } FutureOr> submitShortUrl(ShortURLSubmission shortUrl) async { return API_submitShortUrl(shortUrl, _api_key, _server_url, apiVersion); } FutureOr> deleteShortUrl(String shortCode) async { return API_deleteShortUrl(shortCode, _api_key, _server_url, apiVersion); } FutureOr> getServerHealth() async { return API_getServerHealth(_api_key, _server_url, apiVersion); } FutureOr, Failure>> getRecentShortUrls() async { return API_getRecentShortUrls(_api_key, _server_url, apiVersion); } } class ShortURLPageResponse { List urls; int totalPages; ShortURLPageResponse(this.urls, this.totalPages); } class ServerHealthResponse { String status; String version; ServerHealthResponse({required this.status, required this.version}); } abstract class Failure {} class RequestFailure extends Failure { int statusCode; String description; RequestFailure(this.statusCode, this.description); } class ApiFailure extends Failure { String type; String detail; String title; int status; List? invalidElements; ApiFailure({required this.type, required this.detail, required this.title, required this.status, this.invalidElements}); }