Portable HTTP client (Browser and Native support) with memory cache.
A simple usage example:
import 'package:mercury_client/mercury_client.dart';
main() async {
var client = HttpClient('http://gateway.your.domain/api-1');
try {
// Request with POST method:
// URL: http://gateway.your.domain/api-1/call-foo?var=123
// Content-Type: application/json
// Body: { 'content': 'any' }
var response = await client.post(
'call-foo',
parameters: {'var': '123'},
body: "{ 'content': 'any' }",
contentType: 'application/json',
);
if (response.isOK) {
print(response.body);
}
} catch (e) {
print('Error requesting URL: $e');
}
}
Using HttpCache
you can perform in-memory cached requests.
You can pass the parameter onStaleResponse
for the notification of a stale version
(a cached response that can be used while a new request is being performed):
import 'package:mercury_client/mercury_client.dart';
main() async {
// HTTP Cache with max memory of 16M and timeout of 5min:
var cache = HttpCache(
maxCacheMemory: 1024 * 1024 * 16, timeout: Duration(minutes: 5));
// The image element that will received the loaded data:
var img = ImageElement();
try {
// Request an image URL, that can be cached.
// If a stale version (already cached instance with timeout) exits,
// `onStaleResponse` will be called to indicate the existence
// of a cached response to be used while requesting the URL.
var response = await cache.getURL(
'http://host/path/to/base64/image.jpeg',
onStaleResponse: (staleResponse) {
var staleTime = staleResponse.instanceDateTime;
print('Stale image available: $staleTime');
img.src = 'data:image/jpeg;base64,${staleResponse.bodyAsString}';
},
);
if (response.isOK) {
img.src = 'data:image/jpeg;base64,${response.bodyAsString}';
}
} catch (e) {
print('Error requesting URL: $e');
}
}
Mercury is known to the Romans as Mercurius.
He is the god of financial gain, commerce, eloquence, messages, communication (including divination), travelers, boundaries, luck, trickery and thieves.
Please file feature requests and bugs at the issue tracker.
The official source code is hosted @ GitHub:
Please file feature requests and bugs at the issue tracker.
Any help from the open-source community is always welcome and needed:
Graciliano M. Passos: gmpassos@GitHub.
Dart free & open-source license.