Skip to content

Commit

Permalink
feat: 978 - new prices features (thumbnails, stats)
Browse files Browse the repository at this point in the history
New file:
* `price_total_stats.dart`: Total stats for Prices.

Impacted files:
* `api_get_product_test.dart`: unrelated minor fix
* `api_prices_test.dart`: added test for the new `getStats` method; added test for thumbnails
* `location.dart`: added new count fields
* `location.g.dart`: generated
* `open_prices_api_client.dart`: new method `getStats`
* `openfoodfacts.dart`: exported new file `price_total_stats.dart`
* `price_product.dart`: added new count fields
* `price_product.g.dart`: generated
* `price_user.dart`: added new count fields
* `price_user.g.dart`: generated
* `proof.dart`: added thumbnail field and getter
* `proof.g.dart`: generated
* `proof_type.dart`: added new "SHOP_IMPORT" value
  • Loading branch information
monsieurtanuki committed Oct 4, 2024
1 parent 0d3f6fc commit 6ed58f8
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export 'src/prices/order_by.dart';
export 'src/prices/price.dart';
export 'src/prices/price_per.dart';
export 'src/prices/price_product.dart';
export 'src/prices/price_total_stats.dart';
export 'src/prices/price_user.dart';
export 'src/prices/proof.dart';
export 'src/prices/proof_type.dart';
Expand Down
26 changes: 26 additions & 0 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'prices/get_users_result.dart';
import 'prices/location.dart';
import 'prices/location_osm_type.dart';
import 'prices/price_product.dart';
import 'prices/price_total_stats.dart';
import 'prices/proof_type.dart';
import 'prices/session.dart';
import 'prices/update_price_parameters.dart';
Expand Down Expand Up @@ -612,4 +613,29 @@ class OpenPricesAPIClient {
}
return MaybeError<GetUsersResult>.responseError(response);
}

/// Returns the total stats.
static Future<MaybeError<PriceTotalStats>> getStats({
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = getUri(
path: '/api/v1/stats',
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
uriHelper: uriHelper,
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<PriceTotalStats>.value(
PriceTotalStats.fromJson(decodedResponse),
);
} catch (e) {
//
}
}
return MaybeError<PriceTotalStats>.responseError(response);
}
}
18 changes: 15 additions & 3 deletions lib/src/prices/location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part 'location.g.dart';

