Skip to content

Commit

Permalink
fix: Refactor Robotoff methods (#723)
Browse files Browse the repository at this point in the history
* Separated Robotoff APIs (initial)

Deprecated and routed the robotoff apis to a new file for decluttering

* Cleaned up methods and parameters

Removed User parameter as mentioned in the requirements along with getIngredientSpellingCorrection method. Could be a breaking change

* Fixed imports and naming conventions

Fixed import directives, also made the naming conventions for classes consistent. Added TODOs
  • Loading branch information
atharv028 committed Apr 5, 2023
1 parent 5a31a09 commit 7a71cbb
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 189 deletions.
1 change: 1 addition & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ export 'src/utils/tag_type.dart';
export 'src/utils/unit_helper.dart';
export 'src/utils/uri_helper.dart';
export 'src/utils/uri_reader.dart';
export 'src/robot_off_api_client.dart';
export 'src/utils/user_product_search_query_configuration.dart';
231 changes: 42 additions & 189 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import 'model/taxonomy_packaging_material.dart';
import 'model/taxonomy_packaging_recycling.dart';
import 'model/taxonomy_packaging_shape.dart';
import 'model/user.dart';
import 'robot_off_api_client.dart';
import 'utils/abstract_query_configuration.dart';
import 'utils/country_helper.dart';
import 'utils/http_helper.dart';
Expand Down Expand Up @@ -788,226 +789,78 @@ class OpenFoodAPIClient {
}
}
//TODO(x): Add comments for robotoff
//TODO: deprecated from 2023-04-05; remove when old enough
@Deprecated('Use [RobotOffAPIClient.getRandomInsight] Instead')
static Future<InsightsResult> getRandomInsight(
User user, {
InsightType? type,
String? country,
String? valueTag,
String? serverDomain,
QueryType? queryType,
}) async {
final Map<String, String> parameters = {};
if (type != null && type.value != null) {
parameters['type'] = type.value!;
}
if (country != null) {
parameters['country'] = country;
}
if (valueTag != null) {
parameters['value_tag'] = valueTag;
}
if (serverDomain != null) {
parameters['server_domain'] = serverDomain;
}
var insightUri = UriHelper.getRobotoffUri(
path: 'api/v1/insights/random/',
queryType: queryType,
queryParameters: parameters,
);
Response response = await HttpHelper().doGetRequest(
insightUri,
user: user,
queryType: queryType,
);
var result = InsightsResult.fromJson(
HttpHelper().jsonDecode(utf8.decode(response.bodyBytes)),
);
return result;
}
}) =>
RobotOffAPIClient.getRandomInsights(
type: type,
country: OpenFoodFactsCountry.fromOffTag(country),
valueTag: valueTag,
serverDomain: serverDomain,
queryType: queryType);
//TODO: deprecated from 2023-04-05; remove when old enough
@Deprecated('Use [RobotOffAPIClient.getProductInsights] Instead')
static Future<InsightsResult> getProductInsights(
String barcode,
User user, {
QueryType? queryType,
}) async {
var insightsUri = UriHelper.getRobotoffUri(
path: 'api/v1/insights/$barcode',
queryType: queryType,
);
Response response = await HttpHelper().doGetRequest(
insightsUri,
user: user,
queryType: queryType,
);
return InsightsResult.fromJson(
HttpHelper().jsonDecode(utf8.decode(response.bodyBytes)),
);
}
}) =>
RobotOffAPIClient.getProductInsights(barcode, queryType: queryType);
//TODO: deprecated from 2023-04-05; remove when old enough
@Deprecated('Use [RobotOffAPIClient.getProductQuestions] Instead')
static Future<RobotoffQuestionResult> getRobotoffQuestionsForProduct(
String barcode,
String lang, {
User? user,
int? count,
QueryType? queryType,
}) async {
if (count == null || count <= 0) {
count = 1;
}
final Map<String, String> parameters = <String, String>{
'lang': lang,
'count': count.toString()
};
var robotoffQuestionUri = UriHelper.getRobotoffUri(
path: 'api/v1/questions/$barcode',
queryParameters: parameters,
queryType: queryType,
);
Response response = await HttpHelper().doGetRequest(
robotoffQuestionUri,
user: user,
queryType: queryType,
);
var result = RobotoffQuestionResult.fromJson(
HttpHelper().jsonDecode(utf8.decode(response.bodyBytes)),
);
return result;
}
}) =>
RobotOffAPIClient.getProductQuestions(
barcode,
OpenFoodFactsLanguage.fromOffTag(lang) ??
OpenFoodFactsLanguage.ENGLISH,
user: user,
count: count,
queryType: queryType);
//TODO: deprecated from 2023-04-05; remove when old enough
@Deprecated('Use [RobotOffAPIClient.getRandomQuestions] Instead')
static Future<RobotoffQuestionResult> getRandomRobotoffQuestion(
String lang,
User? user, {
int? count,
List<InsightType>? types,
QueryType? queryType,
}) async {
if (count == null || count <= 0) {
count = 1;
}
final List<String> typesValues = [];
if (types != null) {
for (final InsightType t in types) {
final String? value = t.value;
if (value != null) {
typesValues.add(value);
}
}
}
final Map<String, String> parameters = <String, String>{
'lang': lang,
'count': count.toString(),
if (typesValues.isNotEmpty) 'insight_types': typesValues.join(',')
};
var robotoffQuestionUri = UriHelper.getRobotoffUri(
path: 'api/v1/questions/random',
queryParameters: parameters,
queryType: queryType,
);
Response response = await HttpHelper().doGetRequest(
robotoffQuestionUri,
user: user,
queryType: queryType,
);
var result = RobotoffQuestionResult.fromJson(
HttpHelper().jsonDecode(utf8.decode(response.bodyBytes)),
);
return result;
}
}) =>
RobotOffAPIClient.getRandomQuestions(
OpenFoodFactsLanguage.fromOffTag(lang) ??
OpenFoodFactsLanguage.ENGLISH,
user,
count: count,
types: types,
queryType: queryType);
//TODO: deprecated from 2023-04-05; remove when old enough
@Deprecated('Use [RobotOffAPIClient.postInsightAnnotation] Instead')
static Future<Status> postInsightAnnotation(
String? insightId,
InsightAnnotation annotation, {
User? user,
String? deviceId,
bool update = true,
final QueryType? queryType,
}) async {
var insightUri = UriHelper.getRobotoffUri(
path: 'api/v1/insights/annotate',
queryType: queryType,
);
final Map<String, String> annotationData = {
'annotation': annotation.value.toString(),
'update': update ? '1' : '0'
};
if (insightId != null) {
annotationData['insight_id'] = insightId;
}
if (deviceId != null) {
annotationData['device_id'] = deviceId;
}
Response response = await HttpHelper().doPostRequest(
insightUri,
annotationData,
user,
queryType: queryType,
addCredentialsToBody: false,
addCredentialsToHeader: true,
);
return Status.fromApiResponse(response.body);
}
// TODO: deprecated from 2022-11-22; remove when old enough
@Deprecated('Unstable version, do not use and wait for the next version')
static Future<SpellingCorrection?> getIngredientSpellingCorrection({
String? ingredientName,
Product? product,
User? user,
QueryType? queryType,
}) async {
Map<String, String> spellingCorrectionParam;
if (ingredientName != null) {
spellingCorrectionParam = {
'text': ingredientName,
};
} else if (product != null && product.barcode != null) {
spellingCorrectionParam = {
'barcode': product.barcode!,
};
} else {
return null;
}
var spellingCorrectionUri = UriHelper.getRobotoffUri(
path: 'api/v1/predict/ingredients/spellcheck',
queryType: queryType,
);
Response response = await HttpHelper().doPostRequest(
spellingCorrectionUri,
spellingCorrectionParam,
user,
queryType: queryType,
addCredentialsToBody: false,
addCredentialsToHeader: true,
);
SpellingCorrection result = SpellingCorrection.fromJson(
HttpHelper().jsonDecode(utf8.decode(response.bodyBytes)),
);
return result;
}
}) =>
RobotOffAPIClient.postInsightAnnotation(insightId, annotation,
deviceId: deviceId, update: update, queryType: queryType);
/// Extract the ingredients from image with the given parameters.
/// The ingredients' language should be given (ingredients_fr, ingredients_de, ingredients_en)
Expand Down

0 comments on commit 7a71cbb

Please sign in to comment.