2022-09-11 03:01:06 +02:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
|
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
|
|
import 'package:injectable/injectable.dart';
|
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
|
|
|
import 'package:timeago/timeago.dart';
|
2022-09-23 15:36:06 +02:00
|
|
|
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
|
2022-09-11 03:01:06 +02:00
|
|
|
|
|
|
|
@lazySingleton
|
|
|
|
class RevancedAPI {
|
2022-09-19 01:28:26 +02:00
|
|
|
late Dio _dio = Dio();
|
2022-09-11 03:01:06 +02:00
|
|
|
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
|
|
|
|
final Options _cacheOptions = buildCacheOptions(
|
2022-09-15 13:33:19 +02:00
|
|
|
const Duration(days: 1),
|
2022-09-11 03:01:06 +02:00
|
|
|
maxStale: const Duration(days: 7),
|
|
|
|
);
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<void> initialize(String apiUrl) async {
|
2022-09-23 15:36:06 +02:00
|
|
|
_dio = Dio(BaseOptions(
|
|
|
|
baseUrl: apiUrl,
|
|
|
|
))
|
|
|
|
..httpClientAdapter = Http2Adapter(
|
|
|
|
ConnectionManager(
|
|
|
|
idleTimeout: 10000,
|
|
|
|
onClientCreate: (_, config) => config.onBadCertificate = (_) => true,
|
|
|
|
),
|
|
|
|
);
|
2022-09-11 03:01:06 +02:00
|
|
|
_dio.interceptors.add(_dioCacheManager.interceptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
|
|
|
await _dioCacheManager.clearAll();
|
|
|
|
}
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<Map<String, List<dynamic>>> getContributors() async {
|
2022-09-11 03:01:06 +02:00
|
|
|
Map<String, List<dynamic>> contributors = {};
|
|
|
|
try {
|
2022-09-19 01:28:26 +02:00
|
|
|
var response = await _dio.get('/contributors', options: _cacheOptions);
|
2022-09-11 03:01:06 +02:00
|
|
|
List<dynamic> repositories = response.data['repositories'];
|
|
|
|
for (Map<String, dynamic> repo in repositories) {
|
|
|
|
String name = repo['name'];
|
|
|
|
contributors[name] = repo['contributors'];
|
|
|
|
}
|
|
|
|
} on Exception {
|
2022-09-12 10:18:03 +02:00
|
|
|
return {};
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
return contributors;
|
|
|
|
}
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<List<Patch>> getPatches() async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2022-09-19 01:28:26 +02:00
|
|
|
var response = await _dio.get('/patches', options: _cacheOptions);
|
2022-09-11 03:01:06 +02:00
|
|
|
List<dynamic> patches = response.data;
|
|
|
|
return patches.map((patch) => Patch.fromJson(patch)).toList();
|
|
|
|
} on Exception {
|
2022-09-12 10:18:03 +02:00
|
|
|
return List.empty();
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>?> _getLatestRelease(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
|
|
|
try {
|
2022-09-19 01:28:26 +02:00
|
|
|
var response = await _dio.get('/tools', options: _cacheOptions);
|
2022-09-11 03:01:06 +02:00
|
|
|
List<dynamic> tools = response.data['tools'];
|
|
|
|
return tools.firstWhereOrNull(
|
|
|
|
(t) =>
|
|
|
|
t['repository'] == repoName &&
|
|
|
|
(t['name'] as String).endsWith(extension),
|
|
|
|
);
|
|
|
|
} on Exception {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> getLatestReleaseVersion(
|
2022-09-18 19:42:30 +02:00
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2022-09-19 01:28:26 +02:00
|
|
|
Map<String, dynamic>? release = await _getLatestRelease(
|
|
|
|
extension,
|
|
|
|
repoName,
|
|
|
|
);
|
2022-09-11 03:01:06 +02:00
|
|
|
if (release != null) {
|
|
|
|
return release['version'];
|
|
|
|
}
|
|
|
|
} on Exception {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<File?> getLatestReleaseFile(String extension, String repoName) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
|
|
|
Map<String, dynamic>? release = await _getLatestRelease(
|
|
|
|
extension,
|
|
|
|
repoName,
|
|
|
|
);
|
|
|
|
if (release != null) {
|
|
|
|
String url = release['browser_download_url'];
|
|
|
|
return await DefaultCacheManager().getSingleFile(url);
|
|
|
|
}
|
|
|
|
} on Exception {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> getLatestReleaseTime(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
Map<String, dynamic>? release = await _getLatestRelease(
|
|
|
|
extension,
|
|
|
|
repoName,
|
|
|
|
);
|
|
|
|
if (release != null) {
|
|
|
|
DateTime timestamp = DateTime.parse(release['timestamp'] as String);
|
|
|
|
return format(timestamp, locale: 'en_short');
|
|
|
|
}
|
|
|
|
} on Exception {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|