adguard-home-manager/lib/services/external_requests.dart

55 lines
1.5 KiB
Dart
Raw Normal View History

2023-11-19 22:52:40 +01:00
import 'dart:convert';
2023-11-23 01:05:18 +01:00
import 'package:http/http.dart' as http;
2023-11-19 22:52:40 +01:00
import 'package:adguard_home_manager/models/github_release.dart';
import 'package:adguard_home_manager/constants/urls.dart';
import 'package:adguard_home_manager/services/api_client.dart';
class ExternalRequests {
static Future<ApiResponse> getReleasesGitHub() async {
try {
2023-11-23 01:05:18 +01:00
final response = await http.get(Uri.parse(Urls.getReleasesGitHub));
2023-11-19 22:52:40 +01:00
if (response.statusCode == 200) {
return ApiResponse(
successful: true,
2023-11-23 01:05:18 +01:00
content: List<GitHubRelease>.from(
jsonDecode(response.body).map((entry) => GitHubRelease.fromJson(entry))
)
2023-11-19 22:52:40 +01:00
);
}
else {
return const ApiResponse(successful: false);
}
2023-11-23 01:05:18 +01:00
} catch (e) {
2023-11-19 22:52:40 +01:00
return const ApiResponse(successful: false);
}
}
2023-11-23 01:05:18 +01:00
static Future<ApiResponse> getReleaseData({
// If releaseTag is null gets latest release
String? releaseTag
}) async {
2023-11-19 22:52:40 +01:00
try {
2023-11-23 01:05:18 +01:00
final response = await http.get(
Uri.parse(
releaseTag != null
? "${Urls.adGuardHomeReleasesTags}/$releaseTag"
: Urls.getLatestReleaseGitHub
)
);
2023-11-19 22:52:40 +01:00
if (response.statusCode == 200) {
return ApiResponse(
successful: true,
2023-11-23 01:05:18 +01:00
content: GitHubRelease.fromJson(jsonDecode(response.body)),
2023-11-19 22:52:40 +01:00
statusCode: response.statusCode
);
}
else {
return const ApiResponse(successful: false);
}
2023-11-23 01:05:18 +01:00
} catch (e) {
2023-11-19 22:52:40 +01:00
return const ApiResponse(successful: false);
}
}
}