fetchToneAnalysis static method

Future<ToneAnalysis> fetchToneAnalysis(
  1. String query, {
  2. double? aggressiveness,
})

Call the fetchToneAnalysis function to retrieve the tone analysis as a ToneAnalysis object.

Implementation

static Future<ToneAnalysis> fetchToneAnalysis(String query,
    {double? aggressiveness}) async {
  if (aggressiveness != null) {
    return await _generateSample(aggressiveness);
  }

  if (query.isEmpty) {
    throw Exception("inputText is empty");
  }
  final response = await http.post(
    Uri.parse(baseUrl),
    headers: {'Authorization': 'Bearer $apiKey'},
    body: {"query": query},
  );
  if (response.statusCode >= 200 && response.statusCode < 400) {
    final result = response.body;
    try {
      final dto = ToneAnalysisDto.fromJson(jsonDecode(result));
      return dto.toEntity();
    } catch (e) {
      throw Exception("Failed to retrieve tone analysis");
    }
  } else {
    throw Exception("Failed to retrieve tone analysis");
  }
}