sendRequest method
Future
sendRequest(
- String topic,
- String method,
- dynamic params, {
- int? id,
- int? ttl,
- EncodeOptions? encodeOptions,
override
Implementation
@override
Future sendRequest(
String topic,
String method,
dynamic params, {
int? id,
int? ttl,
EncodeOptions? encodeOptions,
}) async {
core.logger.t(
'pairing sendRequest, id: $id topic: $topic, method: $method, params: $params, ttl: $ttl',
);
final payload = JsonRpcUtils.formatJsonRpcRequest(
method,
params,
id: id,
);
// print('sending request: $payload');
final message = await core.crypto.encode(
topic,
payload,
options: encodeOptions,
);
if (message == null) {
return;
}
RpcOptions opts = MethodConstants.RPC_OPTS[method]!['req']!;
if (ttl != null) {
opts = opts.copyWith(ttl: ttl);
}
// print('adding payload to pending requests: ${payload['id']}');
final resp = PendingRequestResponse(completer: Completer());
resp.completer.future.catchError((err) {
// Catch the error so that it won't throw an uncaught error
});
pendingRequests[payload['id']] = resp;
// print('sent request');
await core.relayClient.publish(
topic: topic,
message: message,
ttl: opts.ttl,
tag: opts.tag,
);
// Get the result from the completer, if it's an error, throw it
try {
if (resp.error != null) {
throw resp.error!;
}
// print('checking if completed');
if (resp.completer.isCompleted) {
return resp.response;
}
return await resp.completer.future;
} catch (e) {
rethrow;
}
}