invoke method

  1. @override
String invoke(
  1. String method,
  2. dynamic params,
  3. InvokeModuleCallback callback
)
override

Implementation

@override
String invoke(String method, params, InvokeModuleCallback callback) {
  Uri uri = _resolveUri(method);
  Map<String, dynamic> options = params;

  _handleError(Object error, StackTrace? stackTrace) {
    String errmsg = '$error';
    if (stackTrace != null) {
      errmsg += '\n$stackTrace';
    }
    callback(error: errmsg);
  }
  if (uri.host.isEmpty) {
    // No host specified in URI.
    _handleError('Failed to parse URL from $uri.', null);
  } else {
    getRequest(uri, options['method'], options['headers'], options['body'])
      .then((HttpClientRequest request) {
        if (_disposed) return Future.value(null);
        return request.close();
      })
      .then((HttpClientResponse? response) {
        if (response == null) return Future.value(null);

        StringBuffer contentBuffer = StringBuffer();

        response
          // @TODO: Consider binary format, now callback tunnel only accept strings.
          .transform(utf8.decoder)
          .listen(contentBuffer.write)
          ..onDone(() {
            // @TODO: response.headers not transmitted.
            callback(data: [EMPTY_STRING, response.statusCode, contentBuffer.toString()]);
          })
          ..onError(_handleError);
      })
      .catchError(_handleError);
  }

  return EMPTY_STRING;
}