Skip to content

Commit

Permalink
feat: openfoodfacts#306 - unselect product image
Browse files Browse the repository at this point in the history
Impacted files:
* `api_addProductImage_test.dart`: new test about "unselect product image"
* `openfoodfacts.dart`: new method `unselectProductImage`
* `ProductImage.dart`: minor unrelated refactoring
  • Loading branch information
monsieurtanuki committed Dec 14, 2021
1 parent 8b6c9bc commit ea91655
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/model/ProductImage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class ProductImage {
String toString() =>
'ProductImage('
'${field.value}' +
(size == null ? '' : ',size=${size.value}]') +
(size == null ? '' : ',size=${size.value}') +
(language == null ? '' : ',language=${language.code}') +
(angle == null ? '' : ',angle=${angle!.degreesClockwise}') +
(url == null ? '' : ',url=$url') +
Expand Down
42 changes: 42 additions & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1022,4 +1022,46 @@ class OpenFoodAPIClient {
'/' +
filename;
}

/// Unselect a product image.
///
/// Typically, after that the openfoodfacts web page will _not_ show
/// the image as selected for this product x imagefield x language anymore.
/// Throws an exception if not successful.
/// Will work OK even when there was no previous selected product image.
static Future<void> unselectProductImage({
required final String barcode,
required final ImageField imageField,
required final OpenFoodFactsLanguage language,
final QueryType? queryType,
}) async {
final String id = '${imageField.value}_${language.code}';
final Uri uri = UriHelper.getUri(
path: 'cgi/product_image_unselect.pl',
queryType: queryType,
queryParameters: <String, String>{'code': barcode, 'id': id},
);

final Response response = await HttpHelper()
.doGetRequest(uri, userAgent: OpenFoodAPIConfiguration.userAgent);
if (response.statusCode != 200) {
throw Exception(
'Bad response (${response.statusCode}): ${response.body}');
}
final Map<String, dynamic> json =
jsonDecode(response.body) as Map<String, dynamic>;
final String status = json['status'];
if (status != 'status ok') {
throw Exception('Status not ok ($status)');
}
final int statusCode = json['status_code'];
if (statusCode != 0) {
throw Exception('Status Code not ok ($statusCode)');
}
final String imagefield = json['imagefield'];
if (imagefield != id) {
throw Exception(
'Different imagefield: expected "$id", actual "$imageField"');
}
}
}
28 changes: 28 additions & 0 deletions test/api_addProductImage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,33 @@ void main() {
// this guy is rather slow
Duration(seconds: 90),
));

test('image unselect', () async {
const ImageField unselectedImageField = ImageField.INGREDIENTS;
await OpenFoodAPIClient.unselectProductImage(
barcode: barcode,
imageField: unselectedImageField,
language: language,
);

final ProductResult productResult = await OpenFoodAPIClient.getProduct(
ProductQueryConfiguration(
barcode,
fields: <ProductField>[ProductField.SELECTED_IMAGE],
),
);
expect(productResult.product, isNotNull);
expect(productResult.product!.selectedImages, isNotNull);
for (final ProductImage productImage
in productResult.product!.selectedImages!) {
if (productImage.language == language) {
expect(productImage.field, isNot(unselectedImageField));
}
}
},
timeout: Timeout(
// this guy is rather slow
Duration(seconds: 90),
));
});
}

0 comments on commit ea91655

Please sign in to comment.