Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #306 - unselect product image #319

Merged
merged 2 commits into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/model/ProductImage.dart
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a comma after the user agent it is probably a bit more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I've just committed a refactoring.
It made me realize that it would probably be better to systematically include a User user parameter to methods where we impact the server database (not just read it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@M123-dev I have added a mandatory User user parameter in my recent setProductImageAngle and setProductImageCrop methods: does that qualify for a breaking change and the corresponding version upgrade? That would be a bit too generous.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think we need a breaking change here, the methods are only public for a few days anyway. And besides, I think it's more for when we make deprecate some things. So for things where the "code" breaks when upgrading. Even though the dart @deprecated doesn't actually make that so important.

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
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),
));
});
}