sendRequest method

Future<Envelope> sendRequest(
  1. Uri uri,
  2. XmlDocument requestData
)

Send the SOAP requestData to the given url endpoint.

Implementation

Future<Envelope> sendRequest(Uri uri, XmlDocument requestData) async {
  Response response;

  try {
    response = await dio.post(uri.toString(),
        data: requestData.toString(),
        options: Options(headers: {
          // Headers.contentTypeHeader: 'text/xml; charset=utf-8',
          Headers.contentTypeHeader: 'application/soap+xml; charset=utf-8',
        }));
  } on DioException catch (error) {
    switch (error.response?.statusCode) {
      case 500:
      case 400:
        loggy.error('ERROR RESPONSE:\n${error.response?.data}');

        final jsonMap = OnvifUtil.xmlToMap(error.response?.data);

        final envelope = Envelope.fromJson(jsonMap);

        if (envelope.body.hasFault) {
          throw Exception('Error code: ${envelope.body.fault}');
        }
        break;
    }

    throw Exception(error);
  }

  return Envelope.fromXmlString(response.data);
}