Skip to content

Commit

Permalink
feat: Allow to pass insight_types to api/v1/questions/$barcode (#748
Browse files Browse the repository at this point in the history
)
  • Loading branch information
g123k committed Jul 7, 2023
1 parent 38c4695 commit cf9dd14
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/src/robot_off_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import 'package:http/http.dart';

import 'model/insight.dart';
import 'model/robotoff_question.dart';
import 'model/robotoff_question_order.dart';
import 'model/status.dart';
import 'model/user.dart';
import 'utils/country_helper.dart';
import 'utils/http_helper.dart';
import 'utils/language_helper.dart';
import 'utils/query_type.dart';
import 'model/robotoff_question_order.dart';
import 'utils/uri_helper.dart';
import 'utils/server_type.dart';
import 'utils/uri_helper.dart';

class RobotoffAPIClient {
RobotoffAPIClient._();
Expand Down Expand Up @@ -84,11 +84,15 @@ class RobotoffAPIClient {
int? count,
ServerType? serverType,
QueryType? queryType,
List<InsightType>? insightTypes,
}) async {
final Map<String, String> parameters = <String, String>{
'lang': language.code,
if (count != null) 'count': count.toString(),
if (serverType != null) 'server_type': serverType.offTag,
if (insightTypes != null)
'insight_types':
insightTypes.map((InsightType type) => type.offTag).join(','),
};

var robotoffQuestionUri = UriHelper.getRobotoffUri(
Expand Down
39 changes: 39 additions & 0 deletions test/api_get_robotoff_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/test.dart';

import 'test_constants.dart';

void main() {
Expand Down Expand Up @@ -58,6 +59,44 @@ void main() {
}
});

test('Find questions by insight type', () async {
// Let's find 5 products with questions
final OpenFoodFactsLanguage language = OpenFoodFactsLanguage.ENGLISH;
final User user = TestConstants.PROD_USER;

final RobotoffQuestionResult productsWithQuestions =
await RobotoffAPIClient.getQuestions(
language,
user: user,
count: 5,
);

// For each question, check if we can get it with [getProductQuestions]
// with the given insight type
if (productsWithQuestions.questions?.isNotEmpty == true) {
for (RobotoffQuestion question in productsWithQuestions.questions!) {
if (question.insightType != null) {
final InsightType insightType = question.insightType!;

final RobotoffQuestionResult result =
await RobotoffAPIClient.getProductQuestions(
question.barcode!,
language,
user: user,
insightTypes: [insightType],
);

int count = result.questions!
.where((RobotoffQuestion productQuestion) =>
productQuestion.insightType == insightType)
.length;

expect(count, greaterThan(0));
}
}
}
});

test('get popular questions with types', () async {
const InsightType type = InsightType.CATEGORY;
const int numQuestions = 10;
Expand Down

0 comments on commit cf9dd14

Please sign in to comment.