Skip to content

Commit

Permalink
fix: 937 - removed home-made user agent header when for web (#950)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
monsieurtanuki authored Jul 19, 2024
1 parent 35269db commit b7da12b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ class OpenPricesAPIClient {
if (currency != null) 'currency': currency.name,
},
);
final List<int> fileBytes = await UriReader.instance!.readAsBytes(imageUri);
final List<int> fileBytes = await UriReader.instance.readAsBytes(imageUri);
final String filename = basename(imageUri.toString());
final http.MultipartFile multipartFile = http.MultipartFile.fromBytes(
'file',
Expand Down
6 changes: 3 additions & 3 deletions lib/src/utils/http_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ class HttpHelper {
// add all file entries to the request
if (files != null) {
for (MapEntry<String, Uri> entry in files.entries) {
List<int> fileBytes =
await UriReader.instance!.readAsBytes(entry.value);
List<int> fileBytes = await UriReader.instance.readAsBytes(entry.value);
var multipartFile = http.MultipartFile.fromBytes(entry.key, fileBytes,
filename: basename(entry.value.toString()));
request.files.add(multipartFile);
Expand Down Expand Up @@ -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,
),
Expand Down
12 changes: 9 additions & 3 deletions lib/src/utils/uri_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -32,4 +36,6 @@ abstract class UriReader {
}

Future<List<int>> readFileAsBytes(final Uri uri);

bool get isWeb => false;
}
3 changes: 3 additions & 0 deletions lib/src/utils/uri_reader_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ class UriReaderJs extends UriReader {
@override
Future<List<int>> readFileAsBytes(final Uri uri) async =>
throw Exception('Cannot read files in web version');

@override
bool get isWeb => true;
}
2 changes: 1 addition & 1 deletion test/api_add_product_image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void main() {

/// Returns the width and height (pixels) and size (bytes) of a JPEG URL file
Future<List<int>> 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)
Expand Down

0 comments on commit b7da12b

Please sign in to comment.