mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
Merge commit 'e5d30a9f6d0854e20049309333c2f637cd03025c' as 'frontend'
This commit is contained in:
35
frontend/lib/utils/rest_api_utility.dart
Normal file
35
frontend/lib/utils/rest_api_utility.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class RestApiUtility {
|
||||
String _baseUrl;
|
||||
|
||||
RestApiUtility(this._baseUrl);
|
||||
|
||||
void updateBaseURL(String newBaseURL) {
|
||||
_baseUrl = newBaseURL;
|
||||
}
|
||||
|
||||
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<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');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user