network 0.10.0+1 network: ^0.10.0+1 copied to clipboard
Package including hooks for easy works with http package in dart
Package including hooks for easy works with http package in dart.
Key Features • Getting Started • Todo • Credits
Key Features #
- Simple hooks for all http methods
- Interceptors witch handling requests, responses and errors
- Decode json from response (
Response.json()
/Response.asMap
/Response.asList
) - Browser support
Getting Started #
import 'package:network/network.dart';
// hooks
'https://jsonplaceholder.typicode.com/comments'.get(...);
'https://jsonplaceholder.typicode.com/comments'.post(...);
// Client
final client = NetworkClient();
client.get(...);
client.post(...);
client.close();
Get request:
final restResponse = await 'https://jsonplaceholder.typicode.com/todos/1'.get(
queryParameters: {'name': 'value'}
);
// Auto decode json response
print(restResponse.asMap['title']);
// Also available lists
print(restResponse.asList[0]);
// Or object
print(restResponse.json()); // returns Object
// Or pure string
print(restResponse.asString);
Get request Blob:
final blobResponse = await 'https://via.placeholder.com/300'.get();
print(blobResponse.bytes);
Post request to API:
final postResponse = await 'https://jsonplaceholder.typicode.com/todos'.post(
body: {'title': 'test'},
);
print(postResponse.asMap['id']);
Handle exceptions:
try {
await 'https://jsonplaceholder.typicode.com/todos/202'.get();
} on NetworkException catch (error) {
print('Network exception called, status code: ${error.code}');
}
Add middleware:
import 'package:network/interceptors.dart';
NetworkSettings().interceptors.addAll([
defaultErrors(),
Interceptor(
onRequest: (request) {
print('request on: ${request.url}');
return request.copyWith(
url: request.url + '/additional-link',
);
},
onResponse: (response) {
print('response status code: ${response.statusCode}');
return response;
},
onError: (error) {
if (error is UnauthorizedException) {
signOut();
}
return error;
},
)
]);
// Also can add middleware specify for client:
final client = Network()..interceptors.add(...);
client.get(...) // Usages global (NetworkSettings().interceptors) and local (client) interceptors
And... all api docs available here
Credits #
This software uses the following open source packages: