handleResponseNotification method
Not part of API, but visibleForTesting. Receives 'response' notifications, and
- parses and validates
- logs warnings if deserialization or validation fails
- calls AtRpcCallbacks.handleResponse otherwise
Implementation
@visibleForTesting
Future<void> handleResponseNotification(AtNotification notification) async {
if (!allowList.contains(notification.from)) {
logger.info(
'Ignoring notification from non-allowed atSign ${notification.from} : $notification');
return;
}
// response key should be like:
// @toAtSign:<ack|nack|success|error>.<id>.<domainNameSpace>.<rpcsNameSpace>.<baseNameSpace>@fromAtSign
// strip off the prefix `@toAtSign:<ack|nack|success|error>.`
String requestKey = notification.key
.replaceFirst('${notification.to}:', '')
.replaceFirst(RegExp(r'(success|error|ack|nack)\.'), '');
// We should now have something like:
// <id>.<domainNameSpace>.<rpcsNameSpace>.<baseNameSpace>@fromAtSign
// We want to keep just the <id> and discard the rest
requestKey = requestKey.replaceAll(
'.$domainNameSpace.$rpcsNameSpace.$baseNameSpace${notification.from}',
'');
int requestId = -1;
try {
requestId = int.parse(requestKey);
} catch (e) {
logger.warning('Failed to get request ID from ${notification.key} - $e');
return;
}
late AtRpcResp response;
try {
response = AtRpcResp.fromJson(jsonDecode(notification.value!));
} catch (e, st) {
var message =
'Failed to deserialize AtRpcResp from ${notification.value}: $e';
logger.warning(message);
logger.warning(st);
return;
}
if (response.reqId != requestId) {
var message =
'Ignoring response: requestID from the notification key $requestId'
' does not match requestID from the response notification payload ${response.reqId}';
logger.warning(message);
return;
}
try {
await callbacks.handleResponse(response);
} catch (e, st) {
logger.warning(
'Exception $e from callbacks.handleResponse for response $response');
logger.warning(st);
}
}