Skip to content

Commit

Permalink
Extend RestApiUtility to Support Multiple Base URLs via ApiType Enum
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hunteraraujo committed Sep 15, 2023
1 parent a97e0db commit 5ac4b38
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions frontend/lib/utils/rest_api_utility.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
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 _baseUrl;
String _agentBaseUrl;
final String _benchmarkBaseUrl = "http://127.0.0.1:8080";

RestApiUtility(this._baseUrl);
RestApiUtility(this._agentBaseUrl);

void updateBaseURL(String newBaseURL) {
_baseUrl = newBaseURL;
_agentBaseUrl = newBaseURL;
}

Future<Map<String, dynamic>> get(String endpoint) async {
final response = await http.get(Uri.parse('$_baseUrl/$endpoint'));
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 {
Expand All @@ -21,9 +29,11 @@ class RestApiUtility {
}

Future<Map<String, dynamic>> post(
String endpoint, Map<String, dynamic> payload) async {
String endpoint, Map<String, dynamic> payload,
{ApiType apiType = ApiType.agent}) async {
final effectiveBaseUrl = _getEffectiveBaseUrl(apiType);
final response = await http.post(
Uri.parse('$_baseUrl/$endpoint'),
Uri.parse('$effectiveBaseUrl/$endpoint'),
body: json.encode(payload),
headers: {"Content-Type": "application/json"},
);
Expand All @@ -34,9 +44,11 @@ class RestApiUtility {
}
}

Future<Uint8List> getBinary(String endpoint) async {
Future<Uint8List> getBinary(String endpoint,
{ApiType apiType = ApiType.agent}) async {
final effectiveBaseUrl = _getEffectiveBaseUrl(apiType);
final response = await http.get(
Uri.parse('$_baseUrl/$endpoint'),
Uri.parse('$effectiveBaseUrl/$endpoint'),
headers: {"Content-Type": "application/octet-stream"},
);

Expand Down

0 comments on commit 5ac4b38

Please sign in to comment.