dahttp 3.3.1 icon indicating copy to clipboard operation
dahttp: ^3.3.1 copied to clipboard

HTTP wrapper for Dart with integrated logger design to be used for REST APIs.

Dahttp #

HTTP wrapper for Dart with integrated logger design to be used for REST APIs.

Installation #

Add the following dependency to your pubspec.yaml:

dependencies: 
  dahttp: ^3.3.1

Example #

Future<void> main() async {
  final GetDogCeo getDogCeo = GetDogCeo();
  final HttpResult<DogCeo> result = await getDogCeo.call();

  // checking boolean properties
  if (result.success) {
    print('Success: ${result.data.url}');
  } else if (result.error) {
    print('Error: ${result.response.statusCode}');
  } else if (result.hasException) {
    print('Exception: ${result.exception}');
  }

  // passing callbacks (named parameters)
  result.handle(onSuccess: (DogCeo dog, Response response) {
    print('Success: ${dog.url}');
  }, onError: (Response response) {
    print('Error: ${response.statusCode}');
  }, onException: (dynamic exception) {
    print('Exception: $exception');
  });

  // passing callbacks (chained notation)
  result.onSuccess((DogCeo dog, Response response) {
    print('Success: ${dog.url}');
  }).onError((Response response) {
    print('Error: ${response.statusCode}');
  }).onException((dynamic exception) {
    print('Exception: $exception');
  });

  // passing callbacks (cascade notation)
  result
    ..onSuccess((DogCeo dog, Response response) {
      print('Success: ${dog.url}');
    })
    ..onError((Response response) {
      print('Error: ${response.statusCode}');
    })
    ..onException((dynamic exception) {
      print('Exception: $exception');
    });
}
class GetDogCeo extends ValuedHttpClient<DogCeo> {
  Future<HttpResult<DogCeo>> call() =>
      super.get('https://dog.ceo/api/breeds/image/random');

  @override
  DogCeo convert(Response response) => DogCeo.fromJson(response.body);
}
class DogCeo {
  final String? url;

  const DogCeo(this.url);

  static DogCeo fromJson(String json) {
    final dynamic data = jsonDecode(json);

    return DogCeo(data['message']);
  }
}
1
likes
130
pub points
37%
popularity

Publisher

verified publisher iconmauriciotogneri.com

HTTP wrapper for Dart with integrated logger design to be used for REST APIs.

Homepage

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

http

More

Packages that depend on dahttp