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(); await setupLocator();
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await locator<ManagerAPI>().initialize(); await locator<ManagerAPI>().initialize();
await locator<PatcherAPI>().initialize(); String apiUrl = locator<ManagerAPI>().getApiUrl();
locator<RevancedAPI>().initialize(); await locator<RevancedAPI>().initialize(apiUrl);
locator<GithubAPI>().initialize(); locator<GithubAPI>().initialize();
await locator<PatcherAPI>().initialize();
runApp(const MyApp()); runApp(const MyApp());
} }

View File

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

View File

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

View File

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