Skip to content

Commit

Permalink
feat: 831 - prices methods getProofs, getProof and getUsers + fixed u…
Browse files Browse the repository at this point in the history
…ploadProof (openfoodfacts#926)

* feat: 831 - prices methods getProofs, getProof and getUsers + fixed uploadProof

New files:
* `flavor.dart`: Flavor is used to refer to a specific Open*Facts project.
* `get_price_count_parameters_helper.dart`: Helper class for API query parameters with price count filters.
* `get_proofs_order.dart`: Field for the "order by" clause of "get proofs".
* `get_proofs_parameters.dart`: Parameters for the "get proofs" API query.
* `get_proofs_result.dart`: List of proof objects returned by the "get proofs" method.
* `get_proofs_result.g.dart`: generated
* `get_users_order.dart`: Field for the "order by" clause of "get users".
* `get_users_parameters.dart`: Parameters for the "get users" API query.
* `get_users_result.dart`: List of price user objects returned by the "get users" method.
* `get_users_result.g.dart`: generated
* `price_user.dart`: Price user object.
* `price_user.g.dart`: generated

Impacted files:
* `api_prices_test.dart`: added tests for new methods `getProofs`, `getProof` and `getUsers`; fixed test of method `uploadProof`
* `badge_base.g.dart`: unrelated file generation change
* `events_base.g.dart`: unrelated file generation change
* `get_locations_parameters.dart`: refactoring
* `get_locations_result.g.dart`: unrelated file generation change
* `get_parameters_helper.dart`: refactoring
* `get_prices_parameters.dart`: refactoring
* `get_prices_result.g.dart`: unrelated file generation change
* `key_stats.g.dart`: unrelated file generation change
* `knowledge_panel_element.g.dart`: unrelated file generation change
* `leaderboard_entry.g.dart`: unrelated file generation change
* `location.dart`: new field `priceCount`
* `location.g.dart`: generated
* `ocr_ingredients_result.g.dart`: unrelated file generation change
* `ocr_packaging_result.g.dart`: unrelated file generation change
* `old_product_result.g.dart`: unrelated file generation change
* `open_prices_api_client.dart`: new methods `getProofs`, `getProof` and `getUsers`; fixed method `uploadProof`
* `openfoodfacts.dart`: exported the new files
* `price.dart`: new fields `proof`, `location` and `product`
* `price.g.dart`: generated
* `price_product.dart`: new fields
* `price_product.g.dart`: generated
* `product.g.dart`: unrelated file generation change
* `product_stats.g.dart`: unrelated file generation change
* `product_tag.g.dart`: unrelated file generation change
* `proof.dart`: removed old field (that provoked test failure anyway)
* `proof.g.dart`: generated
* `pubspec.yaml`: upgraded packages to more recent versions - while not too recent either
* `spelling_corrections.g.dart`: unrelated file generation change

* Unrelated unit test fixes
  • Loading branch information
monsieurtanuki authored May 17, 2024
1 parent 9d17073 commit baa1258
Show file tree
Hide file tree
Showing 48 changed files with 728 additions and 123 deletions.
9 changes: 9 additions & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,29 @@ export 'src/personalized_search/preference_importance.dart';
export 'src/personalized_search/product_preferences_manager.dart';
export 'src/personalized_search/product_preferences_selection.dart';
export 'src/prices/currency.dart';
export 'src/prices/flavor.dart';
export 'src/prices/get_locations_order.dart';
export 'src/prices/get_locations_parameters.dart';
export 'src/prices/get_locations_result.dart';
// export 'src/prices/get_parameters_helper.dart'; // uncomment if really needed
export 'src/prices/get_prices_order.dart';
export 'src/prices/get_prices_parameters.dart';
export 'src/prices/get_prices_result.dart';
export 'src/prices/get_price_count_parameters_helper.dart';
export 'src/prices/get_proofs_order.dart';
export 'src/prices/get_proofs_parameters.dart';
export 'src/prices/get_proofs_result.dart';
export 'src/prices/get_users_order.dart';
export 'src/prices/get_users_parameters.dart';
export 'src/prices/get_users_result.dart';
export 'src/prices/location.dart';
export 'src/prices/location_osm_type.dart';
export 'src/prices/maybe_error.dart';
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_user.dart';
export 'src/prices/proof.dart';
export 'src/prices/proof_type.dart';
export 'src/prices/session.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/badge_base.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/model/events_base.g.dart

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

4 changes: 2 additions & 2 deletions lib/src/model/key_stats.g.dart

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

4 changes: 2 additions & 2 deletions lib/src/model/knowledge_panel_element.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/model/leaderboard_entry.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/model/ocr_ingredients_result.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/model/ocr_packaging_result.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/model/old_product_result.g.dart

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

5 changes: 3 additions & 2 deletions lib/src/model/product.g.dart

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

4 changes: 2 additions & 2 deletions lib/src/model/product_stats.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/model/product_tag.g.dart

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

4 changes: 2 additions & 2 deletions lib/src/model/spelling_corrections.g.dart

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

89 changes: 87 additions & 2 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import 'prices/get_locations_result.dart';
import 'prices/get_parameters_helper.dart';
import 'prices/get_prices_parameters.dart';
import 'prices/get_prices_result.dart';
import 'prices/get_proofs_parameters.dart';
import 'prices/get_proofs_result.dart';
import 'prices/get_users_parameters.dart';
import 'prices/get_users_result.dart';
import 'prices/location.dart';
import 'prices/location_osm_type.dart';
import 'prices/price_product.dart';
Expand Down Expand Up @@ -383,9 +387,37 @@ class OpenPricesAPIClient {
return MaybeError<bool>.responseError(response);
}

/// Get user proofs.
static Future<MaybeError<GetProofsResult>> getProofs(
final GetProofsParameters parameters, {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
path: '/api/v1/proofs',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
);
final Response response = await HttpHelper().doGetRequest(
uri,
uriHelper: uriHelper,
bearerToken: bearerToken,
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<GetProofsResult>.value(
GetProofsResult.fromJson(decodedResponse),
);
} catch (e) {
//
}
}
return MaybeError<GetProofsResult>.responseError(response);
}

