cancelable_http_client 1.1.5 copy "cancelable_http_client: ^1.1.5" to clipboard
cancelable_http_client: ^1.1.5 copied to clipboard

A cancelable HTTP client is a wrapper over `http.Client` that allows to cancel a request or the operation of receiving data from the response or sending data via request.

example/example.dart

import 'dart:async';
import 'dart:io';

import 'package:cancelable_http_client/cancelable_http_client.dart';

Future<void> main(List<String> args) async {
  final server = await HttpServer.bind('localhost', 8080);
  final serverUrl = 'http://${server.address.host}:${server.port}';

  unawaited(() async {
    await for (HttpRequest request in server) {
      final response = request.response;
      final url = request.requestedUri;
      _server('Begin request: $url');
      try {
        response.bufferOutput = false;
        Stream<List<int>> gen() async* {
          const count = 100;
          _server('Total chunks: $count');
          var processed = 0;
          try {
            for (var i = 0; i < count; i++, processed++) {
              _server('Send chunk: $i');
              yield [i];
              await Future<void>.delayed(Duration(seconds: 1));
            }
          } finally {
            _server('Chunks sent: $processed of $count');
          }
        }

        await response.addStream(gen());
      } catch (e) {
        _server('Error: $e');
      } finally {
        _server('End request: $url');
        await response.close();
      }
    }
  }());

  final url = Uri.parse(serverUrl);
  const timeout = 2500;
  final watch = Stopwatch();
  watch.start();
  final cts = CancellationTokenSource(Duration(milliseconds: timeout));
  final token = cts.token;
  final client = CancelableClient(token);
  try {
    _client('Send request with timeout $timeout ms');
    final response = await client.get(url);
    cts.cancelAfter(null);
    final bodyBytes = response.bodyBytes;
    _client('Received response: $bodyBytes');
  } catch (e) {
    watch.stop();
    _client('Error: $e at ${watch.elapsedMilliseconds}');
  }

  _client('Elapsed ${watch.elapsedMilliseconds} ms');
  await Future<void>.delayed(Duration(seconds: 5));
  await server.close();
}

void _client(String text) => print('Client: $text');

void _server(String text) => print('Server: $text');
0
likes
0
points
80
downloads

Publisher

unverified uploader

Weekly Downloads

A cancelable HTTP client is a wrapper over `http.Client` that allows to cancel a request or the operation of receiving data from the response or sending data via request.

Repository (GitHub)
View/report issues

Topics

#cancellation #http #http-client #network #streams

License

unknown (license)

Dependencies

http, multitasking

More

Packages that depend on cancelable_http_client