WebhookEvent.fromJson constructor

WebhookEvent.fromJson(
  1. JsonMap json
)

Decodes an event from its JSON payload, without verifying it.

Throws an InvalidPayloadException when json is not a valid payload. Unknown event types decode to UnknownWebhookEvent.

Implementation

factory fromJson(JsonMap json) {
  final type = json['type'];
  if (type is! String || type.isEmpty) {
    throw const InvalidPayloadException('The payload has no "type" string.');
  }
  try {
    final timestamp = decodeDateTime(json['timestamp']);
    final webhookId = json['webhook_id'] as String? ?? '';
    JsonMap data() => asJsonMap(json['data']);
    MessageEventData messageData() => MessageEventData.fromJson(data());
    return switch (type) {
      MessageReceptionEvent.eventType => MessageReceptionEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageDeliveredEvent.eventType => MessageDeliveredEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageTransientErrorEvent.eventType => MessageTransientErrorEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageFailedEvent.eventType => MessageFailedEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageBouncedEvent.eventType => MessageBouncedEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageSuppressedEvent.eventType => MessageSuppressedEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageOpenedEvent.eventType => MessageOpenedEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: messageData(),
        raw: json,
      ),
      MessageClickedEvent.eventType => MessageClickedEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: MessageClickedEventData.fromJson(data()),
        raw: json,
      ),
      SuppressionCreatedEvent.eventType => SuppressionCreatedEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: SuppressionCreatedEventData.fromJson(data()),
        raw: json,
      ),
      DomainDnsErrorEvent.eventType => DomainDnsErrorEvent(
        timestamp: timestamp,
        webhookId: webhookId,
        data: DomainDnsErrorEventData.fromJson(data()),
        raw: json,
      ),
      MessageRoutingEvent.eventType => MessageRoutingEvent(
        timestamp: timestamp,
        routeId: json['route_id'] as String? ?? '',
        data: MessageRoutingEventData.fromJson(data()),
        raw: json,
      ),
      _ => UnknownWebhookEvent(
        type: type,
        timestamp: timestamp,
        data: json['data'],
        raw: json,
      ),
    };
  } on WebhookException {
    rethrow;
  } on Object catch (e) {
    throw InvalidPayloadException(
      'Could not decode the "$type" payload: $e',
      cause: e,
    );
  }
}