static Future<MaybeError<Proof>> uploadProof({
required final ProofType proofType,
required final bool isPublic,
required final Uri imageUri,
required final MediaType mediaType,
required final String bearerToken,
Expand All @@ -404,7 +436,6 @@ class OpenPricesAPIClient {
request.fields.addAll(
<String, String>{
'type': proofType.offTag,
'is_public': isPublic ? 'true' : 'false',
},
);
final List<int> fileBytes = await UriReader.instance!.readAsBytes(imageUri);
Expand Down Expand Up @@ -436,6 +467,32 @@ class OpenPricesAPIClient {
);
}

/// Get user proof by id.
static Future<MaybeError<Proof>> getProof(
final int proofId, {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
path: '/api/v1/proofs/$proofId',
forcedHost: _getHost(uriHelper),
);
final Response response = await HttpHelper().doGetRequest(
uri,
uriHelper: uriHelper,
bearerToken: bearerToken,
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<Proof>.value(Proof.fromJson(decodedResponse));
} catch (e) {
//
}
}
return MaybeError<Proof>.responseError(response);
}

/// Deletes a proof.
/// A user can delete only owned proofs. Can delete only proofs that are not associated with prices. A moderator can delete not owned proofs.
/// Returns true if successful.
Expand All @@ -458,4 +515,32 @@ class OpenPricesAPIClient {
}
return MaybeError<bool>.responseError(response);
}

static Future<MaybeError<GetUsersResult>> getUsers(
final GetUsersParameters parameters, {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
path: '/api/v1/users',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
);
final Response response = await HttpHelper().doGetRequest(
uri,
uriHelper: uriHelper,
bearerToken: bearerToken,
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<GetUsersResult>.value(
GetUsersResult.fromJson(decodedResponse),
);
} catch (e) {
//
}
}
return MaybeError<GetUsersResult>.responseError(response);
}
}
36 changes: 36 additions & 0 deletions lib/src/prices/flavor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:json_annotation/json_annotation.dart';
import '../model/off_tagged.dart';
import '../utils/server_type.dart';

/// Flavor is used to refer to a specific Open*Facts project.
///
/// cf. `Flavor` in https://prices.openfoodfacts.org/api/docs
/// Somehow redundant with [ServerType].
enum Flavor implements OffTagged {
/// Open Food Facts
@JsonValue('off')
openFoodFacts(offTag: 'off'),

/// Open Beauty Facts
@JsonValue('obf')
openBeautyFacts(offTag: 'obf'),

/// Open Pet Food Facts
@JsonValue('opff')
openPetFoodFacts(offTag: 'opff'),

/// Open Product Facts
@JsonValue('opf')
openProductFacts(offTag: 'opf'),

/// Open Product Facts (Pro platform)
@JsonValue('off-pro')
openFoodProductFactsPro(offTag: 'off-pro');

const Flavor({
required this.offTag,
});

@override
final String offTag;
}
22 changes: 3 additions & 19 deletions lib/src/prices/get_locations_parameters.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import 'get_locations_order.dart';
import 'order_by.dart';
import 'get_parameters_helper.dart';
import 'get_price_count_parameters_helper.dart';

/// Parameters for the "get locations" API query.
///
/// cf. https://prices.openfoodfacts.org/api/docs
class GetLocationsParameters extends GetParametersHelper {
class GetLocationsParameters
extends GetPriceCountParametersHelper<GetLocationsOrderField> {
String? osmNameLike;
String? osmCityLike;
String? osmPostcodeLike;
String? osmCountryLike;
int? priceCount;
int? priceCountGte;
int? priceCountLte;
List<OrderBy<GetLocationsOrderField>>? orderBy;

@override
Map<String, String> getQueryParameters() {
Expand All @@ -22,18 +18,6 @@ class GetLocationsParameters extends GetParametersHelper {
addNonNullString(osmCityLike, 'osm_address_city__like');
addNonNullString(osmPostcodeLike, 'osm_address_postcode__like');
addNonNullString(osmCountryLike, 'osm_address_country__like');
addNonNullInt(priceCount, 'price_count');
addNonNullInt(priceCountGte, 'price_count__gte');
addNonNullInt(priceCountLte, 'price_count__lte');
if (orderBy != null) {
final List<String> orders = <String>[];
for (final OrderBy order in orderBy!) {
orders.add(order.offTag);
}
if (orders.isNotEmpty) {
addNonNullString(orders.join(','), 'order_by');
}
}
return result;
}
}
8 changes: 4 additions & 4 deletions lib/src/prices/get_locations_result.g.dart

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

Loading

0 comments on commit baa1258

Please sign in to comment.