mercury_client 2.2.3 copy "mercury_client: ^2.2.3" to clipboard
mercury_client: ^2.2.3 copied to clipboard

Portable HTTP client (Browser and Native support) with memory cache and support for methods GET, HEAD, POST, PUT, DELETE, PATCH and OPTIONS.

Mercury_Client #

pub package Null Safety Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

Portable HTTP client (Browser and Native support) with memory cache.

Methods: #

  • GET
  • HEAD
  • POST
  • PUT
  • DELETE
  • PATCH
  • OPTIONS.

Usage #

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');
  }
  
}

HttpCache Usage #

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 (mythology) #

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.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Source #

The official source code is hosted @ GitHub:

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Contribution #

Any help from the open-source community is always welcome and needed:

  • Found an issue?
  • Wish a feature?
    • Open a feature request with use cases.
  • Are you using and liking the project?
    • Promote the project: create an article, do a post or make a donation.
  • Are you a developer?
    • Fix a bug and send a pull request.
    • Implement a new feature.
    • Improve the Unit Tests.
  • Have you already helped in any way?
    • Many thanks from me, the contributors and everybody that uses this project!

Author #

Graciliano M. Passos: gmpassos@GitHub.

License #

Dart free & open-source license.

15
likes
160
pub points
75%
popularity

Publisher

unverified uploader

Portable HTTP client (Browser and Native support) with memory cache and support for methods GET, HEAD, POST, PUT, DELETE, PATCH and OPTIONS.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

charset, collection, enum_to_string, swiss_knife

More

Packages that depend on mercury_client