gmpassos / mercury_client

A Simple Dart HTTP client (Web and Native support)
https://pub.dev/packages/mercury_client
BSD 3-Clause "New" or "Revised" License
5 stars 0 forks source link

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:

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:

Author

Graciliano M. Passos: gmpassos@GitHub.

License

Dart free & open-source license.