From b7da12b58d936b3778465c5a31887bda41e2084b Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Fri, 19 Jul 2024 13:25:54 +0200 Subject: [PATCH] fix: 937 - removed home-made user agent header when for web (#950) Impacted files: * `api_add_product_image_test.dart`: minor refactoring * `http_helper.dart`: removed home-made user agent header when for web; minor refactoring * `open_prices_api_client.dart`: minor refactoring * `uri_reader.dart`: added an `isWeb` getter; minor refactoring * `uri_reader_js.dart`: implemented the `isWeb` getter --- lib/src/open_prices_api_client.dart | 2 +- lib/src/utils/http_helper.dart | 6 +++--- lib/src/utils/uri_reader.dart | 12 +++++++++--- lib/src/utils/uri_reader_js.dart | 3 +++ test/api_add_product_image_test.dart | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/src/open_prices_api_client.dart b/lib/src/open_prices_api_client.dart index 53e9314169..f6f87ca87b 100644 --- a/lib/src/open_prices_api_client.dart +++ b/lib/src/open_prices_api_client.dart @@ -481,7 +481,7 @@ class OpenPricesAPIClient { if (currency != null) 'currency': currency.name, }, ); - final List fileBytes = await UriReader.instance!.readAsBytes(imageUri); + final List fileBytes = await UriReader.instance.readAsBytes(imageUri); final String filename = basename(imageUri.toString()); final http.MultipartFile multipartFile = http.MultipartFile.fromBytes( 'file', diff --git a/lib/src/utils/http_helper.dart b/lib/src/utils/http_helper.dart index 4a46e302d4..fb567028b4 100644 --- a/lib/src/utils/http_helper.dart +++ b/lib/src/utils/http_helper.dart @@ -204,8 +204,7 @@ class HttpHelper { // add all file entries to the request if (files != null) { for (MapEntry entry in files.entries) { - List fileBytes = - await UriReader.instance!.readAsBytes(entry.value); + List fileBytes = await UriReader.instance.readAsBytes(entry.value); var multipartFile = http.MultipartFile.fromBytes(entry.key, fileBytes, filename: basename(entry.value.toString())); request.files.add(multipartFile); @@ -273,7 +272,8 @@ class HttpHelper { headers.addAll({ 'Accept': 'application/json', - 'User-Agent': OpenFoodAPIConfiguration.userAgent!.toValueString(), + if (!UriReader.instance.isWeb) + 'User-Agent': OpenFoodAPIConfiguration.userAgent!.toValueString(), 'From': _getSafeString( OpenFoodAPIConfiguration.getUser(user)?.userId ?? FROM, ), diff --git a/lib/src/utils/uri_reader.dart b/lib/src/utils/uri_reader.dart index 689a373f13..9ddce198eb 100644 --- a/lib/src/utils/uri_reader.dart +++ b/lib/src/utils/uri_reader.dart @@ -7,10 +7,14 @@ import 'uri_reader_stub.dart' /// Abstract reader of URI data, declined in "not web" and "web" versions abstract class UriReader { - static UriReader? _instance; + static late final UriReader _instance; + static bool _initialized = false; - static UriReader? get instance { - _instance ??= getUriReaderInstance(); + static UriReader get instance { + if (!_initialized) { + _initialized = true; + _instance = getUriReaderInstance(); + } return _instance; } @@ -32,4 +36,6 @@ abstract class UriReader { } Future> readFileAsBytes(final Uri uri); + + bool get isWeb => false; } diff --git a/lib/src/utils/uri_reader_js.dart b/lib/src/utils/uri_reader_js.dart index 7aad4946e3..753634ecc1 100644 --- a/lib/src/utils/uri_reader_js.dart +++ b/lib/src/utils/uri_reader_js.dart @@ -7,4 +7,7 @@ class UriReaderJs extends UriReader { @override Future> readFileAsBytes(final Uri uri) async => throw Exception('Cannot read files in web version'); + + @override + bool get isWeb => true; } diff --git a/test/api_add_product_image_test.dart b/test/api_add_product_image_test.dart index 365b4d34e1..5d75c2203c 100644 --- a/test/api_add_product_image_test.dart +++ b/test/api_add_product_image_test.dart @@ -49,7 +49,7 @@ void main() { /// Returns the width and height (pixels) and size (bytes) of a JPEG URL file Future> getJpegUrlSize(final String url) async => getJpegSize( - await UriReader.instance!.readAsBytes(Uri.parse(url)), + await UriReader.instance.readAsBytes(Uri.parse(url)), ); /// Returns the imgid, i.e. the unique id for (uploaded image x product)