59 lines
1.7 KiB
Dart
59 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class ApiService {
|
|
static const String _baseUrl = 'https://api.example.com';
|
|
|
|
static Future<Map<String, dynamic>> getRequest(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');
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> postRequest(String endpoint, Map<String, dynamic> body) async {
|
|
final response = await http.post(
|
|
Uri.parse('$_baseUrl/$endpoint'),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
},
|
|
body: jsonEncode(body),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return json.decode(response.body);
|
|
} else {
|
|
throw Exception('Failed to perform POST request');
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> putRequest(String endpoint, Map<String, dynamic> body) async {
|
|
final response = await http.put(
|
|
Uri.parse('$_baseUrl/$endpoint'),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
},
|
|
body: jsonEncode(body),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return json.decode(response.body);
|
|
} else {
|
|
throw Exception('Failed to perform PUT request');
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> deleteRequest(String endpoint) async {
|
|
final response = await http.delete(Uri.parse('$_baseUrl/$endpoint'));
|
|
|
|
if (response.statusCode == 200) {
|
|
return json.decode(response.body);
|
|
} else {
|
|
throw Exception('Failed to perform DELETE request');
|
|
}
|
|
}
|
|
}
|