diff --git a/lib/services/msstore_service.dart b/lib/services/msstore_service.dart index b7b50b0..e404191 100644 --- a/lib/services/msstore_service.dart +++ b/lib/services/msstore_service.dart @@ -4,7 +4,6 @@ import 'package:dio/dio.dart'; import 'package:process_run/shell_run.dart'; import 'package:revitool/services/network_service.dart'; import 'package:xml/xml.dart' as xml; -import 'package:path/path.dart' as p; import 'package:revitool/models/ms_store/non_uwp_response.dart'; import 'package:revitool/models/ms_store/search_response.dart'; import 'package:revitool/models/ms_store/packages_info.dart'; @@ -70,8 +69,6 @@ class MSStoreService { static var _cookie = ""; - static final _dio = Dio(); - static final _shell = Shell(); static final _cancelToken = CancelToken(); static final _networkService = NetworkService(); // final RegistryUtilsService = RegistryUtilsService(); @@ -129,7 +126,7 @@ class MSStoreService { Future> searchProducts(String query, String ring) async { //"$_filteredSearchAPI?&Query=$query&FilteredCategories=AllProducts&hl=en-us${systemLanguage.toLowerCase()}& - final response = await _dio.get( + final response = await _networkService.get( "$_searchAPI?gl=US&hl=en-us&query=$query&mediaType=all&age=all&price=all&category=all&subscription=all", // https://apps.microsoft.com/api/products/search?gl=GE&hl=en-us&query=xbox&cursor= @@ -146,7 +143,7 @@ class MSStoreService { } Future _getCookie() async { - final response = await _dio.post( + final response = await _networkService.post( _fe3Delivery, data: xml.XmlDocument.parse(_cookieFile.readAsStringSync()), options: _optionsSoapXML, @@ -167,7 +164,7 @@ class MSStoreService { // When Windows region is set to English (World), the language code isn't compatible with the store API //"$_storeAPI/products/$id?market=US&locale=en-us&deviceFamily=Windows.Desktop", - final response = await _dio.get( + final response = await _networkService.get( "$_storeAPI/products/$id?market=US&locale=en-us&deviceFamily=Windows.Desktop", cancelToken: _cancelToken); final skus = response.data["Payload"]["Skus"]; @@ -188,7 +185,7 @@ class MSStoreService { .replaceAll("{2}", categoryID) .replaceAll("{3}", ring); - final response = await _dio.post( + final response = await _networkService.post( _fe3Delivery, data: cookie2, options: _optionsSoapXML, @@ -206,7 +203,8 @@ class MSStoreService { } Future> _getNonAppxPackage(String id) async { - final response = await _dio.get("$_storeAPI/packageManifests/$id?Market=US", + final response = await _networkService.get( + "$_storeAPI/packageManifests/$id?Market=US", cancelToken: _cancelToken); if (response.statusCode == 200) { @@ -316,7 +314,7 @@ class MSStoreService { .replaceAll("{2}", revision) .replaceAll("{3}", ring); - final response = await _dio.post("$_fe3Delivery/secured", + final response = await _networkService.post("$_fe3Delivery/secured", data: httpContent, options: Options(headers: { HttpHeaders.contentTypeHeader: "application/soap+xml", diff --git a/lib/services/network_service.dart b/lib/services/network_service.dart index e9f95f0..d5aa966 100644 --- a/lib/services/network_service.dart +++ b/lib/services/network_service.dart @@ -14,7 +14,7 @@ enum ApiEndpoints { } class NetworkService { - static final _dio = Dio(); + final _dio = Dio(); NetworkService() { (_dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () => @@ -23,6 +23,38 @@ class NetworkService { (X509Certificate cert, String host, int port) => true; } + Future> get( + String path, { + Object? data, + Map? queryParameters, + Options? options, + CancelToken? cancelToken, + void Function(int, int)? onReceiveProgress, + }) async { + try { + return await _dio.get(path, options: options, cancelToken: cancelToken); + } catch (e) { + throw Exception( + 'Failed to connect to $path.\n\nPlease ensure you have an active internet connection and try again.\n\nError: $e'); + } + } + + Future> post( + final String path, { + final Object? data, + final Map? queryParameters, + final Options? options, + final CancelToken? cancelToken, + final ProgressCallback? onReceiveProgress, + }) async { + try { + return await _dio.post(path, data: data, options: options); + } catch (e) { + throw Exception( + 'Failed to connect to $path.\n\nPlease ensure you have an active internet connection and try again.\n\nError: $e'); + } + } + Future> getGHLatestRelease(ApiEndpoints endpoint) async { try { final response = await _dio.get( @@ -31,7 +63,7 @@ class NetworkService { return response.data as Map; } catch (e) { throw Exception( - 'Failed to connect to the GitHub API.\n\nPlease ensure you have an active internet connection and try again.'); + 'Failed to connect to the GitHub API.\n\nPlease ensure you have an active internet connection and try again.\n\nError: $e'); } } @@ -46,7 +78,7 @@ class NetworkService { return response; } catch (e) { throw Exception( - 'Failed to download.\n\nPlease ensure you have an active internet connection and try again.'); + 'Failed to download.\n\nPlease ensure you have an active internet connection and try again.\n\nError: $e'); } } } diff --git a/lib/services/win_package_service.dart b/lib/services/win_package_service.dart index c9188ee..bfee9b7 100644 --- a/lib/services/win_package_service.dart +++ b/lib/services/win_package_service.dart @@ -48,10 +48,11 @@ class WinPackageService { RegistryHive.localMachine, cbsPackagesRegPath + key, 'CurrentState')!; final int? lastError = RegistryUtilsService.readInt( - RegistryHive.localMachine, cbsPackagesRegPath + key, 'lastError'); + RegistryHive.localMachine, cbsPackagesRegPath + key, 'LastError'); // installation codes - https://forums.ivanti.com/s/article/Understand-Patch-installation-failure-codes?language=en_US - return currentState != 5 || currentState != 4294967264 || lastError == null; + return (currentState != 5 || currentState != 4294967264) && + lastError == null; } Future downloadPackage(final WinPackageType packageType) async {