danela 1.0.1 copy "danela: ^1.0.1" to clipboard
danela: ^1.0.1 copied to clipboard

Dart Abstract Network Layer (Danela)

example/danela_example.dart

import 'package:danela/danela.dart';
import 'package:dio/dio.dart';

String fromJson(Map<String, dynamic> json) {
  final setup = json['setup'];
  final punch = json['punchline'];
  return '$setup\n$punch';
}

String fromError(Object e) => '$e';

void main() async {
  // Create a request to the Joke API
  final request = const Request(
    url: 'https://official-joke-api.appspot.com/random_joke',
  );
  // Define a mapper producing a String from a Dio's Response
  // (since we are using Dio here)
  final mapper = const RequestMapper<Response, String>(
    mapJson: fromJson,
    onError: fromError,
  );
  // Create the Dio instance
  final dio = Dio();
  // Now we define the Gateway
  final gateway = DioGateway(
    dio: dio,
    request: request,
    mapper: mapper,
  );
  // Safely fetch the String result
  print(await gateway.run());

  // Now, let's add caching
  final repository = DefaultRepository(gateway: gateway);
  // We're good! Cache usage is set to [true] by default in
  // the [DefaultRepository]
  print(await repository.run(useCache: true));
}
1
likes
140
points
53
downloads

Publisher

unverified uploader

Weekly Downloads

Dart Abstract Network Layer (Danela)

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

dio

More

Packages that depend on danela