getProjects method
Get all projects for the authenticated user
Implementation
Future<List<ProjectListItem>> getProjects() async {
final url = Uri.parse('$baseUrl/projects');
final headers = await _authHeaders();
final response = await _getWithRetry(url, headers);
if (response.statusCode == 401) {
throw Exception(
'Authentication failed. Please run "ulink login" to re-authenticate.',
);
} else if (response.statusCode == 403) {
throw Exception(
'Access forbidden. You may not have permission to access projects.',
);
} else if (response.statusCode == 404) {
throw Exception(
'Projects endpoint not found. Please check your API base URL: $baseUrl',
);
} else if (response.statusCode != 200) {
throw Exception(
'Failed to fetch projects: ${response.statusCode} ${response.body}',
);
}
final json = jsonDecode(response.body);
// Handle both array response and wrapped response
List<dynamic> projectsList;
if (json is List) {
projectsList = json;
} else if (json is Map && json['data'] is List) {
projectsList = json['data'] as List<dynamic>;
} else if (json is Map && json['projects'] is List) {
projectsList = json['projects'] as List<dynamic>;
} else {
throw Exception('Unexpected response format from projects endpoint');
}
return projectsList
.map((item) => ProjectListItem.fromJson(item as Map<String, dynamic>))
.toList();
}