Skip to content

Tasks refactor #180

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

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:meilisearch/src/key.dart';
import 'package:meilisearch/src/query_parameters/tasks_query.dart';
import 'package:meilisearch/src/result_task.dart';
import 'package:meilisearch/src/task.dart';
import 'package:meilisearch/src/task_info.dart';

Expand Down Expand Up @@ -90,7 +92,7 @@ abstract class MeiliSearchClient {
Future<AllStats> getStats();

/// Get a list of tasks from the client.
Future<List<Task>> getTasks();
Future<ResultTask> getTasks({TasksQuery? params});

/// Get a task from an index specified by uid with the specified uid.
Future<Task> getTask(int uid);
Expand Down
11 changes: 6 additions & 5 deletions lib/src/client_impl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:dio/dio.dart';
import 'package:meilisearch/src/client_task_impl.dart';
import 'package:meilisearch/src/query_parameters/tasks_query.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';
Expand Down Expand Up @@ -202,12 +204,11 @@ class MeiliSearchClientImpl implements MeiliSearchClient {
///

@override
Future<List<Task>> getTasks() async {
final response = await http.getMethod('/tasks');
Future<ResultTask> getTasks({TasksQuery? params}) async {
final response =
await http.getMethod('/tasks', queryParameters: params?.toQuery());

return (response.data['results'] as List)
.map((update) => Task.fromMap(update))
.toList();
return ResultTask.fromMap(response.data);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/client_task_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ClientTaskImpl implements TaskInfo {
MeiliSearchClientImpl client,
Map<String, dynamic> map,
) =>
ClientTaskImpl(client, map['taskUid'] as int);
ClientTaskImpl(client, (map['uid'] ?? map['taskUid']) as int);

@override
Future<Task> getStatus() async {
Expand Down
5 changes: 4 additions & 1 deletion lib/src/index.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:meilisearch/src/query_parameters/tasks_query.dart';
import 'package:meilisearch/src/result_task.dart';

import 'index_settings.dart';

import 'task_info.dart';
Expand Down Expand Up @@ -164,7 +167,7 @@ abstract class MeiliSearchIndex {
Future<IndexStats> getStats();

/// Get all tasks from the index.
Future<List<Task>> getTasks();
Future<ResultTask> getTasks({TasksQuery? params});

/// Get a task from an index specified by uid.
Future<Task> getTask(int uid);
Expand Down
15 changes: 10 additions & 5 deletions lib/src/index_impl.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:dio/dio.dart';
import 'package:meilisearch/src/query_parameters/tasks_query.dart';
import 'package:meilisearch/src/result_task.dart';

import 'client.dart';
import 'index.dart';
Expand Down Expand Up @@ -430,12 +432,15 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex {
/// Tasks endpoints
///

Future<List<Task>> getTasks() async {
final response = await http.getMethod('/indexes/$uid/tasks');
@override
Future<ResultTask> getTasks({TasksQuery? params}) async {
if (params == null) {
params = TasksQuery(indexUid: [this.uid]);
} else {
params.indexUid.add(this.uid);
}

return (response.data['results'] as List)
.map((update) => Task.fromMap(update))
.toList();
return await client.getTasks(params: params);
}

Future<Task> getTask(int uid) async {
Expand Down
29 changes: 29 additions & 0 deletions lib/src/query_parameters/tasks_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class TasksQuery {
final int? from;
final int? next;
final int? limit;
List<String> status;
List<String> type;
List<String> indexUid;

TasksQuery(
{this.limit,
this.from,
this.next,
this.indexUid: const [],
this.status: const [],
this.type: const []});

Map<String, dynamic> toQuery() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the one just above is the constructor what is this one for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of this method is to transform the internal data in a way I can send to the post/get methods as the query string:

final response = await http.getMethod('/tasks', queryParameters: params?.toQuery());

By using this idea, I'll get an object ready to be used, because all the transformations like joined arrays and the removal of null values are already done.

return <String, dynamic>{
'from': this.from,
'next': this.next,
'limit': this.limit,
'indexUid': this.indexUid,
'status': this.status,
'type': this.type,
}
..removeWhere((key, val) => val == null || (val is List && val.isEmpty))
..updateAll((key, val) => val is List ? val.join(',') : val);
}
}
22 changes: 22 additions & 0 deletions lib/src/result_task.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:meilisearch/src/task.dart';

class ResultTask<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name of ResultTask to TaskResult in the golang SDK not sure it's really relevant but for TasksQuery you put Task at the beginning

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we should follow the standard: TasksResults?

image

final List<Task> results;
final int? next;
final int? limit;
final int? from;

ResultTask(
{this.results: const [],
this.limit: null,
this.from: null,
this.next: null});

factory ResultTask.fromMap(Map<String, dynamic> map) => ResultTask(
results:
(map['results'] as List).map((item) => Task.fromMap(item)).toList(),
next: map['next'] as int?,
from: map['from'] as int?,
limit: map['limit'] as int?,
);
}
2 changes: 1 addition & 1 deletion lib/src/task_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TaskImpl implements TaskInfo {
MeiliSearchIndexImpl index,
Map<String, dynamic> map,
) =>
TaskImpl(index, map['taskUid'] as int);
TaskImpl(index, (map['uid'] ?? map['taskUid']) as int);

@override
Future<Task> getStatus() async {
Expand Down
3 changes: 2 additions & 1 deletion test/get_client_stats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void main() {

final tasks = await client.getTasks();

expect(tasks.length, greaterThan(0));
expect(tasks.results.length, greaterThan(0));
expect(tasks.results.first, isA<Task>());
});

test('gets a task by taskId', () async {
Expand Down
6 changes: 3 additions & 3 deletions test/indexes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void main() {

final tasks = await index.getTasks();

expect(tasks.length, 3);
expect(tasks.results.length, equals(3));
});

test('gets a task from a index by taskId', () async {
Expand All @@ -155,9 +155,9 @@ void main() {
{'book_id': 1234, 'title': 'Pride and Prejudice'}
]);

final task = await index.getTask(response.uid);
final task = await index.getTask(response.taskUid);

expect(task.uid, response.uid);
expect(task.uid, response.taskUid);
});

test('gets a task with a failure', () async {
Expand Down