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: 831 - prices methods getProofs, getProof and getUsers + fixed uploadProof #926

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Loading