bond_network 0.0.9
bond_network: ^0.0.9 copied to clipboard
Network is a Bond package provides a convenient way to handle API response.
π Bond API Request β Simple Example #
This file demonstrates how to make basic API requests using the Bond API system. It includes examples for GET and POST requests with optional caching, body submission, and custom deserialization.
β GET Request with Caching #
final response = await GetBondApiRequest<MyModel>(dio, '/products')
.factory((json) => MyModel.fromJson(json))
.cache(
duration: const Duration(minutes: 5),
cacheKey: 'product-list',
cachePolicy: CachePolicy.cacheElseNetwork,
)
.execute();
π¨ POST Request with JSON Body #
final response = await BaseBondApiRequest<MyResponseModel>(dio, '/login', method: 'POST')
.body({
'email': 'user@example.com',
'password': 'secret',
})
.header({'Accept-Language': 'en'})
.factory((json) => MyResponseModel.fromJson(json))
.errorFactory((json) => ApiError.fromJson(json))
.execute();
πΎ Caching Custom Values from Response #
final response = await BaseBondApiRequest<MyResponseModel>(dio, '/login', method: 'POST')
.body({'email': 'me@example.com', 'password': 'password'})
.factory((json) => MyResponseModel.fromJson(json))
.cacheCustomKey('access_token', path: 'accessToken')
.execute();
// Retrieve later:
final token = await Cache.get<String>('access_token');
πͺͺ Streaming with cacheThenNetwork #
final stream = GetBondApiRequest<MyModel>(dio, '/profile')
.cache(
duration: Duration(minutes: 10),
cacheKey: 'profile-data',
cachePolicy: CachePolicy.cacheThenNetwork,
)
.factory((json) => MyModel.fromJson(json))
.streamExecute();
stream.listen((data) {
print('Profile: \${data.name}');
});
π Model Example #
class MyModel {
final int id;
final String name;
MyModel({required this.id, required this.name});
factory MyModel.fromJson(Map<String, dynamic> json) =>
MyModel(id: json['id'], name: json['name']);
}