File size: 2,986 Bytes
b225a21 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import 'dart:convert';
import 'dart:typed_data';
import 'package:auto_gpt_flutter_client/models/benchmark/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";
final String _leaderboardBaseUrl = "https://leaderboard.agpt.co";
RestApiUtility(this._agentBaseUrl);
void updateBaseURL(String newBaseURL) {
_agentBaseUrl = newBaseURL;
}
String _getEffectiveBaseUrl(ApiType apiType) {
switch (apiType) {
case ApiType.agent:
return _agentBaseUrl;
case ApiType.benchmark:
return _benchmarkBaseUrl;
case ApiType.leaderboard:
return _leaderboardBaseUrl;
default:
return _agentBaseUrl;
}
}
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 {
// TODO: We are bubbling up the full response to show better errors on the UI.
// Let's put some thought into how we would like to structure this.
throw response;
}
}
Future<Map<String, dynamic>> put(
String endpoint, Map<String, dynamic> payload,
{ApiType apiType = ApiType.agent}) async {
final effectiveBaseUrl = _getEffectiveBaseUrl(apiType);
final response = await http.put(
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 update data with PUT request');
}
}
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');
}
}
}
|