fix: Improve API URL handling and force init after setting a new config

This commit is contained in:
Alberto Ponces 2022-09-19 00:28:26 +01:00
parent c333fa3c33
commit 6c2ceed91f
4 changed files with 26 additions and 56 deletions

View File

@ -15,9 +15,10 @@ Future main() async {
await setupLocator();
WidgetsFlutterBinding.ensureInitialized();
await locator<ManagerAPI>().initialize();
await locator<PatcherAPI>().initialize();
locator<RevancedAPI>().initialize();
String apiUrl = locator<ManagerAPI>().getApiUrl();
await locator<RevancedAPI>().initialize(apiUrl);
locator<GithubAPI>().initialize();
await locator<PatcherAPI>().initialize();
runApp(const MyApp());
}

View File

@ -9,8 +9,7 @@ import 'package:revanced_manager/models/patch.dart';
@lazySingleton
class GithubAPI {
final String apiUrl = 'https://api.github.com';
final Dio _dio = Dio();
final Dio _dio = Dio(BaseOptions(baseUrl: 'https://api.github.com'));
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
const Duration(days: 1),
@ -37,7 +36,7 @@ class GithubAPI {
Future<Map<String, dynamic>?> _getLatestRelease(String repoName) async {
try {
var response = await _dio.get(
'$apiUrl/repos/$repoName/releases/latest',
'/repos/$repoName/releases/latest',
options: _cacheOptions,
);
return response.data;
@ -55,7 +54,7 @@ class GithubAPI {
'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}';
try {
var response = await _dio.get(
'$apiUrl/repos/$repoName/commits',
'/repos/$repoName/commits',
queryParameters: {
'path': path,
'per_page': 3,

View File

@ -38,6 +38,8 @@ class ManagerAPI {
if (url.isEmpty || url == ' ') {
url = defaultApiUrl;
}
await _revancedAPI.initialize(url);
await _revancedAPI.clearAllCache();
await _prefs.setString('apiUrl', url);
}
@ -118,15 +120,13 @@ class ManagerAPI {
}
Future<Map<String, List<dynamic>>> getContributors() async {
String apiUrl = getApiUrl();
return await _revancedAPI.getContributors(apiUrl);
return await _revancedAPI.getContributors();
}
Future<List<Patch>> getPatches() async {
String repoName = getPatchesRepo();
if (repoName == defaultPatchesRepo) {
String apiUrl = getApiUrl();
return await _revancedAPI.getPatches(apiUrl);
return await _revancedAPI.getPatches();
} else {
return await _githubAPI.getPatches(repoName);
}
@ -135,9 +135,7 @@ class ManagerAPI {
Future<File?> downloadPatches() async {
String repoName = getPatchesRepo();
if (repoName == defaultPatchesRepo) {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseFile(
apiUrl,
'.jar',
defaultPatchesRepo,
);
@ -149,9 +147,7 @@ class ManagerAPI {
Future<File?> downloadIntegrations() async {
String repoName = getIntegrationsRepo();
if (repoName == defaultIntegrationsRepo) {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseFile(
apiUrl,
'.apk',
defaultIntegrationsRepo,
);
@ -161,36 +157,19 @@ class ManagerAPI {
}
Future<File?> downloadManager() async {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseFile(
apiUrl,
'.apk',
defaultManagerRepo,
);
return await _revancedAPI.getLatestReleaseFile('.apk', defaultManagerRepo);
}
Future<String?> getLatestPatcherReleaseTime() async {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseTime(
apiUrl,
'.gz',
defaultPatcherRepo,
);
return await _revancedAPI.getLatestReleaseTime('.gz', defaultPatcherRepo);
}
Future<String?> getLatestManagerReleaseTime() async {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseTime(
apiUrl,
'.apk',
defaultManagerRepo,
);
return await _revancedAPI.getLatestReleaseTime('.apk', defaultManagerRepo);
}
Future<String?> getLatestManagerVersion() async {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseVersion(
apiUrl,
'.apk',
defaultManagerRepo,
);

View File

@ -9,14 +9,15 @@ import 'package:timeago/timeago.dart';
@lazySingleton
class RevancedAPI {
final Dio _dio = Dio();
late Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
const Duration(days: 1),
maxStale: const Duration(days: 7),
);
Future<void> initialize() async {
Future<void> initialize(String apiUrl) async {
_dio = Dio(BaseOptions(baseUrl: apiUrl));
_dio.interceptors.add(_dioCacheManager.interceptor);
}
@ -24,13 +25,10 @@ class RevancedAPI {
await _dioCacheManager.clearAll();
}
Future<Map<String, List<dynamic>>> getContributors(String apiUrl) async {
Future<Map<String, List<dynamic>>> getContributors() async {
Map<String, List<dynamic>> contributors = {};
try {
var response = await _dio.get(
'$apiUrl/contributors',
options: _cacheOptions,
);
var response = await _dio.get('/contributors', options: _cacheOptions);
List<dynamic> repositories = response.data['repositories'];
for (Map<String, dynamic> repo in repositories) {
String name = repo['name'];
@ -42,9 +40,9 @@ class RevancedAPI {
return contributors;
}
Future<List<Patch>> getPatches(String apiUrl) async {
Future<List<Patch>> getPatches() async {
try {
var response = await _dio.get('$apiUrl/patches', options: _cacheOptions);
var response = await _dio.get('/patches', options: _cacheOptions);
List<dynamic> patches = response.data;
return patches.map((patch) => Patch.fromJson(patch)).toList();
} on Exception {
@ -53,12 +51,11 @@ class RevancedAPI {
}
Future<Map<String, dynamic>?> _getLatestRelease(
String apiUrl,
String extension,
String repoName,
) async {
try {
var response = await _dio.get('$apiUrl/tools', options: _cacheOptions);
var response = await _dio.get('/tools', options: _cacheOptions);
List<dynamic> tools = response.data['tools'];
return tools.firstWhereOrNull(
(t) =>
@ -71,13 +68,14 @@ class RevancedAPI {
}
Future<String?> getLatestReleaseVersion(
String apiUrl,
String extension,
String repoName,
) async {
try {
Map<String, dynamic>? release =
await _getLatestRelease(apiUrl, extension, repoName);
Map<String, dynamic>? release = await _getLatestRelease(
extension,
repoName,
);
if (release != null) {
return release['version'];
}
@ -87,14 +85,9 @@ class RevancedAPI {
return null;
}
Future<File?> getLatestReleaseFile(
String apiUrl,
String extension,
String repoName,
) async {
Future<File?> getLatestReleaseFile(String extension, String repoName) async {
try {
Map<String, dynamic>? release = await _getLatestRelease(
apiUrl,
extension,
repoName,
);
@ -109,13 +102,11 @@ class RevancedAPI {
}
Future<String?> getLatestReleaseTime(
String apiUrl,
String extension,
String repoName,
) async {
try {
Map<String, dynamic>? release = await _getLatestRelease(
apiUrl,
extension,
repoName,
);