Skip to content

Commit 53b2c42

Browse files
feat: 1066 - new parameters for proof upload (#1071)
* feat: 1066 - new parameters for proof upload New files: * `common_proof_parameters.dart`: Common parameters for the "upload and update proof" API queries, including 5 new fields. * `create_proof_parameters.dart`: Parameters for the "upload proof" API query. Impacted files: * `api_prices_test.dart`: added more tests around proof upload and update; refactored accordingly * `open_prices_api_client.dart`: changed the parameters of `uploadProof` for more flexibility * `openfoodfacts.dart`: added the 2 new files * `proof.dart`: added 4 missing fields * `proof.g.dart`: generated * `update_proof_parameters.dart`: moved code to `CommonProofParameters` * typo fix
1 parent cac6076 commit 53b2c42

8 files changed

+328
-176
lines changed

lib/openfoodfacts.dart

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export 'src/prices/get_locations_result.dart';
106106

107107
// export 'src/prices/get_parameters_helper.dart'; // uncomment if really needed
108108
export 'src/prices/contribution_kind.dart';
109+
export 'src/prices/common_proof_parameters.dart';
110+
export 'src/prices/create_proof_parameters.dart';
109111
export 'src/prices/get_prices_order.dart';
110112
export 'src/prices/get_prices_parameters.dart';
111113
export 'src/prices/get_price_products_order.dart';

lib/src/open_prices_api_client.dart

+29-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import 'prices/proof_type.dart';
2828
import 'prices/session.dart';
2929
import 'prices/update_price_parameters.dart';
3030
import 'prices/update_proof_parameters.dart';
31+
import 'prices/create_proof_parameters.dart';
3132
import 'utils/http_helper.dart';
3233
import 'utils/open_food_api_configuration.dart';
3334
import 'utils/uri_helper.dart';
@@ -512,15 +513,20 @@ class OpenPricesAPIClient {
512513
return MaybeError<GetProofsResult>.responseError(response);
513514
}
514515

516+
// TODO: deprecated from 2025-04-25 regarding single parameters; remove them when old enough
515517
static Future<MaybeError<Proof>> uploadProof({
516-
required final ProofType proofType,
518+
@Deprecated('Use CreateProofParameters instead') final ProofType? proofType,
517519
required final Uri imageUri,
518520
required final MediaType mediaType,
519-
final int? locationOSMId,
521+
final CreateProofParameters? createProofParameters,
522+
@Deprecated('Use CreateProofParameters instead') final int? locationOSMId,
523+
@Deprecated('Use CreateProofParameters instead')
520524
final LocationOSMType? locationOSMType,
521-
final DateTime? date,
522-
final Currency? currency,
525+
@Deprecated('Use CreateProofParameters instead') final DateTime? date,
526+
@Deprecated('Use CreateProofParameters instead') final Currency? currency,
527+
@Deprecated('Use CreateProofParameters instead')
523528
final int? receiptPriceCount,
529+
@Deprecated('Use CreateProofParameters instead')
524530
final num? receiptPriceTotal,
525531
required final String bearerToken,
526532
final UriProductHelper uriHelper = uriHelperFoodProd,
@@ -535,20 +541,25 @@ class OpenPricesAPIClient {
535541
'Authorization': 'bearer $bearerToken',
536542
'Content-Type': 'multipart/form-data',
537543
});
538-
request.fields.addAll(
539-
<String, String>{
540-
'type': proofType.offTag,
541-
if (locationOSMId != null) 'location_osm_id': locationOSMId.toString(),
542-
if (locationOSMType != null)
543-
'location_osm_type': locationOSMType.offTag,
544-
if (date != null) 'date': GetParametersHelper.formatDate(date),
545-
if (currency != null) 'currency': currency.name,
546-
if (receiptPriceCount != null)
547-
'receipt_price_count': receiptPriceCount.toString(),
548-
if (receiptPriceTotal != null)
549-
'receipt_price_total': receiptPriceTotal.toString(),
550-
},
551-
);
544+
if (createProofParameters != null) {
545+
request.fields.addAll(createProofParameters.toData());
546+
} else {
547+
request.fields.addAll(
548+
<String, String>{
549+
'type': proofType!.offTag,
550+
if (locationOSMId != null)
551+
'location_osm_id': locationOSMId.toString(),
552+
if (locationOSMType != null)
553+
'location_osm_type': locationOSMType.offTag,
554+
if (date != null) 'date': GetParametersHelper.formatDate(date),
555+
if (currency != null) 'currency': currency.name,
556+
if (receiptPriceCount != null)
557+
'receipt_price_count': receiptPriceCount.toString(),
558+
if (receiptPriceTotal != null)
559+
'receipt_price_total': receiptPriceTotal.toString(),
560+
},
561+
);
562+
}
552563
final List<int> fileBytes = await UriReader.instance.readAsBytes(imageUri);
553564
final String filename = basename(imageUri.toString());
554565
final http.MultipartFile multipartFile = http.MultipartFile.fromBytes(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import '../interface/json_object.dart';
2+
import 'currency.dart';
3+
import 'get_parameters_helper.dart';
4+
import 'location_osm_type.dart';
5+
import 'proof_type.dart';
6+
7+
/// Common parameters for the "upload and update proof" API queries.
8+
///
9+
/// cf. https://prices.openfoodfacts.org/api/docs
10+
abstract class CommonProofParameters extends JsonObject {
11+
/// Proof type.
12+
ProofType? get type;
13+
14+
/// Date when the product was bought.
15+
DateTime? date;
16+
17+
/// Currency of the price.
18+
Currency? currency;
19+
20+
/// ID of the location in OpenStreetMap.
21+
int? locationOSMId;
22+
23+
/// Type of the OpenStreetMap location object.
24+
LocationOSMType? locationOSMType;
25+
26+
/// Receipt's number of prices.
27+
int? receiptPriceCount;
28+
29+
/// Receipt's total amount (user input).
30+
num? receiptPriceTotal;
31+
32+
num? receiptOnlineDeliveryCosts;
33+
34+
bool? readyForPriceTagValidation;
35+
36+
bool? ownerConsumption;
37+
38+
String? ownerComment;
39+
40+
int? locationId;
41+
42+
@override
43+
Map<String, dynamic> toJson() => <String, dynamic>{
44+
if (type != null) 'type': type!.offTag,
45+
if (date != null) 'date': GetParametersHelper.formatDate(date!),
46+
if (currency != null) 'currency': currency!.name,
47+
if (locationOSMId != null) 'location_osm_id': locationOSMId,
48+
if (locationOSMType != null)
49+
'location_osm_type': locationOSMType!.offTag,
50+
if (receiptPriceCount != null) 'receipt_price_count': receiptPriceCount,
51+
if (receiptPriceTotal != null) 'receipt_price_total': receiptPriceTotal,
52+
if (receiptOnlineDeliveryCosts != null)
53+
'receipt_online_delivery_costs': receiptOnlineDeliveryCosts,
54+
if (readyForPriceTagValidation != null)
55+
'ready_for_price_tag_validation': readyForPriceTagValidation,
56+
if (ownerConsumption != null) 'owner_consumption': ownerConsumption,
57+
if (ownerComment != null) 'owner_comment': ownerComment,
58+
if (locationId != null) 'location_id': locationId,
59+
};
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'common_proof_parameters.dart';
2+
import 'proof_type.dart';
3+
4+
/// Parameters for the "upload proof" API query.
5+
///
6+
/// cf. https://prices.openfoodfacts.org/api/docs
7+
class CreateProofParameters extends CommonProofParameters {
8+
CreateProofParameters(this.type);
9+
10+
@override
11+
final ProofType type;
12+
}

lib/src/prices/proof.dart

+12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ class Proof extends JsonObject {
9494
@JsonKey()
9595
Location? location;
9696

97+
@JsonKey(name: 'receipt_online_delivery_costs')
98+
num? receiptOnlineDeliveryCosts;
99+
100+
@JsonKey(name: 'ready_for_price_tag_validation')
101+
bool? readyForPriceTagValidation;
102+
103+
@JsonKey(name: 'owner_consumption')
104+
bool? ownerConsumption;
105+
106+
@JsonKey(name: 'owner_comment')
107+
String? ownerComment;
108+
97109
Proof();
98110

99111
factory Proof.fromJson(Map<String, dynamic> json) => _$ProofFromJson(json);

lib/src/prices/proof.g.dart

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+3-36
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,10 @@
1-
import '../interface/json_object.dart';
2-
import 'currency.dart';
3-
import 'get_parameters_helper.dart';
4-
import 'location_osm_type.dart';
1+
import 'common_proof_parameters.dart';
52
import 'proof_type.dart';
63

74
/// Parameters for the "update proof" API query.
85
///
96
/// cf. https://prices.openfoodfacts.org/api/docs
10-
class UpdateProofParameters extends JsonObject {
11-
/// Proof type.
12-
ProofType? type;
13-
14-
/// Date when the product was bought.
15-
DateTime? date;
16-
17-
/// Currency of the price.
18-
Currency? currency;
19-
20-
/// ID of the location in OpenStreetMap.
21-
int? locationOSMId;
22-
23-
/// Type of the OpenStreetMap location object.
24-
LocationOSMType? locationOSMType;
25-
26-
/// Receipt's number of prices.
27-
int? receiptPriceCount;
28-
29-
/// Receipt's total amount (user input).
30-
num? receiptPriceTotal;
31-
7+
class UpdateProofParameters extends CommonProofParameters {
328
@override
33-
Map<String, dynamic> toJson() => <String, dynamic>{
34-
if (type != null) 'type': type!.offTag,
35-
if (date != null) 'date': GetParametersHelper.formatDate(date!),
36-
if (currency != null) 'currency': currency!.name,
37-
if (locationOSMId != null) 'location_osm_id': locationOSMId,
38-
if (locationOSMType != null)
39-
'location_osm_type': locationOSMType!.offTag,
40-
if (receiptPriceCount != null) 'receipt_price_count': receiptPriceCount,
41-
if (receiptPriceTotal != null) 'receipt_price_total': receiptPriceTotal,
42-
};
9+
ProofType? type;
4310
}

0 commit comments

Comments
 (0)