Skip to content

Commit 3de3eb8

Browse files
Merge pull request #1 from brqnko/main
feat: fetchSuggestion
2 parents 01b8b6d + b61f9a7 commit 3de3eb8

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

lib/src/search/search_client.dart

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ class SearchClient {
1717
: _http = httpClient,
1818
_controller = controller;
1919

20+
/// Fetches for suggestions based on the given query.
21+
///
22+
/// [query] The query to search for suggestions for.
23+
Future<List<String>> fetchSuggestion(
24+
String query,
25+
) async {
26+
if (query.isEmpty) {
27+
return [];
28+
}
29+
30+
final clientId = await _controller.getClientId();
31+
32+
final uri = Uri.https(
33+
'api-v2.soundcloud.com',
34+
'/search/queries', {
35+
'q': query,
36+
'client_id': clientId,
37+
}
38+
);
39+
40+
final response = await _http.get(uri);
41+
final json = jsonDecode(response.body);
42+
final collection = json['collection'] as List;
43+
44+
return collection.map((suggestion) => suggestion['query'] as String).toList();
45+
}
46+
2047
/// Searches for the provided [query] with the specified [searchFilter].
2148
///
2249
/// Search results are offset by the specified [offset], with each batch containing
@@ -178,4 +205,4 @@ class SearchClient {
178205
offset: offset,
179206
limit: limit
180207
).map((b) => b.cast<UserSearchResult>());
181-
}
208+
}

0 commit comments

Comments
 (0)