2023-06-26 23:40:05 +02:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2023-07-09 23:00:00 +02:00
|
|
|
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';
|
2023-07-10 17:32:14 +02:00
|
|
|
import 'package:shlink_app/API/Methods/getRecentShortUrls.dart';
|
|
|
|
import 'package:shlink_app/API/Methods/getServerHealth.dart';
|
2023-07-09 23:00:00 +02:00
|
|
|
import 'package:shlink_app/API/Methods/getShlinkStats.dart';
|
|
|
|
import 'package:shlink_app/API/Methods/getShortUrls.dart';
|
|
|
|
|
|
|
|
import 'Methods/deleteShortUrl.dart';
|
|
|
|
import 'Methods/submitShortUrl.dart';
|
2023-06-26 23:40:05 +02:00
|
|
|
|
|
|
|
class ServerManager {
|
|
|
|
|
|
|
|
String? _server_url;
|
|
|
|
String? _api_key;
|
|
|
|
|
|
|
|
static String apiVersion = "3";
|
|
|
|
|
|
|
|
String getServerUrl() {
|
|
|
|
return _server_url ?? "";
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:32:14 +02:00
|
|
|
String getApiVersion() {
|
|
|
|
return apiVersion;
|
|
|
|
}
|
|
|
|
|
2023-06-26 23:40:05 +02:00
|
|
|
Future<bool> checkLogin() async {
|
|
|
|
await _loadCredentials();
|
|
|
|
return (_server_url != null);
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:15:15 +02:00
|
|
|
Future<void> logOut() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
await storage.delete(key: "shlink_url");
|
|
|
|
await storage.delete(key: "shlink_apikey");
|
|
|
|
}
|
|
|
|
|
2023-06-26 23:40:05 +02:00
|
|
|
Future<void> _loadCredentials() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
_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<Either<String, Failure>> 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<Either<String, Failure>> connect() async {
|
|
|
|
_loadCredentials();
|
2023-07-09 23:00:00 +02:00
|
|
|
return API_connect(_api_key, _server_url, apiVersion);
|
2023-06-26 23:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FutureOr<Either<List<ShortURL>, Failure>> getShortUrls() async {
|
2023-07-09 23:00:00 +02:00
|
|
|
return API_getShortUrls(_api_key, _server_url, apiVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureOr<Either<ShlinkStats, Failure>> getShlinkStats() async {
|
|
|
|
return API_getShlinkStats(_api_key, _server_url, apiVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureOr<Either<String, Failure>> submitShortUrl(ShortURLSubmission shortUrl) async {
|
|
|
|
return API_submitShortUrl(shortUrl, _api_key, _server_url, apiVersion);
|
2023-06-26 23:40:05 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:00:00 +02:00
|
|
|
FutureOr<Either<String, Failure>> deleteShortUrl(String shortCode) async {
|
|
|
|
return API_deleteShortUrl(shortCode, _api_key, _server_url, apiVersion);
|
2023-06-26 23:40:05 +02:00
|
|
|
}
|
2023-07-10 17:32:14 +02:00
|
|
|
|
|
|
|
FutureOr<Either<ServerHealthResponse, Failure>> getServerHealth() async {
|
|
|
|
return API_getServerHealth(_api_key, _server_url, apiVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureOr<Either<List<ShortURL>, Failure>> getRecentShortUrls() async {
|
|
|
|
return API_getRecentShortUrls(_api_key, _server_url, apiVersion);
|
|
|
|
}
|
2023-06-26 23:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ShortURLPageResponse {
|
|
|
|
List<ShortURL> urls;
|
|
|
|
int totalPages;
|
|
|
|
|
|
|
|
ShortURLPageResponse(this.urls, this.totalPages);
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:32:14 +02:00
|
|
|
class ServerHealthResponse {
|
|
|
|
String status;
|
|
|
|
String version;
|
|
|
|
|
|
|
|
ServerHealthResponse({required this.status, required this.version});
|
|
|
|
}
|
|
|
|
|
2023-06-26 23:40:05 +02:00
|
|
|
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;
|
2023-07-09 23:00:00 +02:00
|
|
|
List<dynamic>? invalidElements;
|
2023-06-26 23:40:05 +02:00
|
|
|
|
2023-07-09 23:00:00 +02:00
|
|
|
ApiFailure({required this.type, required this.detail, required this.title, required this.status, this.invalidElements});
|
2023-06-26 23:40:05 +02:00
|
|
|
}
|