import 'dart:convert'; import 'dart:typed_data'; import 'package:auto_gpt_flutter_client/models/benchmark_service/api_type.dart'; import 'package:http/http.dart' as http; class RestApiUtility { String _agentBaseUrl; final String _benchmarkBaseUrl = "http://127.0.0.1:8080/ap/v1"; RestApiUtility(this._agentBaseUrl); void updateBaseURL(String newBaseURL) { _agentBaseUrl = newBaseURL; } String _getEffectiveBaseUrl(ApiType apiType) { return apiType == ApiType.agent ? _agentBaseUrl : _benchmarkBaseUrl; } Future> get(String endpoint, {ApiType apiType = ApiType.agent}) async { final effectiveBaseUrl = _getEffectiveBaseUrl(apiType); final response = await http.get(Uri.parse('$effectiveBaseUrl/$endpoint')); if (response.statusCode == 200) { return json.decode(response.body); } else { throw Exception('Failed to load data'); } } Future> post( String endpoint, Map payload, {ApiType apiType = ApiType.agent}) async { final effectiveBaseUrl = _getEffectiveBaseUrl(apiType); final response = await http.post( Uri.parse('$effectiveBaseUrl/$endpoint'), body: json.encode(payload), headers: {"Content-Type": "application/json"}, ); if (response.statusCode == 200 || response.statusCode == 201) { return json.decode(response.body); } else { throw Exception('Failed to post data'); } } Future getBinary(String endpoint, {ApiType apiType = ApiType.agent}) async { final effectiveBaseUrl = _getEffectiveBaseUrl(apiType); final response = await http.get( Uri.parse('$effectiveBaseUrl/$endpoint'), headers: {"Content-Type": "application/octet-stream"}, ); if (response.statusCode == 200) { return response.bodyBytes; } else if (response.statusCode == 404) { throw Exception('Resource not found'); } else { throw Exception('Failed to load binary data'); } } }