public FlagEvaluation getEvaluation()

in sdk-java/src/main/java/com/spotify/confidence/Confidence.java [122:198]


  public <T> FlagEvaluation<T> getEvaluation(String key, T defaultValue) {
    try {
      final FlagPath flagPath = getPath(key);
      final String requestFlagName = "flags/" + flagPath.getFlag();
      final ResolveFlagsResponse response = resolveFlags(requestFlagName).get();
      if (response.getResolvedFlagsList().isEmpty()) {
        final String errorMessage =
            String.format("No active flag '%s' was found", flagPath.getFlag());
        log.warn(errorMessage);
        return new FlagEvaluation<>(
            defaultValue, "", "ERROR", ErrorType.FLAG_NOT_FOUND, errorMessage);
      }

      final ResolvedFlag resolvedFlag = response.getResolvedFlags(0);
      final String clientKey = client().clientSecret;
      final String flag = resolvedFlag.getFlag();
      final String context = URLEncoder.encode(getContext().toString(), StandardCharsets.UTF_8);
      final String logMessage =
          String.format(
              "See resolves for '%s' in Confidence: "
                  + "https://app.confidence.spotify.com/flags/resolver-test?client-key=%s&flag=flags/%s&context=%s",
              flag, clientKey, flag, context);
      log.debug(logMessage);
      if (!requestFlagName.equals(resolvedFlag.getFlag())) {
        final String errorMessage =
            String.format(
                "Unexpected flag '%s' from remote",
                resolvedFlag.getFlag().replaceFirst("^flags/", ""));
        log.warn(errorMessage);
        return new FlagEvaluation<>(
            defaultValue, "", "ERROR", ErrorType.INTERNAL_ERROR, errorMessage);
      }
      if (resolvedFlag.getVariant().isEmpty()) {
        final String errorMessage =
            String.format(
                "The server returned no assignment for the flag '%s'. Typically, this happens "
                    + "if no configured rules matches the given evaluation context.",
                flagPath.getFlag());
        log.debug(errorMessage);
        return new FlagEvaluation<>(defaultValue, "", resolvedFlag.getReason().toString());
      } else {
        final ConfidenceValue confidenceValue;
        confidenceValue =
            getValueForPath(
                flagPath.getPath(),
                ConfidenceTypeMapper.from(resolvedFlag.getValue(), resolvedFlag.getFlagSchema()));

        // regular resolve was successful
        return new FlagEvaluation<>(
            getTyped(confidenceValue, defaultValue),
            resolvedFlag.getVariant(),
            resolvedFlag.getReason().toString());
      }
    } catch (IllegalValuePath | ValueNotFound e) {
      log.warn(e.getMessage());
      return new FlagEvaluation<>(
          defaultValue, "", "ERROR", ErrorType.INVALID_VALUE_PATH, e.getMessage());
    } catch (IncompatibleValueType | IllegalValueType e) {
      log.warn(e.getMessage());
      return new FlagEvaluation<>(
          defaultValue, "", "ERROR", ErrorType.INVALID_VALUE_TYPE, e.getMessage());
    } catch (StatusRuntimeException e) {
      log.warn(e.getMessage());
      return new FlagEvaluation<>(
          defaultValue, "", "ERROR", ErrorType.NETWORK_ERROR, e.getMessage());
    } catch (Exception e) {
      // catch all for any runtime exception
      if (e.getCause() instanceof StatusRuntimeException) {
        log.warn(e.getMessage());
        return new FlagEvaluation<>(
            defaultValue, "", "ERROR", ErrorType.NETWORK_ERROR, e.getMessage());
      }
      log.warn(e.getMessage());
      return new FlagEvaluation<>(
          defaultValue, "", "ERROR", ErrorType.INTERNAL_ERROR, e.getMessage());
    }
  }