Skip to content

Commit

Permalink
Merge branch 'master' into feat/893
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurtanuki authored Jun 20, 2024
2 parents d392500 + b7366f3 commit e73540c
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 66 deletions.
9 changes: 9 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ OxF:
- any-glob-to-any-file: 'lib/src/prices/*.dart'
- any-glob-to-any-file: 'lib/src/open_prices_api_client.dart'
- any-glob-to-any-file: 'test/api_prices_*.dart'
- any-glob-to-any-file: 'lib/src/prices/currency.dart'
- any-glob-to-any-file: 'lib/src/prices/price.dart'
- any-glob-to-any-file: 'lib/src/prices/proof.dart'
- any-glob-to-any-file: 'lib/src/prices/proof_type.dart'
- any-glob-to-any-file: 'test/api_prices_test.dart'

Search:
- changed-files:
- any-glob-to-any-file: 'test/api_search_products_test.dart'

🔐 Authentification:
- changed-files:
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## [3.11.0](https://github.com/openfoodfacts/openfoodfacts-dart/compare/v3.10.0...v3.11.0) (2024-06-07)


### Features

* 890 - new method getLocalizedCountryNames ([#904](https://github.com/openfoodfacts/openfoodfacts-dart/issues/904)) ([0fa5941](https://github.com/openfoodfacts/openfoodfacts-dart/commit/0fa59410a1df3e31b0cb2bbe8404b585938f65fa))


### Bug Fixes

* 900 - for KnowledgePanelActionElement html is now optional ([#901](https://github.com/openfoodfacts/openfoodfacts-dart/issues/901)) ([e4ec263](https://github.com/openfoodfacts/openfoodfacts-dart/commit/e4ec263a1c1dc309bfb47076870131afd5090aa1))
* 927 - minimum forced query version is now 2 ([#936](https://github.com/openfoodfacts/openfoodfacts-dart/issues/936)) ([260e482](https://github.com/openfoodfacts/openfoodfacts-dart/commit/260e48275aba37e52f11bbca72353ab58d1c066c))
* added "app_name" parameter for prices methods uploadProof and createPrice ([#938](https://github.com/openfoodfacts/openfoodfacts-dart/issues/938)) ([e4bd1ed](https://github.com/openfoodfacts/openfoodfacts-dart/commit/e4bd1ed091767c609e12066f08cfee05eea9cffe))

## [3.10.0](https://github.com/openfoodfacts/openfoodfacts-dart/compare/v3.9.0...v3.10.0) (2024-05-22)


Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/knowledge_panel_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ class KnowledgePanelTableElement extends JsonObject {
@JsonSerializable()
class KnowledgePanelActionElement extends JsonObject {
/// HTML description.
final String html;
final String? html;

/// Needed contribute actions, as [KnowledgePanelAction.addCategories.offTag].
final List<String> actions;

const KnowledgePanelActionElement({
required this.html,
this.html,
required this.actions,
});

Expand Down
2 changes: 1 addition & 1 deletion 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: 2 additions & 0 deletions lib/src/model/parameter/page_size.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import '../../interface/parameter.dart';

/// "Page size" search API parameter
///
/// Typically defaults to 50 (used to be 24). Max value seems to be 100.
class PageSize extends Parameter {
@override
String getName() => 'page_size';
Expand Down
27 changes: 27 additions & 0 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1335,4 +1335,31 @@ class OpenFoodAPIClient {
'Different imagefield: expected "$id", actual "$imageField"');
}
}

/// Returns the name of each country localized in [language].
static Future<Map<OpenFoodFactsCountry, String>> getLocalizedCountryNames(
final OpenFoodFactsLanguage language, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Response response = await HttpHelper().doGetRequest(
uriHelper.getUri(
path: '/cgi/countries.pl',
queryParameters: {'lc': language.offTag},
),
uriHelper: uriHelper,
);
final Map<OpenFoodFactsCountry, String> result =
<OpenFoodFactsCountry, String>{};
final Map<String, dynamic> map = HttpHelper().jsonDecode(response.body);
for (final String countryCode in map.keys) {
final OpenFoodFactsCountry? country =
OpenFoodFactsCountry.fromOffTag(countryCode);
if (country == null) {
// very unlikely
continue;
}
result[country] = map[countryCode];
}
return result;
}
}
81 changes: 47 additions & 34 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,28 @@ class OpenPricesAPIClient {
static String _getHost(final UriProductHelper uriHelper) =>
uriHelper.getHost(_subdomain);

static Uri getUri({
required final String path,
final Map<String, dynamic>? queryParameters,
final UriProductHelper uriHelper = uriHelperFoodProd,
final bool? addUserAgentParameters,
}) =>
uriHelper.getUri(
path: path,
queryParameters: queryParameters,
forcedHost: _getHost(uriHelper),
addUserAgentParameters: addUserAgentParameters,
);

static Future<MaybeError<GetPricesResult>> getPrices(
final GetPricesParameters parameters, {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/prices',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -74,9 +87,9 @@ class OpenPricesAPIClient {
required final LocationOSMType locationOSMType,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/locations/osm/${locationOSMType.offTag}/$locationOSMId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -98,10 +111,10 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/locations',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -125,9 +138,9 @@ class OpenPricesAPIClient {
final int locationId, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/locations/$locationId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -148,9 +161,9 @@ class OpenPricesAPIClient {
final int productId, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/products/$productId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -173,9 +186,9 @@ class OpenPricesAPIClient {
final String productCode, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/products/code/$productCode',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -201,9 +214,9 @@ class OpenPricesAPIClient {
static Future<MaybeError<String>> getStatus({
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/status',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand Down Expand Up @@ -234,9 +247,9 @@ class OpenPricesAPIClient {
final bool setCookie = false,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/auth${setCookie ? '?set_cookie=1' : ''}',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await post(
uri,
Expand All @@ -261,9 +274,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/session',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -288,9 +301,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/session',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doDeleteRequest(
uri,
Expand All @@ -308,9 +321,9 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/prices',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final StringBuffer body = StringBuffer();
body.write('{');
Expand Down Expand Up @@ -372,9 +385,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/prices/$priceId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doDeleteRequest(
uri,
Expand All @@ -393,10 +406,10 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -423,9 +436,9 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs/upload',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);

final http.MultipartRequest request = http.MultipartRequest('POST', uri);
Expand Down Expand Up @@ -473,9 +486,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs/$proofId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -501,9 +514,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs/$proofId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doDeleteRequest(
uri,
Expand All @@ -521,10 +534,10 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/users',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand Down
12 changes: 12 additions & 0 deletions lib/src/prices/currency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -927,4 +927,16 @@ enum Currency {

/// Not really attached to a specific country at all.
final bool noCountry;

static Currency? fromName(final String? name) {
if (name == null) {
return null;
}
for (final Currency currency in values) {
if (currency.name == name) {
return currency;
}
}
return null;
}
}
12 changes: 8 additions & 4 deletions lib/src/prices/price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,35 @@ class Price extends JsonObject {
@JsonKey(name: 'proof_id')
int? proofId;

/// Price ID. Read-only.
@JsonKey()
late int id;

/// Product ID. Read-only.
@JsonKey(name: 'product_id')
int? productId;

/// Location ID. Read-only.
@JsonKey(name: 'location_id')
int? locationId;

/// Proof.
/// Proof. Read-only.
@JsonKey()
Proof? proof;

/// Location.
/// Location. Read-only.
@JsonKey()
Location? location;

/// Product.
/// Product. Read-only.
@JsonKey()
PriceProduct? product;

/// Owner.
/// Owner. Read-only.
@JsonKey()
late String owner;

/// Creation timestamp. Read-only.
@JsonKey(fromJson: JsonHelper.stringTimestampToDate)
late DateTime created;

Expand Down
Loading

0 comments on commit e73540c

Please sign in to comment.