http_toolkit 1.0.0+1 copy "http_toolkit: ^1.0.0+1" to clipboard
http_toolkit: ^1.0.0+1 copied to clipboard

Collection of missing features for the Dart HTTP package.

example/main.dart

// ignore_for_file: avoid_print

import 'package:http_toolkit/http_toolkit.dart';

void main() async {
  // 1. Create the client with middlewares and interceptors
  final client = Client(
    middlewares: [
      BaseUrlMiddleware(
        Uri.parse('https://jsonplaceholder.typicode.com'),
      ),
      const LoggerMiddleware(logBody: true),
      const BearerAuthMiddleware('super-secret-token'),
      const RetryMiddleware(
        maxRetries: 2,
        strategy: FixedDelayStrategy(Duration(milliseconds: 200)),
      ),
      const HeadersMiddleware(headers: {'User-Agent': 'HttpToolkit/1.0'}),
    ],
  );

  try {
    final response = await client.get(
      Uri.parse('/todos/1'),
    );

    if (response.isSuccess) {
      print('Done');
    }
  } on Exception catch (e) {
    print('Error: $e');
  }

  try {
    // This should trigger retries if the host is reachable but returns 5xx,
    // or if connection fails (e.g. invalid host usually throws immediately, let's see)
    // jsonplaceholder doesn't have 5xx easily triggered, so this might just fail or 404.
    // 404 does NOT trigger retry in our default logic (only exceptions unless configured).
    final response = await client.get(
      Uri.parse('/invalid-endpoint'),
    );
    print('Status: ${response.statusCode}');
  } on Exception catch (e) {
    print('Error caught: $e');
  }

  client.close();
}
2
likes
160
points
42
downloads

Publisher

unverified uploader

Weekly Downloads

Collection of missing features for the Dart HTTP package.

Repository (GitHub)
View/report issues

Topics

#network #http #middlewares #interceptors #utils

Documentation

API reference

License

MIT (license)

Dependencies

http

More

Packages that depend on http_toolkit