-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathnotification.dart
More file actions
77 lines (68 loc) · 2.44 KB
/
notification.dart
File metadata and controls
77 lines (68 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
part of '../google_cloud_storage.dart';
class Notification extends ServiceObject<NotificationMetadata>
with
GettableMixin<NotificationMetadata, Notification>,
DeletableMixin<NotificationMetadata>,
CreatableMixin<NotificationMetadata, Notification> {
final Bucket bucket;
Notification._(this.bucket, String id)
: super(
service: bucket.storage,
id: id,
metadata: NotificationMetadata()..id = id,
);
@override
Future<Notification> create(NotificationMetadata metadata) {
// Extract topic from metadata - it's required for createNotification
var topic = metadata.topic;
if (topic == null || topic.isEmpty) {
throw ArgumentError('Topic is required to create a notification.');
}
// Strip universe domain prefix if present (e.g., "//pubsub.googleapis.com/projects/...")
// This allows metadata from getMetadata() to be reused for create()
if (topic.startsWith('//pubsub.')) {
// Find the position after "//pubsub.{domain}/"
final domainEndIndex = topic.indexOf('/', 9); // Skip "//pubsub."
if (domainEndIndex != -1 && domainEndIndex < topic.length - 1) {
// Extract everything after "//pubsub.{domain}/"
topic = topic.substring(domainEndIndex + 1);
}
}
// Extract options from metadata
final options = CreateNotificationOptions(
customAttributes: metadata.customAttributes,
eventTypes: metadata.eventTypes,
objectNamePrefix: metadata.objectNamePrefix,
payloadFormat: metadata.payloadFormat,
);
// Delegate to bucket.createNotification, matching JS behavior
return bucket.createNotification(topic, options: options);
}
@override
Future<void> delete({DeleteOptions? options}) {
final api = ApiExecutor(bucket.storage);
return api.execute<void>((client) async {
try {
await client.notifications.delete(bucket.id, id);
} on ApiError catch (e) {
if (e.code == 404 && options?.ignoreNotFound == true) {
return;
}
rethrow;
}
});
}
@override
Future<NotificationMetadata> getMetadata({String? userProject}) async {
final api = ApiExecutor(bucket.storage);
return api.execute<NotificationMetadata>((client) async {
final metadata = await client.notifications.get(
bucket.id,
id,
userProject: userProject,
);
setInstanceMetadata(metadata);
return metadata;
});
}
}