nbr 2.1.6 copy "nbr: ^2.1.6" to clipboard
nbr: ^2.1.6 copied to clipboard

Generic implementation of the network-bound-resource algorithm in Dart

example/main.dart

import 'dart:convert';
import 'dart:io';

import 'package:nbr/nbr.dart';

class SampleRepository
    extends NetworkBoundResource<MapEntry<String, List<String>>> {
  final Map<String, List<String>> _mockDB = {};
  final HttpClient _client = HttpClient();

  Future<void> fetchPackageDependencies(
    String package,
  ) async {
    await fetch<List<String>>(
      fetchFromAPI: () async => await _fetchDependencies(package),
      loadFromDB: () async {
        final versions = _mockDB[package];
        return versions != null ? MapEntry(package, versions) : null;
      },
      storeToDB: (entry) => _persistDependencies(entry.key, entry.value),
      shouldFetch: (entry) => entry == null || !_mockDB.containsKey(package),
      mapDTOToEntity: (versions) => MapEntry(package, versions),
    );
  }

  Future<List<String>> _fetchDependencies(String package) async {
    final url = Uri.https('pub.dev', 'packages/$package.json');
    final request = await _client.getUrl(url);
    final response = await request.close();

    if (response.statusCode != 200) {
      throw Exception('API returned ${response.statusCode}');
    }

    final body = await response
        .transform(const Utf8Decoder(allowMalformed: true))
        .reduce((previous, element) => previous + element);
    return ((json.decode(body) as Map<String, dynamic>)['versions']
            as List<dynamic>)
        .cast<String>();
  }

  void _persistDependencies(String package, List<String> versions) {
    _mockDB[package] = versions;
  }
}
2
likes
160
pub points
0%
popularity

Publisher

unverified uploader

Generic implementation of the network-bound-resource algorithm in Dart

Repository (GitHub)
View/report issues

Topics

#network-bound-resource #nbr #utility

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

equatable

More

Packages that depend on nbr