getProjectConfig method

Future<ProjectConfig> getProjectConfig(
  1. String projectId
)

Get project configuration

Implementation

Future<ProjectConfig> getProjectConfig(String projectId) async {
  final url = Uri.parse('$baseUrl/projects/$projectId');
  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 this project.',
    );
  } else if (response.statusCode == 404) {
    throw Exception('Project not found. Please check the project ID.');
  } else if (response.statusCode != 200) {
    throw Exception(
      'Failed to fetch project configuration: ${response.statusCode} ${response.body}',
    );
  }

  final json = jsonDecode(response.body) as Map<String, dynamic>;

  // Extract configuration from project response
  final configuration = json['configuration'] as Map<String, dynamic>?;

  // If configuration is null, create an empty one (project might not be configured yet)
  final configData = configuration ?? <String, dynamic>{};

  // Get domains
  final domains = await getProjectDomains(projectId);

  // Convert DomainConfig list to list of maps for fromJson
  final domainsJson = domains
      .map((d) => <String, dynamic>{
            'id': d.id,
            'host': d.host,
            'status': d.status,
            'isPrimary': d.isPrimary,
          })
      .toList();

  return ProjectConfig.fromJson({
    'projectId': projectId,
    ...configData,
    'domains': domainsJson,
  });
}