mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
This commit enhances the `RestApiUtility` class to support multiple base URLs by incorporating an `ApiType` enum parameter in its methods. The changes include: 1. `_agentBaseUrl`: The base URL for the agent-related API calls. 2. `_benchmarkBaseUrl`: A hard-coded base URL for benchmark-related API calls. 3. `_getEffectiveBaseUrl`: A new private method that determines the effective base URL based on the given `ApiType`. All public methods (`get`, `post`, `getBinary`) have been updated to include an optional `ApiType` parameter, which defaults to `ApiType.agent`. Based on this parameter, `_getEffectiveBaseUrl` is called to decide the base URL for the HTTP request. This change allows for flexible API calls without the need to instantiate multiple `RestApiUtility` objects for different services.
64 lines
2.0 KiB
Dart
64 lines
2.0 KiB
Dart
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";
|
|
|
|
RestApiUtility(this._agentBaseUrl);
|
|
|
|
void updateBaseURL(String newBaseURL) {
|
|
_agentBaseUrl = newBaseURL;
|
|
}
|
|
|
|
String _getEffectiveBaseUrl(ApiType apiType) {
|
|
return apiType == ApiType.agent ? _agentBaseUrl : _benchmarkBaseUrl;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> 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<Map<String, dynamic>> post(
|
|
String endpoint, Map<String, dynamic> 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<Uint8List> 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');
|
|
}
|
|
}
|
|
}
|