diff --git a/lib/src/client.dart b/lib/src/client.dart index 254f343a..b9f802ff 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -5,7 +5,6 @@ import 'package:meilisearch/src/query_parameters/tasks_query.dart'; import 'package:meilisearch/src/result.dart'; import 'package:meilisearch/src/result_task.dart'; import 'package:meilisearch/src/task.dart'; -import 'package:meilisearch/src/task_info.dart'; import 'http_request.dart'; import 'index.dart'; @@ -46,13 +45,13 @@ abstract class MeiliSearchClient { /// Create a new index by given [uid] and optional [primaryKey] parameter. /// Throws an error if index is already exists. - Future createIndex(String uid, {String primaryKey}); + Future createIndex(String uid, {String primaryKey}); /// Delete the index by matching [uid]. - Future deleteIndex(String uid); + Future deleteIndex(String uid); /// Update the primary Key of the index by matching [uid]. - Future updateIndex(String uid, String primaryKey); + Future updateIndex(String uid, String primaryKey); /// Return health of the Meilisearch server. /// Throws an error if containing details if Meilisearch can't process your request. diff --git a/lib/src/client_impl.dart b/lib/src/client_impl.dart index 14d0f91c..2576b663 100644 --- a/lib/src/client_impl.dart +++ b/lib/src/client_impl.dart @@ -1,12 +1,10 @@ import 'package:dio/dio.dart'; -import 'package:meilisearch/src/client_task_impl.dart'; import 'package:meilisearch/src/query_parameters/indexes_query.dart'; import 'package:meilisearch/src/query_parameters/keys_query.dart'; import 'package:meilisearch/src/query_parameters/tasks_query.dart'; import 'package:meilisearch/src/result.dart'; import 'package:meilisearch/src/result_task.dart'; import 'package:meilisearch/src/task.dart'; -import 'package:meilisearch/src/task_info.dart'; import 'package:meilisearch/src/tenant_token.dart'; import 'http_request.dart'; @@ -38,14 +36,14 @@ class MeiliSearchClientImpl implements MeiliSearchClient { return new MeiliSearchIndexImpl(this, uid); } - Future _update(Future future) async { + Future _update(Future future) async { final response = await future; - return ClientTaskImpl.fromMap(this, response.data); + return Task.fromMap(response.data); } @override - Future createIndex(String uid, {String? primaryKey}) async { + Future createIndex(String uid, {String? primaryKey}) async { final data = { 'uid': uid, if (primaryKey != null) 'primaryKey': primaryKey, @@ -84,14 +82,14 @@ class MeiliSearchClientImpl implements MeiliSearchClient { } @override - Future deleteIndex(String uid) async { + Future deleteIndex(String uid) async { final index = this.index(uid); return await index.delete(); } @override - Future updateIndex(String uid, String primaryKey) async { + Future updateIndex(String uid, String primaryKey) async { final index = this.index(uid); return index.update(primaryKey: primaryKey); @@ -132,7 +130,8 @@ class MeiliSearchClientImpl implements MeiliSearchClient { @override Future getKey(String keyOrUid) async { - final response = await http.getMethod>('/keys/${keyOrUid}'); + final response = + await http.getMethod>('/keys/${keyOrUid}'); return Key.fromJson(response.data!); } diff --git a/lib/src/client_task_impl.dart b/lib/src/client_task_impl.dart deleted file mode 100644 index 2232eb63..00000000 --- a/lib/src/client_task_impl.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:meilisearch/src/client_impl.dart'; -import 'package:meilisearch/src/task.dart'; - -import 'task_info.dart'; - -class ClientTaskImpl implements TaskInfo { - final int taskUid; - final MeiliSearchClientImpl client; - - ClientTaskImpl(this.client, this.taskUid); - - factory ClientTaskImpl.fromMap( - MeiliSearchClientImpl client, - Map map, - ) => - ClientTaskImpl(client, (map['uid'] ?? map['taskUid']) as int); - - @override - Future getStatus() async { - return await client.getTask(this.taskUid); - } -} diff --git a/lib/src/index.dart b/lib/src/index.dart index 0c50ae85..5d224344 100644 --- a/lib/src/index.dart +++ b/lib/src/index.dart @@ -5,7 +5,6 @@ import 'package:meilisearch/src/result_task.dart'; import 'index_settings.dart'; -import 'task_info.dart'; import 'search_result.dart'; import 'stats.dart' show IndexStats; import 'task.dart'; @@ -19,10 +18,10 @@ abstract class MeiliSearchIndex { set primaryKey(String? primaryKey); /// Update the primary Key of the index. - Future update({String primaryKey}); + Future update({String primaryKey}); /// Delete the index. - Future delete(); + Future delete(); /// Search for documents matching a specific query in the index. Future search( @@ -50,26 +49,26 @@ abstract class MeiliSearchIndex { /// Add a list of documents by given [documents] and optional [primaryKey] parameter. /// If index is not exists tries to create a new index and adds documents. - Future addDocuments( + Future addDocuments( List> documents, { String? primaryKey, }); /// Add a list of documents or update them if they already exist by given [documents] and optional [primaryKey] parameter. /// If index is not exists tries to create a new index and adds documents. - Future updateDocuments( + Future updateDocuments( List> documents, { String? primaryKey, }); /// Delete one document by given [id]. - Future deleteDocument(dynamic id); + Future deleteDocument(dynamic id); /// Delete all documents in the specified index. - Future deleteAllDocuments(); + Future deleteAllDocuments(); /// Delete a selection of documents by given [ids]. - Future deleteDocuments(List ids); + Future deleteDocuments(List ids); /// Get the settings of the index. Future getSettings(); @@ -78,82 +77,80 @@ abstract class MeiliSearchIndex { Future> getFilterableAttributes(); /// Reset filterable attributes of the index. - Future resetFilterableAttributes(); + Future resetFilterableAttributes(); /// Update filterable attributes of the index. - Future updateFilterableAttributes( - List filterableAttributes); + Future updateFilterableAttributes(List filterableAttributes); /// Get the displayed attributes of the index. Future> getDisplayedAttributes(); /// Reset the displayed attributes of the index. - Future resetDisplayedAttributes(); + Future resetDisplayedAttributes(); /// Update the displayed attributes of the index. - Future updateDisplayedAttributes(List displayedAttributes); + Future updateDisplayedAttributes(List displayedAttributes); /// Get the distinct attribute for the index. Future getDistinctAttribute(); /// Reset the distinct attribute for the index. - Future resetDistinctAttribute(); + Future resetDistinctAttribute(); /// Update the distinct attribute for the index. - Future updateDistinctAttribute(String distinctAttribute); + Future updateDistinctAttribute(String distinctAttribute); /// Get ranking rules of the index. Future> getRankingRules(); /// Reset ranking rules of the index. - Future resetRankingRules(); + Future resetRankingRules(); /// Update ranking rules of the index. - Future updateRankingRules(List rankingRules); + Future updateRankingRules(List rankingRules); /// Get searchable attributes of the index. Future> getSearchableAttributes(); /// Reset searchable attributes of the index. - Future resetSearchableAttributes(); + Future resetSearchableAttributes(); /// Update the searchable attributes of the index. - Future updateSearchableAttributes( - List searchableAttributes); + Future updateSearchableAttributes(List searchableAttributes); /// Get stop words of the index. Future> getStopWords(); /// Reset stop words of the index. - Future resetStopWords(); + Future resetStopWords(); /// Update stop words of the index - Future updateStopWords(List stopWords); + Future updateStopWords(List stopWords); /// Get synonyms of the index. Future>> getSynonyms(); /// Reset synonyms of the index. - Future resetSynonyms(); + Future resetSynonyms(); /// Update synonyms of the index - Future updateSynonyms(Map> synonyms); + Future updateSynonyms(Map> synonyms); /// Get sortable attributes of the index. Future> getSortableAttributes(); /// Reset sortable attributes of the index. - Future resetSortableAttributes(); + Future resetSortableAttributes(); /// Update sortable attributes of the index. - Future updateSortableAttributes(List sortableAttributes); + Future updateSortableAttributes(List sortableAttributes); /// Reset the settings of the index. /// All settings will be reset to their default value. - Future resetSettings(); + Future resetSettings(); /// Update the settings of the index. Any parameters not provided in the body will be left unchanged. - Future updateSettings(IndexSettings settings); + Future updateSettings(IndexSettings settings); /// Get the information of the index from the Meilisearch server and return it. Future fetchInfo(); diff --git a/lib/src/index_impl.dart b/lib/src/index_impl.dart index 98845fd8..ebc7ac32 100644 --- a/lib/src/index_impl.dart +++ b/lib/src/index_impl.dart @@ -8,8 +8,6 @@ import 'client.dart'; import 'index.dart'; import 'http_request.dart'; import 'index_settings.dart'; -import 'task_info.dart'; -import 'task_impl.dart'; import 'search_result.dart'; import 'stats.dart' show IndexStats; import 'task.dart'; @@ -73,7 +71,7 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { // @override - Future update({String? primaryKey}) async { + Future update({String? primaryKey}) async { final data = { 'primaryKey': primaryKey, }; @@ -83,7 +81,7 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future delete() async { + Future delete() async { return await _update(http.deleteMethod('/indexes/$uid')); } @@ -149,13 +147,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { // Document endpoints // - Future _update(Future future) async { + Future _update(Future future) async { final response = await future; - return TaskImpl.fromMap(this, response.data); + return Task.fromMap(response.data); } @override - Future addDocuments( + Future addDocuments( documents, { String? primaryKey, }) async { @@ -169,7 +167,7 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future updateDocuments( + Future updateDocuments( documents, { String? primaryKey, }) async { @@ -183,17 +181,17 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future deleteAllDocuments() async { + Future deleteAllDocuments() async { return await _update(http.deleteMethod('/indexes/$uid/documents')); } @override - Future deleteDocument(dynamic id) async { + Future deleteDocument(dynamic id) async { return await _update(http.deleteMethod('/indexes/$uid/documents/$id')); } @override - Future deleteDocuments(List ids) async { + Future deleteDocuments(List ids) async { return await _update(http.postMethod( '/indexes/$uid/documents/delete-batch', data: ids, @@ -230,12 +228,12 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetSettings() async { + Future resetSettings() async { return await _update(http.deleteMethod('/indexes/$uid/settings')); } @override - Future updateSettings(IndexSettings settings) async { + Future updateSettings(IndexSettings settings) async { return await _update(http.patchMethod( '/indexes/$uid/settings', data: settings.toMap(), @@ -251,13 +249,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetFilterableAttributes() async { + Future resetFilterableAttributes() async { return await _update( http.deleteMethod('/indexes/$uid/settings/filterable-attributes')); } @override - Future updateFilterableAttributes( + Future updateFilterableAttributes( List filterableAttributes) async { return await _update(http.putMethod( '/indexes/$uid/settings/filterable-attributes', @@ -273,13 +271,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetDisplayedAttributes() async { + Future resetDisplayedAttributes() async { return await _update( http.deleteMethod('/indexes/$uid/settings/displayed-attributes')); } @override - Future updateDisplayedAttributes( + Future updateDisplayedAttributes( List displayedAttributes) async { return await _update(http.putMethod( '/indexes/$uid/settings/displayed-attributes', @@ -295,13 +293,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetDistinctAttribute() async { + Future resetDistinctAttribute() async { return await _update( http.deleteMethod('/indexes/$uid/settings/distinct-attribute')); } @override - Future updateDistinctAttribute(String distinctAttribute) async { + Future updateDistinctAttribute(String distinctAttribute) async { return await _update(http.putMethod( '/indexes/$uid/settings/distinct-attribute', data: '"$distinctAttribute"')); @@ -316,13 +314,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetRankingRules() async { + Future resetRankingRules() async { return await _update( http.deleteMethod('/indexes/$uid/settings/ranking-rules')); } @override - Future updateRankingRules(List rankingRules) async { + Future updateRankingRules(List rankingRules) async { return await _update(http.putMethod('/indexes/$uid/settings/ranking-rules', data: rankingRules)); } @@ -335,7 +333,7 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetStopWords() async { + Future resetStopWords() async { return await _update( http.deleteMethod('/indexes/$uid/settings/stop-words')); } @@ -349,13 +347,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetSearchableAttributes() async { + Future resetSearchableAttributes() async { return await _update( http.deleteMethod('/indexes/$uid/settings/searchable-attributes')); } @override - Future updateSearchableAttributes( + Future updateSearchableAttributes( List searchableAttributes) async { return await _update(http.putMethod( '/indexes/$uid/settings/searchable-attributes', @@ -363,7 +361,7 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future updateStopWords(List stopWords) async { + Future updateStopWords(List stopWords) async { return await _update( http.putMethod('/indexes/$uid/settings/stop-words', data: stopWords)); } @@ -378,12 +376,12 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetSynonyms() async { + Future resetSynonyms() async { return await _update(http.deleteMethod('/indexes/$uid/settings/synonyms')); } @override - Future updateSynonyms(Map> synonyms) async { + Future updateSynonyms(Map> synonyms) async { return await _update( http.putMethod('/indexes/$uid/settings/synonyms', data: synonyms)); } @@ -397,14 +395,13 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex { } @override - Future resetSortableAttributes() async { + Future resetSortableAttributes() async { return await _update( http.deleteMethod('/indexes/$uid/settings/sortable-attributes')); } @override - Future updateSortableAttributes( - List sortableAttributes) async { + Future updateSortableAttributes(List sortableAttributes) async { return _update(http.putMethod('/indexes/$uid/settings/sortable-attributes', data: sortableAttributes)); } diff --git a/lib/src/task.dart b/lib/src/task.dart index 0f822d57..d9294441 100644 --- a/lib/src/task.dart +++ b/lib/src/task.dart @@ -21,7 +21,7 @@ class Task { factory Task.fromMap(Map map) => Task( status: map['status'] as String?, - uid: map['uid'] as int?, + uid: (map['uid'] ?? map['taskUid']) as int, duration: map['duration'] as String?, enqueuedAt: map['enqueuedAt'] != null ? DateTime.tryParse(map['enqueuedAt'] as String) diff --git a/lib/src/task_impl.dart b/lib/src/task_impl.dart deleted file mode 100644 index dd47a177..00000000 --- a/lib/src/task_impl.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:meilisearch/src/task.dart'; - -import 'index_impl.dart'; -import 'task_info.dart'; - -class TaskImpl implements TaskInfo { - final int taskUid; - final MeiliSearchIndexImpl index; - - TaskImpl(this.index, this.taskUid); - - factory TaskImpl.fromMap( - MeiliSearchIndexImpl index, - Map map, - ) => - TaskImpl(index, (map['uid'] ?? map['taskUid']) as int); - - @override - Future getStatus() async { - return index.getTask(taskUid); - } -} diff --git a/lib/src/task_info.dart b/lib/src/task_info.dart deleted file mode 100644 index 6716607c..00000000 --- a/lib/src/task_info.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'task.dart'; - -abstract class TaskInfo { - int get taskUid; - - Future getStatus(); -} diff --git a/test/get_client_stats_test.dart b/test/get_client_stats_test.dart index ccf77922..237b4f9d 100644 --- a/test/get_client_stats_test.dart +++ b/test/get_client_stats_test.dart @@ -40,10 +40,10 @@ void main() { final uid = randomUid(); final info = await client.createIndex(uid); - final task = await client.getTask(info.taskUid); + final task = await client.getTask(info.uid!); expect(task, isA()); - expect(task.uid, equals(info.taskUid)); + expect(task.uid, equals(info.uid)); }); }); } diff --git a/test/indexes_test.dart b/test/indexes_test.dart index 75847b25..6283a888 100644 --- a/test/indexes_test.dart +++ b/test/indexes_test.dart @@ -154,9 +154,9 @@ void main() { {'book_id': 1234, 'title': 'Pride and Prejudice'} ]); - final task = await index.getTask(response.taskUid); + final task = await index.getTask(response.uid!); - expect(task.uid, response.taskUid); + expect(task.uid, response.uid!); }); test('gets a task with a failure', () async { diff --git a/test/utils/client.dart b/test/utils/client.dart index 4db4141b..c292516b 100644 --- a/test/utils/client.dart +++ b/test/utils/client.dart @@ -6,7 +6,6 @@ import 'package:crypto/crypto.dart'; import 'package:meilisearch/src/http_request_impl.dart'; import 'package:meilisearch/src/http_request.dart'; import 'package:meilisearch/meilisearch.dart'; -import 'package:meilisearch/src/task_info.dart'; import 'package:meilisearch/src/task.dart'; import 'package:test/test.dart'; @@ -80,7 +79,7 @@ String sha1RandomString() { return sha1.convert(randomBytes).toString(); } -extension TaskWaiter on TaskInfo { +extension TaskWaiter on Task { Future waitFor({ Duration timeout = const Duration(seconds: 5), Duration interval = const Duration(milliseconds: 50), @@ -88,18 +87,20 @@ extension TaskWaiter on TaskInfo { var endingTime = DateTime.now().add(timeout); while (DateTime.now().isBefore(endingTime)) { - var response = await getStatus(); - if (response.status != 'enqueued' && response.status != 'processing') { - return response; + var task = await client.getTask(this.uid!); + + if (task.status != 'enqueued' && task.status != 'processing') { + return task; } + await Future.delayed(interval); } - throw Exception('The task ${taskUid} timed out.'); + throw Exception('The task ${this.uid!} timed out.'); } } -extension TaskWaiterForFutures on Future { +extension TaskWaiterForFutures on Future { Future waitFor({ Duration timeout = const Duration(seconds: 5), Duration interval = const Duration(milliseconds: 50),