diff --git a/CHANGELOG.md b/CHANGELOG.md index 5926476c..e9528201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Next + +- All print statements for errors and exceptions have been placed behind a debug flag. + ## 8.1.0-alpha.2 - Fixed an issue related to the stream updates from the previous release of 8.1.0-alpha.1. diff --git a/bin/itunes_lookup.dart b/bin/itunes_lookup.dart index 1bd4c8ad..036780f9 100644 --- a/bin/itunes_lookup.dart +++ b/bin/itunes_lookup.dart @@ -31,7 +31,7 @@ void main(List arguments) async { } final iTunes = ITunesSearchAPI(); - iTunes.debugEnabled = true; + iTunes.debugLogging = true; const countryCode = 'US'; Map? results; @@ -52,12 +52,12 @@ void main(List arguments) async { return; } - final bundleId = ITunesResults.bundleId(results); - final description = ITunesResults.description(results); - final minAppVersion = ITunesResults.minAppVersion(results); - final releaseNotes = ITunesResults.releaseNotes(results); - final trackViewUrl = ITunesResults.trackViewUrl(results); - final version = ITunesResults.version(results); + final bundleId = iTunes.bundleId(results); + final description = iTunes.description(results); + final minAppVersion = iTunes.minAppVersion(results); + final releaseNotes = iTunes.releaseNotes(results); + final trackViewUrl = iTunes.trackViewUrl(results); + final version = iTunes.version(results); print('itunes_lookup bundleId: $bundleId'); print('itunes_lookup description: $description'); diff --git a/bin/playstore_lookup.dart b/bin/playstore_lookup.dart index a5a19fe7..ac9872d2 100644 --- a/bin/playstore_lookup.dart +++ b/bin/playstore_lookup.dart @@ -36,7 +36,7 @@ void main(List arguments) async { } final playStore = PlayStoreSearchAPI(); - playStore.debugEnabled = true; + playStore.debugLogging = true; final results = await playStore.lookupById(lookupId, country: lookupCountry); @@ -45,10 +45,10 @@ void main(List arguments) async { return; } - final description = PlayStoreResults.description(results); - final minAppVersion = PlayStoreResults.minAppVersion(results); - final releaseNotes = PlayStoreResults.releaseNotes(results); - final version = PlayStoreResults.version(results); + final description = playStore.description(results); + final minAppVersion = playStore.minAppVersion(results); + final releaseNotes = playStore.releaseNotes(results); + final version = playStore.version(results); print('playstore_lookup description: $description'); print('playstore_lookup minAppVersion: $minAppVersion'); diff --git a/lib/src/itunes_search_api.dart b/lib/src/itunes_search_api.dart index 9dee92cd..48881d37 100644 --- a/lib/src/itunes_search_api.dart +++ b/lib/src/itunes_search_api.dart @@ -21,7 +21,8 @@ class ITunesSearchAPI { /// Provide an HTTP Client that can be replaced for mock testing. http.Client? client = http.Client(); - bool debugEnabled = false; + /// Enable print statements for debugging. + bool debugLogging = false; /// Look up by bundle id. /// Example: look up Google Maps iOS App: @@ -36,20 +37,22 @@ class ITunesSearchAPI { final url = lookupURLByBundleId(bundleId, country: country ??= '', useCacheBuster: useCacheBuster)!; - if (debugEnabled) { + if (debugLogging) { print('upgrader: download: $url'); } try { final response = await client!.get(Uri.parse(url)); - if (debugEnabled) { + if (debugLogging) { print('upgrader: response statusCode: ${response.statusCode}'); } final decodedResults = _decodeResults(response.body); return decodedResults; } catch (e) { - print('upgrader: lookupByBundleId exception: $e'); + if (debugLogging) { + print('upgrader: lookupByBundleId exception: $e'); + } return null; } } @@ -66,7 +69,7 @@ class ITunesSearchAPI { final url = lookupURLById(id, country: country, useCacheBuster: useCacheBuster)!; - if (debugEnabled) { + if (debugLogging) { print('upgrader: download: $url'); } try { @@ -74,7 +77,9 @@ class ITunesSearchAPI { final decodedResults = _decodeResults(response.body); return decodedResults; } catch (e) { - print('upgrader: lookupById exception: $e'); + if (debugLogging) { + print('upgrader: lookupById exception: $e'); + } return null; } } @@ -131,7 +136,7 @@ class ITunesSearchAPI { if (decodedResults is Map) { final resultCount = decodedResults['resultCount']; if (resultCount == 0) { - if (debugEnabled) { + if (debugLogging) { print( 'upgrader.ITunesSearchAPI: results are empty: $decodedResults'); } @@ -143,36 +148,42 @@ class ITunesSearchAPI { } } -class ITunesResults { +extension ITunesResults on ITunesSearchAPI { /// Return field bundleId from iTunes results. - static String? bundleId(Map response) { + String? bundleId(Map response) { String? value; try { value = response['results'][0]['bundleId']; } catch (e) { - print('upgrader.ITunesResults.bundleId: $e'); + if (debugLogging) { + print('upgrader.ITunesResults.bundleId: $e'); + } } return value; } /// Return field currency from iTunes results. - static String? currency(Map response) { + String? currency(Map response) { String? value; try { value = response['results'][0]['currency']; } catch (e) { - print('upgrader.ITunesResults.currency: $e'); + if (debugLogging) { + print('upgrader.ITunesResults.currency: $e'); + } } return value; } /// Return field description from iTunes results. - static String? description(Map response) { + String? description(Map response) { String? value; try { value = response['results'][0]['description']; } catch (e) { - print('upgrader.ITunesResults.description: $e'); + if (debugLogging) { + print('upgrader.ITunesResults.description: $e'); + } } return value; } @@ -180,15 +191,15 @@ class ITunesResults { /// Return the minimum app version taken from the tag in the description field /// from the store response. The format is: [:mav: 1.2.3]. /// Returns version, such as 1.2.3, or null. - static Version? minAppVersion(Map response, {String tagName = 'mav'}) { + Version? minAppVersion(Map response, {String tagName = 'mav'}) { Version? version; try { - final description = ITunesResults.description(response); - if (description != null) { + final desc = description(response); + if (desc != null) { String regExpSource = r"\[\:tagName\:[\s]*(?[^\s]+)[\s]*\]"; regExpSource = regExpSource.replaceAll(RegExp('tagName'), tagName); final regExp = RegExp(regExpSource, caseSensitive: false); - final match = regExp.firstMatch(description); + final match = regExp.firstMatch(desc); final mav = match?.namedGroup('version'); if (mav != null) { @@ -196,45 +207,56 @@ class ITunesResults { // Verify version string using class Version version = Version.parse(mav); } on Exception catch (e) { - print('upgrader: ITunesResults.minAppVersion: $tagName error: $e'); + if (debugLogging) { + print( + 'upgrader: ITunesResults.minAppVersion: $tagName error: $e'); + } } } } } on Exception catch (e) { - print('upgrader.ITunesResults.minAppVersion : $e'); + if (debugLogging) { + print('upgrader.ITunesResults.minAppVersion : $e'); + } } return version; } /// Return field releaseNotes from iTunes results. - static String? releaseNotes(Map response) { + String? releaseNotes(Map response) { String? value; try { value = response['results'][0]['releaseNotes']; } catch (e) { - print('upgrader.ITunesResults.releaseNotes: $e'); + if (debugLogging) { + print('upgrader.ITunesResults.releaseNotes: $e'); + } } return value; } /// Return field trackViewUrl from iTunes results. - static String? trackViewUrl(Map response) { + String? trackViewUrl(Map response) { String? value; try { value = response['results'][0]['trackViewUrl']; } catch (e) { - print('upgrader.ITunesResults.trackViewUrl: $e'); + if (debugLogging) { + print('upgrader.ITunesResults.trackViewUrl: $e'); + } } return value; } /// Return field version from iTunes results. - static String? version(Map response) { + String? version(Map response) { String? value; try { value = response['results'][0]['version']; } catch (e) { - print('upgrader.ITunesResults.version: $e'); + if (debugLogging) { + print('upgrader.ITunesResults.version: $e'); + } } return value; } diff --git a/lib/src/play_store_search_api.dart b/lib/src/play_store_search_api.dart index 0eb1a8cd..f83e46d6 100644 --- a/lib/src/play_store_search_api.dart +++ b/lib/src/play_store_search_api.dart @@ -16,7 +16,8 @@ class PlayStoreSearchAPI { /// Provide an HTTP Client that can be replaced for mock testing. final http.Client? client; - bool debugEnabled = false; + /// Enable print statements for debugging. + bool debugLogging = false; /// Look up by id. Future lookupById(String id, @@ -28,15 +29,17 @@ class PlayStoreSearchAPI { final url = lookupURLById(id, country: country, language: language, useCacheBuster: useCacheBuster)!; - if (debugEnabled) { + if (debugLogging) { print('upgrader: lookupById url: $url'); } try { final response = await client!.get(Uri.parse(url)); if (response.statusCode < 200 || response.statusCode >= 300) { - print( - 'upgrader: Can\'t find an app in the Play Store with the id: $id'); + if (debugLogging) { + print( + 'upgrader: Can\'t find an app in the Play Store with the id: $id'); + } return null; } @@ -48,7 +51,9 @@ class PlayStoreSearchAPI { return decodedResults; } on Exception catch (e) { - print('upgrader: lookupById exception: $e'); + if (debugLogging) { + print('upgrader: lookupById exception: $e'); + } return null; } } @@ -85,11 +90,11 @@ class PlayStoreSearchAPI { } } -class PlayStoreResults { +extension PlayStoreResults on PlayStoreSearchAPI { static RegExp releaseNotesSpan = RegExp(r'>(.*?)'); /// Return field description from Play Store results. - static String? description(Document response) { + String? description(Document response) { try { final sectionElements = response.getElementsByClassName('W4P4ne'); final descriptionElement = sectionElements[0]; @@ -104,14 +109,16 @@ class PlayStoreResults { } /// Return field description from Redesigned Play Store results. - static String? redesignedDescription(Document response) { + String? redesignedDescription(Document response) { try { final sectionElements = response.getElementsByClassName('bARER'); final descriptionElement = sectionElements.last; final description = descriptionElement.text; return description; } catch (e) { - print('upgrader: PlayStoreResults.redesignedDescription exception: $e'); + if (debugLogging) { + print('upgrader: PlayStoreResults.redesignedDescription exception: $e'); + } } return null; } @@ -120,17 +127,17 @@ class PlayStoreResults { /// The [tagRegExpSource] is used to represent the format of a tag using a regular expression. /// The format in the description by default is like this: `[Minimum supported app version: 1.2.3]`, which /// returns the version `1.2.3`. If there is no match, it returns null. - static Version? minAppVersion( + Version? minAppVersion( Document response, { String tagRegExpSource = r'\[\Minimum supported app version\:[\s]*(?[^\s]+)[\s]*\]', }) { Version? version; try { - final description = PlayStoreResults.description(response); - if (description != null) { + final desc = description(response); + if (desc != null) { final regExp = RegExp(tagRegExpSource, caseSensitive: false); - final match = regExp.firstMatch(description); + final match = regExp.firstMatch(desc); final mav = match?.namedGroup('version'); if (mav != null) { @@ -138,20 +145,24 @@ class PlayStoreResults { // Verify version string using class Version version = Version.parse(mav); } on Exception catch (e) { - print( - 'upgrader: PlayStoreResults.minAppVersion: mav=$mav, tag=$tagRegExpSource, error=$e'); + if (debugLogging) { + print( + 'upgrader: PlayStoreResults.minAppVersion: mav=$mav, tag=$tagRegExpSource, error=$e'); + } } } } } on Exception catch (e) { - print('upgrader.PlayStoreResults.minAppVersion : $e'); + if (debugLogging) { + print('upgrader.PlayStoreResults.minAppVersion : $e'); + } } return version; } /// Returns field releaseNotes from Play Store results. When there are no /// release notes, the main app description is used. - static String? releaseNotes(Document response) { + String? releaseNotes(Document response) { try { final sectionElements = response.getElementsByClassName('W4P4ne'); final releaseNotesElement = sectionElements.firstWhere( @@ -173,7 +184,7 @@ class PlayStoreResults { /// Returns field releaseNotes from Redesigned Play Store results. When there are no /// release notes, the main app description is used. - static String? redesignedReleaseNotes(Document response) { + String? redesignedReleaseNotes(Document response) { try { final sectionElements = response.querySelectorAll('[itemprop="description"]'); @@ -182,12 +193,15 @@ class PlayStoreResults { final releaseNotes = multilineReleaseNotes(rawReleaseNotes); return releaseNotes; } catch (e) { - print('upgrader: PlayStoreResults.redesignedReleaseNotes exception: $e'); + if (debugLogging) { + print( + 'upgrader: PlayStoreResults.redesignedReleaseNotes exception: $e'); + } } return null; } - static String? multilineReleaseNotes(Element rawReleaseNotes) { + String? multilineReleaseNotes(Element rawReleaseNotes) { final innerHtml = rawReleaseNotes.innerHtml; String? releaseNotes = innerHtml; @@ -201,7 +215,7 @@ class PlayStoreResults { } /// Return field version from Play Store results. - static String? version(Document response) { + String? version(Document response) { String? version; try { final additionalInfoElements = response.getElementsByClassName('hAyfc'); @@ -219,7 +233,7 @@ class PlayStoreResults { } /// Return field version from Redesigned Play Store results. - static String? redesignedVersion(Document response) { + String? redesignedVersion(Document response) { String? version; try { const patternName = ",\"name\":\""; @@ -261,7 +275,9 @@ class PlayStoreResults { // storeVersion might be: 'Varies with device', which is not a valid version. version = Version.parse(storeVersion).toString(); } catch (e) { - print('upgrader: PlayStoreResults.redesignedVersion exception: $e'); + if (debugLogging) { + print('upgrader: PlayStoreResults.redesignedVersion exception: $e'); + } } return version; diff --git a/lib/src/upgrader.dart b/lib/src/upgrader.dart index 499c6816..707be92a 100644 --- a/lib/src/upgrader.dart +++ b/lib/src/upgrader.dart @@ -367,16 +367,16 @@ class Upgrader with WidgetsBindingObserver { await _getAndroidStoreVersion(country: country, language: language); } else if (upgraderOS.isIOS) { final iTunes = ITunesSearchAPI(); - iTunes.debugEnabled = debugLogging; + iTunes.debugLogging = debugLogging; iTunes.client = client; final response = await (iTunes .lookupByBundleId(_packageInfo!.packageName, country: country)); if (response != null) { - _appStoreVersion = ITunesResults.version(response); - _appStoreListingURL = ITunesResults.trackViewUrl(response); - _releaseNotes ??= ITunesResults.releaseNotes(response); - final mav = ITunesResults.minAppVersion(response); + _appStoreVersion = iTunes.version(response); + _appStoreListingURL = iTunes.trackViewUrl(response); + _releaseNotes ??= iTunes.releaseNotes(response); + final mav = iTunes.minAppVersion(response); if (mav != null) { minAppVersion = mav.toString(); if (debugLogging) { @@ -395,15 +395,15 @@ class Upgrader with WidgetsBindingObserver { {String? country, String? language}) async { final id = _packageInfo!.packageName; final playStore = PlayStoreSearchAPI(client: client); - playStore.debugEnabled = debugLogging; + playStore.debugLogging = debugLogging; final response = await (playStore.lookupById(id, country: country, language: language)); if (response != null) { - _appStoreVersion ??= PlayStoreResults.version(response); + _appStoreVersion ??= playStore.version(response); _appStoreListingURL ??= playStore.lookupURLById(id, language: language, country: country); - _releaseNotes ??= PlayStoreResults.releaseNotes(response); - final mav = PlayStoreResults.minAppVersion(response); + _releaseNotes ??= playStore.releaseNotes(response); + final mav = playStore.minAppVersion(response); if (mav != null) { minAppVersion = mav.toString(); if (debugLogging) { @@ -541,7 +541,9 @@ class Upgrader with WidgetsBindingObserver { final installedVersion = Version.parse(_installedVersion!); rv = installedVersion < minVersion; } catch (e) { - print(e); + if (debugLogging) { + print(e); + } } } return rv; @@ -587,7 +589,9 @@ class Upgrader with WidgetsBindingObserver { final available = appStoreVersion > installedVersion; _updateAvailable = available ? _appStoreVersion : null; } on Exception catch (e) { - print('upgrader: isUpdateAvailable: $e'); + if (debugLogging) { + print('upgrader: isUpdateAvailable: $e'); + } } final isAvailable = _updateAvailable != null; if (debugLogging) print('upgrader: isUpdateAvailable: $isAvailable'); diff --git a/test/itunes_test.dart b/test/itunes_test.dart index 94367d85..625b966e 100644 --- a/test/itunes_test.dart +++ b/test/itunes_test.dart @@ -10,9 +10,9 @@ import 'mock_itunes_client.dart'; void main() { test('testing ITunesSearchAPI properties', () async { final iTunes = ITunesSearchAPI(); - expect(iTunes.debugEnabled, equals(false)); - iTunes.debugEnabled = true; - expect(iTunes.debugEnabled, equals(true)); + expect(iTunes.debugLogging, equals(false)); + iTunes.debugLogging = true; + expect(iTunes.debugLogging, equals(true)); expect(iTunes.iTunesDocumentationURL.length, greaterThan(0)); expect(iTunes.lookupPrefixURL.length, greaterThan(0)); expect(iTunes.searchPrefixURL.length, greaterThan(0)); @@ -53,9 +53,9 @@ void main() { expect(result0, isNotNull); expect(result0['bundleId'], 'com.google.Maps'); expect(result0['version'], '5.6'); - expect(ITunesResults.bundleId(response), 'com.google.Maps'); - expect(ITunesResults.releaseNotes(response), 'Bug fixes.'); - expect(ITunesResults.version(response), '5.6'); + expect(iTunes.bundleId(response), 'com.google.Maps'); + expect(iTunes.releaseNotes(response), 'Bug fixes.'); + expect(iTunes.version(response), '5.6'); }, skip: false); test('testing lookupByBundleId unknown app', () async { @@ -88,10 +88,10 @@ void main() { expect(result0['releaseNotes'], 'Bug fixes.'); expect(result0['version'], '5.6'); expect(result0['currency'], 'USD'); - expect(ITunesResults.bundleId(response), 'com.google.Maps'); - expect(ITunesResults.releaseNotes(response), 'Bug fixes.'); - expect(ITunesResults.version(response), '5.6'); - expect(ITunesResults.currency(response), 'USD'); + expect(iTunes.bundleId(response), 'com.google.Maps'); + expect(iTunes.releaseNotes(response), 'Bug fixes.'); + expect(iTunes.version(response), '5.6'); + expect(iTunes.currency(response), 'USD'); }, skip: false); test('testing lookupById FR', () async { @@ -110,9 +110,9 @@ void main() { expect(result0['bundleId'], 'com.google.Maps'); expect(result0['version'], '5.6'); expect(result0['currency'], 'EUR'); - expect(ITunesResults.bundleId(response), 'com.google.Maps'); - expect(ITunesResults.version(response), '5.6'); - expect(ITunesResults.currency(response), 'EUR'); + expect(iTunes.bundleId(response), 'com.google.Maps'); + expect(iTunes.version(response), '5.6'); + expect(iTunes.currency(response), 'EUR'); }, skip: false); /// Helper method @@ -126,7 +126,7 @@ void main() { /// Helper method String? imav(Map response, {String tagName = 'mav'}) { - final mav = ITunesResults.minAppVersion(response, tagName: tagName); + final mav = ITunesSearchAPI().minAppVersion(response, tagName: tagName); return mav?.toString(); } diff --git a/test/play_store_test.dart b/test/play_store_test.dart index e6574130..4cb14924 100644 --- a/test/play_store_test.dart +++ b/test/play_store_test.dart @@ -12,7 +12,8 @@ import 'mock_play_store_client.dart'; /// Helper method String? pmav(Document response, {String tagRES = r'\[\:mav\:[\s]*(?[^\s]+)[\s]*\]'}) { - final mav = PlayStoreResults.minAppVersion(response, tagRegExpSource: tagRES); + final mav = + PlayStoreSearchAPI().minAppVersion(response, tagRegExpSource: tagRES); return mav?.toString(); } @@ -30,9 +31,9 @@ void main() { test('testing PlayStoreSearchAPI properties', () async { final playStore = PlayStoreSearchAPI(); - expect(playStore.debugEnabled, equals(false)); - playStore.debugEnabled = true; - expect(playStore.debugEnabled, equals(true)); + expect(playStore.debugLogging, equals(false)); + playStore.debugLogging = true; + expect(playStore.debugLogging, equals(true)); expect(playStore.playStorePrefixURL.length, greaterThan(0)); expect( @@ -50,9 +51,9 @@ void main() { expect(response, isNotNull); expect(response, isInstanceOf()); - expect(PlayStoreResults.releaseNotes(response!), - 'Minor updates and improvements.'); - expect(PlayStoreResults.version(response), '1.23.0'); + expect( + playStore.releaseNotes(response!), 'Minor updates and improvements.'); + expect(playStore.version(response), '1.23.0'); expect(await playStore.lookupById('com.not.a.valid.application'), isNull); @@ -109,10 +110,10 @@ void main() { expect(response, isNotNull); expect(response, isInstanceOf()); - expect(PlayStoreResults.releaseNotes(response!), - 'Minor updates and improvements.'); - expect(PlayStoreResults.version(response), '2.3.0'); - expect(PlayStoreResults.description(response)?.length, greaterThan(10)); + expect( + playStore.releaseNotes(response!), 'Minor updates and improvements.'); + expect(playStore.version(response), '2.3.0'); + expect(playStore.description(response)?.length, greaterThan(10)); expect( pmav(response, tagRES: @@ -130,10 +131,10 @@ void main() { expect(response, isNotNull); expect(response, isInstanceOf()); - expect(PlayStoreResults.releaseNotes(response!), - 'Minor updates and improvements.'); - expect(PlayStoreResults.version(response), '2.0.2'); - expect(PlayStoreResults.description(response)?.length, greaterThan(10)); + expect( + playStore.releaseNotes(response!), 'Minor updates and improvements.'); + expect(playStore.version(response), '2.0.2'); + expect(playStore.description(response)?.length, greaterThan(10)); }, skip: false); test('testing release notes
', () async { @@ -144,10 +145,10 @@ void main() { expect(response, isNotNull); expect(response, isInstanceOf()); - expect(PlayStoreResults.releaseNotes(response!), + expect(playStore.releaseNotes(response!), 'Minor updates and improvements.\nAgain.\nAgain.'); - expect(PlayStoreResults.version(response), '2.0.2'); - expect(PlayStoreResults.description(response)?.length, greaterThan(10)); + expect(playStore.version(response), '2.0.2'); + expect(playStore.description(response)?.length, greaterThan(10)); }, skip: false); test('testing release notes
2', () async { @@ -158,16 +159,10 @@ void main() { expect(response, isNotNull); expect(response, isInstanceOf()); - expect(PlayStoreResults.releaseNotes(response!), + expect(playStore.releaseNotes(response!), 'Minor updates and improvements.\nAgain.\nAgain.'); - expect(PlayStoreResults.version(response), '2.0.2'); - expect(PlayStoreResults.description(response)?.length, greaterThan(10)); - }, skip: false); - - test('testing PlayStoreResults', () async { - expect(PlayStoreResults(), isNotNull); - expect(PlayStoreResults.releaseNotes(Document()), isNull); - expect(PlayStoreResults.version(Document()), isNull); + expect(playStore.version(response), '2.0.2'); + expect(playStore.description(response)?.length, greaterThan(10)); }, skip: false); /// Helper method