dahttp

Creator: coderz1093

Last updated:

0 purchases

dahttp Image
dahttp Images

Languages

Categories

Add to Cart

Description:

dahttp

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
copied to clipboard
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');
});
}
copied to clipboard
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);
}
copied to clipboard
class DogCeo {
final String? url;

const DogCeo(this.url);

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

return DogCeo(data['message']);
}
}
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.