Skip to content

Commit

Permalink
feat: #465 - new "limit" parameter for autocomplete suggestions (#498)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_get_automcompleted_suggestions.dart`: additional test about the new `limit` parameter.
* `api_saveProduct_test.dart`: unrelated test skip
* `KnowlegdePanelElement.dart`: `title` is now optional
* `KnowlegdePanelElement.g.dart`: generated
* `openfoodfacts.dart`: new `limit` parameter for autocomplete suggestions.
  • Loading branch information
monsieurtanuki authored Jun 29, 2022
1 parent a8b1b5b commit 4fd1f75
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
29 changes: 14 additions & 15 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -730,33 +730,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

0 comments on commit 4fd1f75

Please sign in to comment.