diff --git a/lib/utils/rest_api_utility.dart b/lib/utils/rest_api_utility.dart new file mode 100644 index 00000000..47e355a0 --- /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> 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> 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> post( + String endpoint, Map 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'); + } + } +}