Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Add RestApiUtility Class for HTTP Requests

hunteraraujo0dcdaaf6413b
diff --git a/lib/utils/rest_api_utility.dart b/lib/utils/rest_api_utility.dart
new file mode 100644
index 0000000000..47e355a073
--- /dev/null
+++ b/lib/utils/rest_api_utility.dart
@@ -0,0 +1,40 @@
+import 'dart:convert';
+import 'package:http/http.dart' as http;
+
+class RestApiUtility {
+ final String baseUrl;
+
+ RestApiUtility(this.baseUrl);
+
+ Future<Map<String, dynamic>> get(String endpoint) async {
+ final response = await http.get(Uri.parse('$baseUrl/$endpoint'));
+ if (response.statusCode == 200) {
+ return json.decode(response.body);
+ } else {
+ throw Exception('Failed to load data');
+ }
+ }
+
+ Future<List<dynamic>> getList(String endpoint) async {
+ final response = await http.get(Uri.parse('$baseUrl/$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) async {
+ final response = await http.post(
+ Uri.parse('$baseUrl/$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');
+ }
+ }
+}