RemoteProviderEntry.fromJson constructor

RemoteProviderEntry.fromJson(
  1. Map<String, Object?> json
)

Implementation

factory RemoteProviderEntry.fromJson(Map<String, Object?> json) {
  final windowsRaw = json['contextWindows'];
  final pricingRaw = json['pricing'];
  final mediaRaw = json['media'];
  final windows = <String, int>{};
  if (windowsRaw is Map) {
    for (final entry in windowsRaw.entries) {
      final key = entry.key;
      final value = entry.value;
      if (key is String && value is num && value > 0) {
        windows[key] = value.round();
      }
    }
  }
  final pricing = <String, RemoteModelPricing>{};
  if (pricingRaw is Map) {
    for (final entry in pricingRaw.entries) {
      final key = entry.key;
      final value = entry.value;
      if (key is String && value is Map) {
        final input = value['input'];
        final output = value['output'];
        if (input is num && output is num) {
          pricing[key] = RemoteModelPricing(
            input: input.toDouble(),
            output: output.toDouble(),
          );
        }
      }
    }
  }
  final media = <String, List<String>>{};
  if (mediaRaw is Map) {
    for (final entry in mediaRaw.entries) {
      final key = entry.key;
      final value = entry.value;
      if (key is! String || value is! List) continue;
      final ids = <String>[];
      for (final item in value) {
        if (item is String && item.isNotEmpty) ids.add(item);
      }
      if (ids.isNotEmpty) media[key] = ids;
    }
  }
  return RemoteProviderEntry(
    contextWindows: windows,
    pricing: pricing,
    media: media,
  );
}