ts-tat/lib/services/apiService.dart
2025-04-02 15:47:15 +05:30

73 lines
1.7 KiB
Dart

import 'dart:convert';
import 'package:frontend/utils/auth_utils.dart';
import 'package:http/http.dart' as http;
import '../../config/apiUrl.dart';
class ApiService {
Future<List<dynamic>> fetchCountryList() async {
final String apiUrldata = '$apiUrl/api/getcountryMaster';
final token = await getToken();
if (token == null) {
throw Exception('Token not found. Please log in.');
}
final response = await http.get(
Uri.parse(apiUrldata),
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
);
if (response.statusCode == 200) {
try {
final data = json.decode(response.body);
print("Country - $data");
if (!data.containsKey('data') || data['data'] is! List) {
throw Exception("Invalid response format: 'data' field is missing or not a List");
}
return data['data'];
} catch (e) {
throw Exception('Error parsing response: $e');
}
} else {
throw Exception('Failed to load country list');
}
}
Future<List<dynamic>> fetchUsers() async {
final String apiUrlData = '$apiUrl/api/users';
final String? token = await getToken();
print("Fetch Users");
print("TOEKRWE: $token");
if (token == null) {
throw Exception('Token not found. Please log in.');
}
final response = await http.get(
Uri.parse(apiUrlData),
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
);
if (response.statusCode == 200) {
final data = json.decode(response.body);
return data['data']; // Returning raw JSON list
} else {
throw Exception('Failed to load users');
}
}
}