Skip to content

Commit

Permalink
fix: Improve API URL handling and force init after setting a new config
Browse files Browse the repository at this point in the history
  • Loading branch information
ponces committed Sep 18, 2022
1 parent c333fa3 commit 6c2ceed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 56 deletions.
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
7 changes: 3 additions & 4 deletions lib/services/github_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
Expand All @@ -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,
Expand Down
35 changes: 7 additions & 28 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ManagerAPI {
if (url.isEmpty || url == ' ') {
url = defaultApiUrl;
}
await _revancedAPI.initialize(url);
await _revancedAPI.clearAllCache();
await _prefs.setString('apiUrl', url);
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand Down
35 changes: 13 additions & 22 deletions lib/services/revanced_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ 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);
}

Future<void> clearAllCache() async {
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'];
Expand All @@ -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 {
Expand All @@ -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) =>
Expand All @@ -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'];
}
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand Down

0 comments on commit 6c2ceed

Please sign in to comment.