subscribe method

Future<DVWebhookSubscription> subscribe({
  1. required String url,
  2. required Set<String> events,
  3. required String signingSecret,
  4. String? tenant,
  5. String? id,
})

Subscribes url to events, after checking the address.

Throws DVWebhookAddressRefusedException for an address a delivery may not go to, and DVWebhookUndeclaredEventException for an event nobody declared.

Implementation

Future<DVWebhookSubscription> subscribe({
  required String url,
  required Set<String> events,
  required String signingSecret,
  String? tenant,
  String? id,
}) async {
  for (final String event in events) {
    if (!_events.containsKey(event)) {
      throw DVWebhookUndeclaredEventException(event);
    }
  }
  final Uri uri = Uri.parse(url);
  await _checkAddress(uri);
  final DVWebhookSubscription subscription = DVWebhookSubscription(
    id: id ?? _newId('whsub'),
    url: uri,
    events: Set<String>.unmodifiable(events),
    signingSecret: signingSecret,
    tenant: tenant,
    createdAt: clock(),
  );
  await _DVWebhookTables.insertSubscription(await _db(), subscription);
  return subscription;
}