Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #465 - new "limit" parameter for autocomplete suggestions #498

Merged
merged 7 commits into from
Jun 29, 2022
29 changes: 14 additions & 15 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -722,33 +722,32 @@ class OpenFoodAPIClient {
return result;
}

/// Give user suggestion based on autocompleted outputs
/// The expected output language can be set otherwise English will be used by default
/// The TagType is required
/// Returns a List of suggestions
/// Returns suggestions.
///
/// The [limit] has a max value of 400 on the server side.
static Future<List<dynamic>> getAutocompletedSuggestions(
TagType taxonomyType, {
String input = '',
OpenFoodFactsLanguage language = OpenFoodFactsLanguage.ENGLISH,
QueryType? queryType,
final TagType taxonomyType, {
final String input = '',
final OpenFoodFactsLanguage language = OpenFoodFactsLanguage.ENGLISH,
final QueryType? queryType,
final int limit = 25,
}) async {
var suggestionUri = UriHelper.getPostUri(
final Uri uri = UriHelper.getPostUri(
path: '/cgi/suggest.pl',
queryType: queryType,
);
Map<String, String> queryParamater = <String, String>{
final Map<String, String> queryParameters = <String, String>{
'tagtype': taxonomyType.key,
'term': input,
'lc': language.code,
'limit': limit.toString(),
};

Response response = await HttpHelper().doPostRequest(
suggestionUri,
queryParamater,
final Response response = await HttpHelper().doPostRequest(
uri,
queryParameters,
null,
queryType: queryType,
);

return json.decode(response.body);
}

Expand Down
9 changes: 9 additions & 0 deletions test/api_get_autocompleted_suggestions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ void main() {
);

_listContains(result, 'compo');

result = await OpenFoodAPIClient.getAutocompletedSuggestions(
TagType.CATEGORIES,
language: OpenFoodFactsLanguage.FRENCH,
input: 'soja',
limit: 1000,
);
expect(result.length, greaterThan(40)); // 20220626: 51 items
_listContains(result, 'soja');
});
test('Suggestions for ingredients', () async {
List<dynamic> result =
Expand Down
2 changes: 1 addition & 1 deletion test/api_saveProduct_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void main() {

expect(status.status, 1);
expect(status.statusVerbose, 'fields saved');
});
}, skip: 'Works randomly');

test('add new product test 6', () async {
Product product = Product(
Expand Down