/// Location object in the Prices API.
///
/// cf. `LocationBase` in https://prices.openfoodfacts.net/docs
/// cf. `Location` in https://prices.openfoodfacts.org/api/docs
@JsonSerializable()
class Location extends JsonObject {
/// ID of the location in OpenStreetMap: the store where the product was bought.
Expand All @@ -25,7 +25,19 @@ class Location extends JsonObject {

/// Number of prices for this location.
@JsonKey(name: 'price_count')
late int priceCount;
int? priceCount;

/// Number of users for this location.
@JsonKey(name: 'user_count')
int? userCount;

/// Number of products for this location.
@JsonKey(name: 'product_count')
int? productCount;

/// Number of proofs for this location.
@JsonKey(name: 'proof_count')
int? proofCount;

/// ID in the Prices API.
@JsonKey(name: 'id')
Expand Down Expand Up @@ -65,7 +77,7 @@ class Location extends JsonObject {
@JsonKey(fromJson: JsonHelper.stringTimestampToDate)
late DateTime created;

/// Date when the product was bought.
/// Latest update timestamp.
@JsonKey(fromJson: JsonHelper.nullableStringTimestampToDate)
DateTime? updated;

Expand Down
8 changes: 7 additions & 1 deletion lib/src/prices/location.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions lib/src/prices/price_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part 'price_product.g.dart';

/// Product object in the Prices API.
///
/// cf. `ProductFull` in https://prices.openfoodfacts.net/docs
/// cf. `ProductFull` in https://prices.openfoodfacts.org/api/docs
@JsonSerializable()
class PriceProduct extends JsonObject {
/// Barcode (EAN) of the product, as a string.
Expand All @@ -17,7 +17,19 @@ class PriceProduct extends JsonObject {

/// Number of prices for this product.
@JsonKey(name: 'price_count')
late int priceCount;
int? priceCount;

/// Number of locations for this product.
@JsonKey(name: 'location_count')
int? locationCount;

/// Number of users for this product.
@JsonKey(name: 'user_count')
int? userCount;

/// Number of proofs for this product.
@JsonKey(name: 'proof_count')
int? proofCount;

@JsonKey(name: 'id')
late int productId;
Expand Down
8 changes: 7 additions & 1 deletion lib/src/prices/price_product.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions lib/src/prices/price_total_stats.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:json_annotation/json_annotation.dart';

import '../interface/json_object.dart';
import '../utils/json_helper.dart';

part 'price_total_stats.g.dart';

/// Total stats for Prices.
///
/// cf. `TotalStats` in https://prices.openfoodfacts.org/api/docs
@JsonSerializable()
class PriceTotalStats extends JsonObject {
@JsonKey(name: 'price_count')
int? priceCount;

@JsonKey(name: 'price_type_product_code_count')
int? priceTypeProductCodeCount;

@JsonKey(name: 'price_type_category_tag_count')
int? priceTypeCategoryTagCount;

@JsonKey(name: 'product_count')
int? productCount;

@JsonKey(name: 'product_with_price_count')
int? productWithPriceCount;

@JsonKey(name: 'location_count')
int? locationCount;

@JsonKey(name: 'location_with_price_count')
int? locationWithPriceCount;

@JsonKey(name: 'proof_count')
int? proofCount;

@JsonKey(name: 'proof_with_price_count')
int? proofWithPriceCount;

@JsonKey(name: 'proof_type_price_tag_count')
int? proofTypePriceTagCount;

@JsonKey(name: 'proof_type_receipt_count')
int? proofTypeReceiptCount;

@JsonKey(name: 'user_count')
int? userCount;

@JsonKey(name: 'user_with_price_count')
int? userWithPriceCount;

@JsonKey(fromJson: JsonHelper.nullableStringTimestampToDate)
DateTime? updated;

PriceTotalStats();

factory PriceTotalStats.fromJson(Map<String, dynamic> json) =>
_$PriceTotalStatsFromJson(json);

@override
Map<String, dynamic> toJson() => _$PriceTotalStatsToJson(this);
}
18 changes: 13 additions & 5 deletions lib/src/prices/price_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ part 'price_user.g.dart';

/// Price user object.
///
/// cf. `UserBase` in https://prices.openfoodfacts.org/api/docs
/// cf. `User` in https://prices.openfoodfacts.org/api/docs
@JsonSerializable()
class PriceUser extends JsonObject {
@JsonKey(name: 'user_id')
late String userId;

/// Number of prices for this user.
@JsonKey(name: 'price_count')
late int priceCount;
int? priceCount;

/// Number of prices for this user.
@JsonKey(name: 'is_moderator')
bool? isModerator;
/// Number of locations for this user.
@JsonKey(name: 'location_count')
int? locationCount;

/// Number of products for this user.
@JsonKey(name: 'product_count')
int? productCount;

/// Number of proofs for this user.
@JsonKey(name: 'proof_count')
int? proofCount;

PriceUser();

Expand Down
10 changes: 7 additions & 3 deletions lib/src/prices/price_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions lib/src/prices/proof.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Proof extends JsonObject {
@JsonKey(name: 'file_path')
String? filePath;

/// Image thumb file path. Read-only.
@JsonKey(name: 'image_thumb_path')
String? imageThumbPath;

/// Mime type. Read-only.
@JsonKey()
late String mimetype;
Expand Down Expand Up @@ -89,12 +93,25 @@ class Proof extends JsonObject {
@override
Map<String, dynamic> toJson() => _$ProofToJson(this);

/// Returns the URL of the proof image.
Uri? getFileUrl({required final UriProductHelper uriProductHelper}) =>
filePath == null
/// Returns the URL of the proof image, thumbnail or full.
Uri? getFileUrl({
required final UriProductHelper uriProductHelper,
final bool isThumbnail = true,
}) =>
_getFileUrl(
uriProductHelper: uriProductHelper,
path: isThumbnail ? imageThumbPath : filePath,
);

/// Returns the URL of a proof image.
static Uri? _getFileUrl({
required final UriProductHelper uriProductHelper,
required String? path,
}) =>
path == null
? null
: OpenPricesAPIClient.getUri(
path: 'img/$filePath',
path: 'img/$path',
uriHelper: uriProductHelper,
addUserAgentParameters: false,
);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/prices/proof.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/src/prices/proof_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ enum ProofType implements OffTagged {
receipt(offTag: 'RECEIPT'),

@JsonValue('GDPR_REQUEST')
gdprRequest(offTag: 'GDPR_REQUEST');
gdprRequest(offTag: 'GDPR_REQUEST'),

@JsonValue('SHOP_IMPORT')
shopImport(offTag: 'SHOP_IMPORT');

const ProofType({
required this.offTag,
Expand Down
2 changes: 0 additions & 2 deletions test/api_get_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ void main() {
expect(nutriments.getValue(Nutrient.proteins, perSize), isNotNull);
expect(nutriments.getValue(Nutrient.salt, perSize), isNotNull);
expect(nutriments.getValue(Nutrient.fat, perSize), isNotNull);

expect(result.product!.countries, 'United States');
});

test('check alcohol data', () async {
Expand Down
Loading

0 comments on commit 6ed58f8

Please sign in to comment.