diff --git a/lib/main.dart b/lib/main.dart index 3fcf32bb..141d32fe 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,9 +15,10 @@ Future main() async { await setupLocator(); WidgetsFlutterBinding.ensureInitialized(); await locator().initialize(); - await locator().initialize(); - locator().initialize(); + String apiUrl = locator().getApiUrl(); + await locator().initialize(apiUrl); locator().initialize(); + await locator().initialize(); runApp(const MyApp()); } diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index 625a189b..3af68e0e 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -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?> _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, diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index eed5e4e3..679454d0 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -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>> getContributors() async { - String apiUrl = getApiUrl(); - return await _revancedAPI.getContributors(apiUrl); + return await _revancedAPI.getContributors(); } Future> 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 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 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 downloadManager() async { - String apiUrl = getApiUrl(); - return await _revancedAPI.getLatestReleaseFile( - apiUrl, - '.apk', - defaultManagerRepo, - ); + return await _revancedAPI.getLatestReleaseFile('.apk', defaultManagerRepo); } Future getLatestPatcherReleaseTime() async { - String apiUrl = getApiUrl(); - return await _revancedAPI.getLatestReleaseTime( - apiUrl, - '.gz', - defaultPatcherRepo, - ); + return await _revancedAPI.getLatestReleaseTime('.gz', defaultPatcherRepo); } Future getLatestManagerReleaseTime() async { - String apiUrl = getApiUrl(); - return await _revancedAPI.getLatestReleaseTime( - apiUrl, - '.apk', - defaultManagerRepo, - ); + return await _revancedAPI.getLatestReleaseTime('.apk', defaultManagerRepo); } Future getLatestManagerVersion() async { - String apiUrl = getApiUrl(); return await _revancedAPI.getLatestReleaseVersion( - apiUrl, '.apk', defaultManagerRepo, ); diff --git a/lib/services/revanced_api.dart b/lib/services/revanced_api.dart index a75e32a1..5c2b9433 100644 --- a/lib/services/revanced_api.dart +++ b/lib/services/revanced_api.dart @@ -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 initialize() async { + Future initialize(String apiUrl) async { + _dio = Dio(BaseOptions(baseUrl: apiUrl)); _dio.interceptors.add(_dioCacheManager.interceptor); } @@ -24,13 +25,10 @@ class RevancedAPI { await _dioCacheManager.clearAll(); } - Future>> getContributors(String apiUrl) async { + Future>> getContributors() async { Map> contributors = {}; try { - var response = await _dio.get( - '$apiUrl/contributors', - options: _cacheOptions, - ); + var response = await _dio.get('/contributors', options: _cacheOptions); List repositories = response.data['repositories']; for (Map repo in repositories) { String name = repo['name']; @@ -42,9 +40,9 @@ class RevancedAPI { return contributors; } - Future> getPatches(String apiUrl) async { + Future> getPatches() async { try { - var response = await _dio.get('$apiUrl/patches', options: _cacheOptions); + var response = await _dio.get('/patches', options: _cacheOptions); List patches = response.data; return patches.map((patch) => Patch.fromJson(patch)).toList(); } on Exception { @@ -53,12 +51,11 @@ class RevancedAPI { } Future?> _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 tools = response.data['tools']; return tools.firstWhereOrNull( (t) => @@ -71,13 +68,14 @@ class RevancedAPI { } Future getLatestReleaseVersion( - String apiUrl, String extension, String repoName, ) async { try { - Map? release = - await _getLatestRelease(apiUrl, extension, repoName); + Map? release = await _getLatestRelease( + extension, + repoName, + ); if (release != null) { return release['version']; } @@ -87,14 +85,9 @@ class RevancedAPI { return null; } - Future getLatestReleaseFile( - String apiUrl, - String extension, - String repoName, - ) async { + Future getLatestReleaseFile(String extension, String repoName) async { try { Map? release = await _getLatestRelease( - apiUrl, extension, repoName, ); @@ -109,13 +102,11 @@ class RevancedAPI { } Future getLatestReleaseTime( - String apiUrl, String extension, String repoName, ) async { try { Map? release = await _getLatestRelease( - apiUrl, extension, repoName, );