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: #2248 - user agent distinction between scan and barcode search #2408

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class ContinuousScanModel with ChangeNotifier {
BarcodeProductQuery(
barcode: barcode,
daoProduct: _daoProduct,
isScanned: true,
).getFetchedProduct();

Future<void> _loadBarcode(
Expand Down
5 changes: 5 additions & 0 deletions packages/smooth_app/lib/database/barcode_product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class BarcodeProductQuery {
BarcodeProductQuery({
required this.barcode,
required this.daoProduct,
required this.isScanned,
});

final String barcode;
final DaoProduct daoProduct;
final bool isScanned;

Future<FetchedProduct> getFetchedProduct() async {
final ProductQueryConfiguration configuration = ProductQueryConfiguration(
Expand All @@ -24,10 +26,13 @@ class BarcodeProductQuery {

final ProductResult result;
try {
ProductQuery.setUserAgentComment(isScanned ? 'scan' : 'search');
result = await OpenFoodAPIClient.getProduct(configuration);
} catch (e) {
ProductQuery.setUserAgentComment('');
return FetchedProduct.error(FetchedProductStatus.internetError);
}
ProductQuery.setUserAgentComment('');

if (result.status == 1) {
final Product? product = result.product;
Expand Down
18 changes: 18 additions & 0 deletions packages/smooth_app/lib/database/product_query.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:openfoodfacts/model/UserAgent.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
Expand Down Expand Up @@ -42,6 +43,23 @@ abstract class ProductQuery {
'_'
'${getCountry()!.iso2Code.toUpperCase()}';

/// Sets a comment for the user agent.
///
/// cf. https://github.com/openfoodfacts/smooth-app/issues/2248
static void setUserAgentComment(final String comment) {
final UserAgent? previous = OpenFoodAPIConfiguration.userAgent;
if (previous == null) {
return;
}
OpenFoodAPIConfiguration.userAgent = UserAgent(
name: previous.name,
version: previous.version,
system: previous.system,
url: previous.url,
comment: comment,
);
}

static const String _UUID_NAME = 'UUID_NAME_REV_1';

/// Sets the uuid id as "final variable", for instance for API queries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ProductDialogHelper {
future: BarcodeProductQuery(
barcode: barcode,
daoProduct: DaoProduct(localDatabase),
isScanned: false,
).getFetchedProduct(),
title: refresh
? AppLocalizations.of(context).refreshing_product
Expand Down