2022-11-10 23:03:07 +05:30
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
|
|
|
|
import 'package:injectable/injectable.dart' hide Environment;
|
|
|
|
import 'package:revanced_manager/utils/environment.dart';
|
|
|
|
import 'package:sentry_dio/sentry_dio.dart';
|
|
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
|
|
|
|
@lazySingleton
|
|
|
|
class CrowdinAPI {
|
2023-02-21 18:06:56 +05:45
|
|
|
CrowdinAPI() {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
Dio _dio = Dio();
|
|
|
|
DioCacheManager get _dioCacheManager => DioCacheManager(CacheConfig());
|
|
|
|
String get apiKey => Environment.crowdinKEY;
|
2022-11-10 23:03:07 +05:30
|
|
|
|
2023-02-21 18:06:56 +05:45
|
|
|
void initialize() {
|
2022-11-10 23:03:07 +05:30
|
|
|
try {
|
2023-01-30 18:05:06 +05:30
|
|
|
_dio = Dio(
|
|
|
|
BaseOptions(
|
|
|
|
baseUrl: 'https://api.crowdin.com/api/v2',
|
|
|
|
),
|
|
|
|
);
|
2022-11-10 23:03:07 +05:30
|
|
|
|
|
|
|
_dio.interceptors.add(_dioCacheManager.interceptor);
|
|
|
|
_dio.addSentry(
|
|
|
|
captureFailedRequests: true,
|
|
|
|
);
|
|
|
|
} on Exception catch (e, s) {
|
2023-02-21 18:06:56 +05:45
|
|
|
Sentry.captureException(e, stackTrace: s).ignore();
|
2022-11-10 23:03:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
|
|
|
try {
|
|
|
|
await _dioCacheManager.clearAll();
|
|
|
|
} on Exception catch (e, s) {
|
2023-02-21 18:06:56 +05:45
|
|
|
Sentry.captureException(e, stackTrace: s).ignore();
|
2022-11-10 23:03:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> getLanguages() async {
|
|
|
|
try {
|
2023-01-30 18:05:06 +05:30
|
|
|
final response = await _dio.get(
|
2022-11-10 23:03:07 +05:30
|
|
|
'/projects',
|
|
|
|
options: buildCacheOptions(
|
|
|
|
const Duration(hours: 6),
|
|
|
|
maxStale: const Duration(days: 1),
|
|
|
|
options: Options(
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'Bearer $apiKey',
|
|
|
|
},
|
|
|
|
contentType: 'application/json',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-01-30 18:05:06 +05:30
|
|
|
final List targetLanguages =
|
2022-11-10 23:03:07 +05:30
|
|
|
await response.data['data'][0]['data']['targetLanguages'];
|
|
|
|
|
|
|
|
return targetLanguages;
|
|
|
|
} on Exception catch (e, s) {
|
2023-02-21 18:06:56 +05:45
|
|
|
Sentry.captureException(e, stackTrace: s).ignore();
|
2022-11-10 23:03:07 +05:30
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|