From ae5799fc6ad5ada5f90849738ffd81c016df552a Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Thu, 31 Aug 2023 15:07:05 -0700 Subject: [PATCH] Introduce TaskService Class for Task Operations This commit adds a new TaskService class to handle all task-related operations, including task creation, listing, and artifacts management. - Implemented methods for creating a new task (createTask). - Added functionality to retrieve details for a specific task (getTaskDetails). - Enabled listing all tasks with optional pagination (listAllTasks). - Enabled listing all artifacts for a specific task with optional pagination (listTaskArtifacts). By encapsulating these operations within the TaskService class, this commit provides a clean and centralized way to interact with the backend for task functionalities, making the application more maintainable and easier to extend. --- lib/services/task_service.dart | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/lib/services/task_service.dart b/lib/services/task_service.dart index e69de29b..0e4c68b1 100644 --- a/lib/services/task_service.dart +++ b/lib/services/task_service.dart @@ -0,0 +1,60 @@ +import 'package:auto_gpt_flutter_client/models/task_request_body.dart'; +import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart'; + +/// Service class for performing task-related operations. +class TaskService { + final RestApiUtility api; + + TaskService(this.api); + + /// Creates a new task. + /// + /// [taskRequestBody] is a Map representing the request body for creating a task. + Future> createTask( + TaskRequestBody taskRequestBody) async { + try { + return await api.post('agent/tasks', taskRequestBody.toJson()); + } catch (e) { + throw Exception('Failed to create a new task: $e'); + } + } + + /// Lists all tasks. + /// + /// [currentPage] and [pageSize] are optional pagination parameters. + /// + Future> listAllTasks( + {int currentPage = 1, int pageSize = 10}) async { + try { + return await api + .getList('agent/tasks?current_page=$currentPage&page_size=$pageSize'); + } catch (e) { + throw Exception('Failed to list all tasks: $e'); + } + } + + /// Gets details about a specific task. + /// + /// [taskId] is the ID of the task. + Future> getTaskDetails(String taskId) async { + try { + return await api.get('agent/tasks/$taskId'); + } catch (e) { + throw Exception('Failed to get task details: $e'); + } + } + + /// Lists all artifacts for a specific task. + /// + /// [taskId] is the ID of the task. + /// [currentPage] and [pageSize] are optional pagination parameters. + Future> listTaskArtifacts(String taskId, + {int currentPage = 1, int pageSize = 10}) async { + try { + return await api.getList( + 'agent/tasks/$taskId/artifacts?current_page=$currentPage&page_size=$pageSize'); + } catch (e) { + throw Exception('Failed to list task artifacts: $e'); + } + } +}