Skip to content

Commit

Permalink
Fix RobotoffQuestionsForProduct API (openfoodfacts#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmeet0817 committed Oct 26, 2021
1 parent 5e74a55 commit 18fe406
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 65 deletions.
13 changes: 12 additions & 1 deletion lib/model/Insight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ extension InsightAnnotationExtension on InsightAnnotation {
}

enum InsightType {
@JsonValue('ingredient_spellcheck')
INGREDIENT_SPELLCHECK,
@JsonValue('packager_code')
PACKAGER_CODE,
@JsonValue('label')
LABEL,
@JsonValue('category')
CATEGORY,
@JsonValue('product_weight')
PRODUCT_WEIGHT,
@JsonValue('expiration_date')
EXPIRATION_DATE,
@JsonValue('brand')
BRAND,
@JsonValue('store')
STORE,
@JsonValue('nutrient')
NUTRIENT,
UNDEFINED
@JsonValue('undefined')
UNDEFINED,
UNKNOWN
}

extension InsightTypesExtension on InsightType? {
Expand Down
53 changes: 7 additions & 46 deletions lib/model/RobotoffQuestion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ part 'RobotoffQuestion.g.dart';
@JsonSerializable()
class RobotoffQuestionResult extends JsonObject {
final String? status;
@JsonKey(
name: 'questions',
includeIfNull: false,
fromJson: RobotoffQuestion.fromJson,
toJson: RobotoffQuestion.toJson)

final List<RobotoffQuestion>? questions;

const RobotoffQuestionResult({this.status, this.questions});
Expand All @@ -23,14 +19,15 @@ class RobotoffQuestionResult extends JsonObject {
Map<String, dynamic> toJson() => _$RobotoffQuestionResultToJson(this);
}

class RobotoffQuestion {
@JsonSerializable()
class RobotoffQuestion extends JsonObject {
final String? barcode;
final String? type;
final String? value;
final String? question;
@JsonKey(name: 'insight_id')
final String? insightId;
@JsonKey(name: 'insight_type')
@JsonKey(name: 'insight_type', unknownEnumValue: InsightType.UNKNOWN)
final InsightType? insightType;
@JsonKey(name: 'source_image_url')
final String? imageUrl;
Expand All @@ -44,44 +41,8 @@ class RobotoffQuestion {
this.insightType,
this.imageUrl});

static List<RobotoffQuestion> fromJson(List<dynamic> json) {
List<RobotoffQuestion> result = [];
for (Map<String, dynamic> jsonQuestion
in json as Iterable<Map<String, dynamic>>) {
InsightType insightType =
InsightTypesExtension.getType(jsonQuestion['insight_type']);

result.add(RobotoffQuestion(
barcode: jsonQuestion['barcode'],
type: jsonQuestion['type'],
value: jsonQuestion['value'],
question: jsonQuestion['question'],
insightId: jsonQuestion['insight_id'],
insightType: insightType,
imageUrl: jsonQuestion['source_image_url']));
}
return result;
}

static List<Map<String, dynamic>> toJson(List<RobotoffQuestion>? questions) {
if (questions == null) {
return [];
}
List<Map<String, dynamic>> result = [];

for (RobotoffQuestion question in questions) {
Map<String, String?> jsonQuestion = {};

jsonQuestion['barcode'] = question.barcode;
jsonQuestion['type'] = question.type;
jsonQuestion['value'] = question.value;
jsonQuestion['question'] = question.question;
jsonQuestion['insight_id'] = question.insightId;
jsonQuestion['insight_type'] = question.insightType.value;
jsonQuestion['insight_url'] = question.imageUrl;
factory RobotoffQuestion.fromJson(Map<String, dynamic> json) =>
_$RobotoffQuestionFromJson(json);

result.add(jsonQuestion);
}
return result;
}
Map<String, dynamic> toJson() => _$RobotoffQuestionToJson(this);
}
92 changes: 80 additions & 12 deletions lib/model/RobotoffQuestion.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/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ class OpenFoodAPIClient {
/// By default the query will hit the PROD DB
static Future<RobotoffQuestionResult> getRobotoffQuestionsForProduct(
String barcode,
String lang,
User user, {
String lang, {
User? user,
int? count,
QueryType? queryType,
}) async {
Expand Down
4 changes: 2 additions & 2 deletions test/api_getRobotoff_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
await OpenFoodAPIClient.getRobotoffQuestionsForProduct(
'3274570800026',
'en',
TestConstants.TEST_USER,
user: TestConstants.TEST_USER,
count: 1,
);

Expand All @@ -41,7 +41,7 @@ void main() {
await OpenFoodAPIClient.getRobotoffQuestionsForProduct(
'3274570800026',
'fr',
TestConstants.TEST_USER,
user: TestConstants.TEST_USER,
);

if (result.status != 'no_questions') {
Expand Down
4 changes: 2 additions & 2 deletions test/api_postRobotoff_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ void main() {
test('get questions for Noix de Saint-Jacques EN and answer', () async {
RobotoffQuestionResult result =
await OpenFoodAPIClient.getRobotoffQuestionsForProduct(
'0080868000633', 'en', TestConstants.TEST_USER,
count: 1);
'0080868000633', 'en',
user: TestConstants.TEST_USER, count: 1);

if (result.status == 'found') {
Status postResult = await OpenFoodAPIClient.postInsightAnnotation(
Expand Down

0 comments on commit 18fe406

Please sign in to comment.