fetchScore method

Future<ScoreResult> fetchScore(
  1. String visitorId
)

Implementation

Future<ScoreResult> fetchScore(String visitorId) async {
  String envId = Flagship.sharedInstance().envId ?? "";
  const fetchEmotionAIScoreURL = Endpoints.fetchEmotionAIScoreURL;

  var urlString = fetchEmotionAIScoreURL
      .replaceFirst("%s", envId)
      .replaceFirst("%s", visitorId);

  try {
    // Perform the GET request
    final response = await Flagship.sharedInstance()
        .getConfiguration()
        ?.decisionManager
        .service
        .sendHttpRequest(RequestType.Get, urlString, {}, null);

    if (response?.statusCode == 204) {
      // The server returned "no content"
      Flagship.logger(Level.INFO, "Score not found");
      return ScoreResult(null, 204);
    } else if (response?.statusCode == 200) {
      final Map<String, dynamic> responseBody =
          json.decode(response?.body ?? "");
      final Map<String, dynamic>? eaiMap = responseBody["eai"];
      // Looking for a "score" inside "eas"
      final String? score = eaiMap?["eas"];
      if (score != null) {
        Flagship.logger(Level.INFO,
            "The emotionAI score for $visitorId is: <<<< $score >>>>");
        DataUsageTracking.sharedInstance().processEaiGetScore(
            CriticalPoints.EMOTIONS_AI_SCORE.name, null, response, score);
        return ScoreResult(score, 200);
      } else {
        Flagship.logger(
            Level.INFO, "No score found from the server response.");
        return ScoreResult(null, 200);
      }
    } else {
      Flagship.logger(
          Level.INFO, "Error on fetching score: ${response?.statusCode}");

      DataUsageTracking.sharedInstance().processEaiGetScore(
          CriticalPoints.EMOTIONS_AI_SCORE_ERROR.name, null, response, null);
      return ScoreResult(null, response?.statusCode ?? 0);
    }
  } catch (error) {
    Flagship.logger(
        Level.INFO, "Exception occurred while fetching score: $error");
    return ScoreResult(null, -1);
  }
}