getAgents method

Future<List<Agent>> getAgents()

Implementation

Future<List<Agent>> getAgents() async {
  final response = await http.get(
    Uri.parse('$baseUrl/getZohoAgents?workspaceId=${Constants.workspaceId}'),
    headers: {
      'Authorization': '$token',
      'Content-Type': 'application/json',
    },
  );
  print ('Agents+++++++ ${response}');
  print("workspace id ${workspaceId}");
  print("status code ${response.statusCode}");

  if (response.statusCode == 200 || response.statusCode == 201) {
    print("response body+_+_+_ ${response.body}");
    final Map<String, dynamic> data = json.decode(response.body);

    if (data['agents'] != null && data['agents'] is List) {
      final List<dynamic> agentsJson = data['agents'];

      if (agentsJson.isNotEmpty) {
        Constants.orgId = agentsJson[0]['org_id'] ?? '';
        print("org id: ${Constants.orgId}");
      }

      return agentsJson.map((json) => Agent.fromJson(json)).toList();
    } else {
      print('No agents found or invalid data structure');
      return [];
    }
  }
  else {
    print('Error status code: ${response.statusCode}');
    print('Error response body: ${response.body}');
    throw Exception('Failed to load agents');
  }
}