handle method

Future<XmlDocument> handle(
  1. XmlDocument document
)

Marshalls the data from XML to Dart types, and then dispatches the function, and marshals the return value back into the XMLRPC format

Implementation

Future<XmlDocument> handle(XmlDocument document) async {
  String methodName;
  final params = <Object?>[];
  try {
    final methodCall = document.findElements('methodCall').first;
    methodName = methodCall.findElements('methodName').first.text;
    var paramsElements = methodCall.findElements('params');
    if (paramsElements.isNotEmpty) {
      final args = paramsElements.first.findElements('param');
      for (final arg in args) {
        params.add(
          decode(getValueContent(arg.findElements('value').first), codecs),
        );
      }
    }
  } catch (e) {
    throw XmlRpcRequestFormatException(e);
  }

  // check method has target
  if (!methods.containsKey(methodName)) {
    throw XmlRpcMethodNotFoundException(methodName);
  }

  // execute call
  Object? result;
  try {
    result = await Function.apply(methods[methodName]!, params);
  } catch (e) {
    throw XmlRpcCallException(e);
  }

  // encode result
  XmlNode encodedResult;
  try {
    encodedResult = encode(result, codecs);
  } catch (e) {
    throw XmlRpcResponseEncodingException(e);
  }
  return XmlDocument([
    XmlProcessing('xml', 'version="1.0"'),
    XmlElement(XmlName('methodResponse'), [], [
      XmlElement(XmlName('params'), [], [
        XmlElement(XmlName('param'), [], [
          XmlElement(XmlName('value'), [], [encodedResult])
        ]),
      ])
    ])
  ]);
}