From 16efb964099d3045cb0d88891cbf3173fc339c5b Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Thu, 14 Sep 2023 20:04:44 -0700 Subject: [PATCH] Implement BenchmarkService with generateReport and pollUpdates methods This commit introduces the `BenchmarkService` class, which encapsulates API calls related to benchmarking operations. The class includes two methods: 1. `generateReport`: Takes a `ReportRequestBody` object as input and performs a POST request to the `/reports` URL, serializing the object to JSON format. 2. `pollUpdates`: Accepts a UNIX UTC timestamp as an argument and performs a GET request to the `/updates?last_update_time=` URL. Both methods use the `RestApiUtility` for making HTTP requests and are designed to work with different base URLs, controlled by the `ApiType` enum. --- frontend/lib/services/benchmark_service.dart | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 frontend/lib/services/benchmark_service.dart diff --git a/frontend/lib/services/benchmark_service.dart b/frontend/lib/services/benchmark_service.dart new file mode 100644 index 00000000..e5358c27 --- /dev/null +++ b/frontend/lib/services/benchmark_service.dart @@ -0,0 +1,35 @@ +import 'dart:async'; +import 'package:auto_gpt_flutter_client/models/benchmark_service/report_request_body.dart'; +import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart'; +import 'package:auto_gpt_flutter_client/models/benchmark_service/api_type.dart'; + +class BenchmarkService { + final RestApiUtility api; + + BenchmarkService(this.api); + + /// Generates a report using POST REST API at the /reports URL. + /// + /// [reportRequestBody] is a Map representing the request body for generating a report. + Future> generateReport( + ReportRequestBody reportRequestBody) async { + try { + return await api.post('reports', reportRequestBody.toJson(), + apiType: ApiType.benchmark); + } catch (e) { + throw Exception('Failed to generate report: $e'); + } + } + + /// Polls for updates using the GET REST API at the /updates?last_update_time=TIMESTAMP URL. + /// + /// [lastUpdateTime] is the UNIX UTC timestamp for last update time. + Future> pollUpdates(int lastUpdateTime) async { + try { + return await api.get('updates?last_update_time=$lastUpdateTime', + apiType: ApiType.benchmark); + } catch (e) { + throw Exception('Failed to poll updates: $e'); + } + } +}