sendRequest method
Sends a request by sending a notification with 'key' of
request.${request.reqId}.$domainNameSpace.$rpcsNameSpace.$baseNameSpace
with payload of jsonEncode([request].toJson())
to toAtSign
Implementation
Future<void> sendRequest(
{required String toAtSign, required AtRpcReq request}) async {
toAtSign = AtUtils.fixAtSign(toAtSign);
String requestRecordIDName =
'request.${request.reqId}.$domainNameSpace.$rpcsNameSpace';
var requestRecordID = AtKey()
..key = requestRecordIDName
..sharedBy = atClient.getCurrentAtSign()
..sharedWith = AtUtils.fixAtSign(toAtSign)
..namespace = baseNameSpace
..metadata = _defaultMetaData;
// Need to be able to receive responses from the atSigns we're sending requests to
allowList.add(toAtSign);
var requestJson = jsonEncode(request.toJson());
bool sent = false;
int delayMillis = 200;
for (int attemptNumber = 1;
attemptNumber <= maxSendAttempts && !sent;
attemptNumber++) {
try {
logger.info(
'Sending notification ${requestRecordID.toString()} with payload $requestJson');
await atClient.notificationService.notify(
NotificationParams.forUpdate(requestRecordID,
value: requestJson,
notificationExpiry: defaultNotificationExpiry),
checkForFinalDeliveryStatus: false,
waitForFinalDeliveryStatus: false);
sent = true;
logger.info('Notification ${requestRecordID.toString()} sent');
} catch (e) {
if (attemptNumber < maxSendAttempts) {
logger.warning(
'Exception $e sending request $request on attempt $attemptNumber - will retry in $delayMillis ms');
} else {
logger.severe(
'Exception $e sending request $request on attempt $attemptNumber - giving up');
}
await Future.delayed(Duration(milliseconds: delayMillis));
if (delayMillis < 5000) {
delayMillis *= 5;
}
}
}
}