fromContent static method

APIConfig? fromContent(
  1. String content, {
  2. String? type,
  3. bool autoIdentify = true,
  4. String? source,
})

Tries to construct an APIConfig from content.

  • If type is defined (JSON, YAMLor properties), forces decoding of the specified type format.
  • If autoIdentify is true it tries to detect the format to decode.

Implementation

static APIConfig? fromContent(String content,
    {String? type, bool autoIdentify = true, String? source}) {
  type ??= '';
  type = type.toLowerCase().trim();

  if (type == '*') {
    autoIdentify = true;
  }

  if (type == 'json' || type == 'js') {
    try {
      return APIConfig.fromJsonEncoded(content).._source = source;
    } catch (_) {
      throw FormatException('Error parsing JSON!');
    }
  }

  if (type == 'yaml' || type == 'yml') {
    try {
      return APIConfig.fromYAML(content).._source = source;
    } catch (_) {
      throw FormatException('Error parsing YAML!');
    }
  }

  if (type == 'properties' || type == 'prop') {
    try {
      return APIConfig.fromPropertiesEncoded(content).._source = source;
    } catch (_) {
      throw FormatException('Error parsing properties!');
    }
  }

  if (autoIdentify) {
    try {
      return APIConfig.fromJsonEncoded(content).._source = source;
    } catch (_) {}

    try {
      return APIConfig.fromYAML(content).._source = source;
    } catch (_) {}

    try {
      return APIConfig.fromPropertiesEncoded(content).._source = source;
    } catch (_) {}
  }

  return null;
}