sendLogRequest method

Future<Response<Uint8List>> sendLogRequest(
  1. Uri uri,
  2. XmlDocument requestData
)

Send the SOAP requestData to the given url endpoint.

Implementation

Future<Response<Uint8List>> sendLogRequest(
  Uri uri,
  XmlDocument requestData,
) async {
  Response<Uint8List> response;

  try {
    response = await dio.post<Uint8List>(
      uri.toString(),
      data: requestData.toString(),
      options: Options(
        responseType: ResponseType.bytes,
        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 jsonData = error.response?.data;
        final jsonMap = OnvifUtil.xmlToMap(jsonData as String);

        final envelope = Envelope.fromJson(jsonMap);

        if (envelope.body.hasFault) {
          // Log detailed fault for debugging, throw sanitized message
          loggy.error('ONVIF fault: ${envelope.body.fault}');
          throw Exception(
            'ONVIF operation failed. Please check device logs for details.',
          );
        }
        break;
    }

    // Log full error context, throw generic message to prevent info leakage
    loggy.error('Transport error: $error');
    throw Exception('Network communication failed. Please try again.');
  }

  return response;
}