32 lines
944 B
Dart
32 lines
944 B
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '/core/constants.dart';
|
|
import '/data/models/contact_model.dart';
|
|
import '/domain/services/api_service.dart';
|
|
|
|
class ContactRepository {
|
|
final http.Client _client = http.Client();
|
|
final ApiService _apiService = ApiService();
|
|
|
|
Future<List<Contact>> fetchContacts() async {
|
|
final token = await _apiService.getAccessToken();
|
|
if (token == null) {
|
|
throw Exception('No access token');
|
|
}
|
|
|
|
final response = await _client.get(
|
|
Uri.http(AppConstants.baseUrl, 'users/all'),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = jsonDecode(utf8.decode(response.bodyBytes));
|
|
return data.map((json) => Contact.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Failed to load contacts');
|
|
}
|
|
}
|
|
} |