Skip to content

Commit

Permalink
Merge branch 'master' into feat/1003
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurtanuki authored Jan 3, 2025
2 parents b525687 + c583648 commit dac64b8
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 27 deletions.
15 changes: 8 additions & 7 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
---
name: Bug report
about: Create a report to help us improve
description: 'Create a report to help us improve the Open Food Facts Dart SDK'
about: 'Create a report to help us improve the Open Food Facts Dart SDK'
title: ''
labels: ''
type: '🐛 Bug'
assignees: ''

---

### Description
A clear and concise description of what the bug is.
<!-- A clear and concise description of what the bug is.-->

### Expected behavior
A clear and concise description of what you expected to happen.
<!-- A clear and concise description of what you expected to happen.-->

### Stacktraces
If applicable, provide error stacktraces.
<!-- If applicable, provide error stacktraces.-->

### Package information
Open Food Facts package version:
<!-- Open Food Facts package version:-->

### Additional context
Add any other context about the problem here.
<!-- Add any other context about the problem here.-->
10 changes: 5 additions & 5 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
type: '✨ Enhancement'
assignees: ''

---

### Why - Problem description
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]-->

### What - Proposed solution
A clear and concise description of what you want to happen.
<!-- A clear and concise description of what you want to happen.-->

### Alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
<!-- A clear and concise description of any alternative solutions or features you've considered.-->

### Additional context
Add any other context or screenshots about the feature request here.
<!-- Add any other context or screenshots about the feature request here.-->
2 changes: 1 addition & 1 deletion .github/workflows/dartdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: dart doc .

- name: Deploy API documentation to Github Pages
uses: JamesIves/[email protected].1
uses: JamesIves/[email protected].2
with:
BRANCH: gh-pages
FOLDER: doc/api/
3 changes: 3 additions & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ 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_price_products_order.dart';
export 'src/prices/get_price_products_parameters.dart';
export 'src/prices/get_price_products_result.dart';
export 'src/prices/get_prices_result.dart';
export 'src/prices/get_price_count_parameters_helper.dart';
export 'src/prices/get_proofs_order.dart';
Expand Down
30 changes: 30 additions & 0 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'prices/proof.dart';
import 'prices/get_locations_parameters.dart';
import 'prices/get_locations_result.dart';
import 'prices/get_parameters_helper.dart';
import 'prices/get_price_products_parameters.dart';
import 'prices/get_price_products_result.dart';
import 'prices/get_prices_parameters.dart';
import 'prices/get_prices_result.dart';
import 'prices/get_proofs_parameters.dart';
Expand Down Expand Up @@ -163,6 +165,34 @@ class OpenPricesAPIClient {
return MaybeError<Location>.responseError(response);
}

static Future<MaybeError<GetPriceProductsResult>> getPriceProducts(
final GetPriceProductsParameters parameters, {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = getUri(
path: '/api/v1/products',
queryParameters: parameters.getQueryParameters(),
uriHelper: 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<GetPriceProductsResult>.value(
GetPriceProductsResult.fromJson(decodedResponse),
);
} catch (e) {
//
}
}
return MaybeError<GetPriceProductsResult>.responseError(response);
}

static Future<MaybeError<PriceProduct>> getPriceProductById(
final int productId, {
final UriProductHelper uriHelper = uriHelperFoodProd,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/prices/get_parameters_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class GetParametersHelper<T extends OrderByField> {

/// Returns the parameters as a query parameter map.
Map<String, String> getQueryParameters() {
_checkIntValue('page_number', pageSize, min: 1);
_checkIntValue('page_number', pageNumber, min: 1);
_checkIntValue('page_size', pageSize, min: 1, max: 100);
_result.clear();
addNonNullInt(pageNumber, 'page');
Expand All @@ -32,7 +32,7 @@ abstract class GetParametersHelper<T extends OrderByField> {
}

void _checkIntValue(
final String field,
final String fieldDescription,
final int? value, {
final int? min,
final int? max,
Expand All @@ -43,14 +43,14 @@ abstract class GetParametersHelper<T extends OrderByField> {
if (min != null) {
if (value < min) {
throw Exception(
'$field minimum value is $min (actual value is $value)',
'$fieldDescription minimum value is $min (actual value is $value)',
);
}
}
if (max != null) {
if (value > max) {
throw Exception(
'$field maximum value is $max (actual value is $value)',
'$fieldDescription maximum value is $max (actual value is $value)',
);
}
}
Expand Down
13 changes: 13 additions & 0 deletions lib/src/prices/get_price_products_order.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'order_by.dart';

/// Field for the "order by" clause of "get price products".
enum GetPriceProductsOrderField implements OrderByField {
priceCount(offTag: 'price_count'),
created(offTag: 'created'),
updated(offTag: 'updated');

const GetPriceProductsOrderField({required this.offTag});

@override
final String offTag;
}
37 changes: 37 additions & 0 deletions lib/src/prices/get_price_products_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:openfoodfacts/src/prices/flavor.dart';

import 'get_price_count_parameters_helper.dart';
import 'get_price_products_order.dart';

/// Parameters for the "get price products" API query.
class GetPriceProductsParameters
extends GetPriceCountParametersHelper<GetPriceProductsOrderField> {
String? brandsLike;
String? brandsTagsContains;
String? categoriesTagsContains;
String? code;
String? ecoscoreGrade;
String? labelsTagsContains;
String? novaGroup;
String? nutriscoreGrade;
String? productNameLike;
int? uniqueScansNGte;
Flavor? source;

@override
Map<String, String> getQueryParameters() {
super.getQueryParameters();
addNonNullString(brandsLike, 'brands__like');
addNonNullString(brandsTagsContains, 'brands_tags__contains');
addNonNullString(categoriesTagsContains, 'categories_tags__contains');
addNonNullString(code, 'code');
addNonNullString(ecoscoreGrade, 'ecoscore_grade');
addNonNullString(labelsTagsContains, 'labels_tags__contains');
addNonNullString(novaGroup, 'nova_group');
addNonNullString(nutriscoreGrade, 'nutriscore_grade');
addNonNullString(productNameLike, 'product_name__like');
addNonNullInt(uniqueScansNGte, 'unique_scans_n__gte');
addNonNullString(source?.offTag, 'source');
return result;
}
}
33 changes: 33 additions & 0 deletions lib/src/prices/get_price_products_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:openfoodfacts/openfoodfacts.dart';

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

part 'get_price_products_result.g.dart';

/// List of price product objects returned by the "get price products" method.
@JsonSerializable()
class GetPriceProductsResult extends JsonObject {
@JsonKey()
List<PriceProduct>? items;

@JsonKey()
int? total;

@JsonKey(name: 'page')
int? pageNumber;

@JsonKey(name: 'size')
int? pageSize;

@JsonKey(name: 'pages')
int? numberOfPages;

GetPriceProductsResult();

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

@override
Map<String, dynamic> toJson() => _$GetPriceProductsResultToJson(this);
}
28 changes: 28 additions & 0 deletions lib/src/prices/get_price_products_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 dac64b8

Please sign in to comment.