start method

Future<String> start({
  1. Duration timeout = const Duration(minutes: 5),
})

Starts the server and returns the URL OpenRouter should redirect to.

timeout caps how long the server waits for the callback; after it elapses the server closes and waitForCode completes with null.

Implementation

Future<String> start({Duration timeout = const Duration(minutes: 5)}) async {
  await _closeExisting();
  _codeCompleter = Completer<String?>();

  _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
  final server = _server!;
  final url = 'http://127.0.0.1:${server.port}/';

  _timeoutTimer = Timer(timeout, () {
    if (_codeCompleter case final c? when !c.isCompleted) {
      c.complete(null);
    }
    unawaited(close());
  });

  server.listen(_handleRequest, onDone: _onDone);

  return url;
}