CockpitRemoteBridgeResponse.fromJson constructor

CockpitRemoteBridgeResponse.fromJson(
  1. Map<String, Object?> json
)

Implementation

factory CockpitRemoteBridgeResponse.fromJson(Map<String, Object?> json) {
  final jsonBody = json['jsonBody'];
  final requestId = json['requestId'];
  final statusCode = json['statusCode'];
  final contentType = json['contentType'];
  final bytesBase64 = json['bytesBase64'];
  if (requestId is! String || requestId.isEmpty) {
    throw const FormatException(
      'Bridge response field "requestId" must be a non-empty string.',
    );
  }
  if (statusCode is! int) {
    throw const FormatException(
      'Bridge response field "statusCode" must be an integer.',
    );
  }
  if (statusCode < 100 || statusCode > 599) {
    throw const FormatException(
      'Bridge response field "statusCode" must be an HTTP status code from 100 to 599.',
    );
  }
  if (contentType != null && contentType is! String) {
    throw const FormatException(
      'Bridge response field "contentType" must be a string.',
    );
  }
  if (jsonBody != null && jsonBody is! Map<Object?, Object?>) {
    throw const FormatException(
      'Bridge response field "jsonBody" must be a JSON object.',
    );
  }
  if (bytesBase64 != null && bytesBase64 is! String) {
    throw const FormatException(
      'Bridge response field "bytesBase64" must be a string.',
    );
  }
  if (jsonBody != null && bytesBase64 != null) {
    throw const FormatException(
      'Bridge response must not contain both "jsonBody" and "bytesBase64".',
    );
  }
  return CockpitRemoteBridgeResponse(
    requestId: requestId,
    statusCode: statusCode,
    contentType: contentType as String? ?? 'application/json',
    jsonBody: jsonBody == null
        ? null
        : Map<String, Object?>.from(jsonBody as Map<Object?, Object?>),
    bytesBase64: bytesBase64 as String?,
  );
}