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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::error::GoogleResponse;
use crate::resources::common::ListResponse;
pub use crate::resources::topic::Topic;
/// A subscription to receive
/// [Pub/Sub notifications](https://cloud.google.com/storage/docs/pubsub-notifications).
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Notification {
/// The ID of the notification.
id: String,
/// The Pub/Sub topic to which this subscription publishes. Formatted as:
/// `'//pubsub.googleapis.com/projects/{project-identifier}/topics/{my-topic}'`.
topic: Topic,
/// If present, only send notifications about listed event types. If empty, send notifications
/// for all event types.
event_types: Option<Vec<String>>,
/// An optional list of additional attributes to attach to each Pub/Sub message published
/// for this notification subscription.
custom_attributes: Option<std::collections::HashMap<String, String>>,
/// The desired content of the Payload.
///
/// Acceptable values are:
/// * "JSON_API_V1"
/// * "NONE"
payload_format: String,
/// If present, only apply this notification configuration to object names that begin with this
/// prefix.
object_name_prefix: Option<String>,
/// HTTP 1.1 Entity tag for this subscription notification.
etag: String,
/// The canonical URL of this notification.
#[serde(rename = "selfLink")]
self_link: String,
/// The kind of item this is. For notifications, this is always `storage#notification`.
kind: String,
}
/// Use this struct to create new notifications.
#[derive(Debug, PartialEq, Default, serde::Serialize)]
pub struct NewNotification {
/// The Pub/Sub topic to which this subscription publishes. Formatted as:
/// `'//pubsub.googleapis.com/projects/{project-identifier}/topics/{my-topic}'`.
topic: String,
/// If present, only send notifications about listed event types. If empty, send notifications
/// for all event types.
event_types: Option<Vec<String>>,
/// An optional list of additional attributes to attach to each Pub/Sub message published
/// for this notification subscription.
custom_attributes: Option<std::collections::HashMap<String, String>>,
/// The desired content of the Payload.
payload_format: Option<PayloadFormat>,
/// If present, only apply this notification configuration to object names that begin with this
/// prefix.
object_name_prefix: Option<String>,
}
/// Various ways of having the response formatted.
#[derive(Debug, PartialEq, serde::Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PayloadFormat {
/// Respond with a format as specified in the Json API V1 documentation.
JsonApiV1,
/// Do not respond.
None,
}
impl Notification {
/// Creates a notification subscription for a given bucket.
pub fn create(bucket: &str, new_notification: &NewNotification) -> Result<Self, crate::Error> {
let url = format!("{}/b/{}/notificationConfigs", crate::BASE_URL, bucket);
let client = reqwest::blocking::Client::new();
let result: GoogleResponse<Self> = client
.post(&url)
.headers(crate::get_headers()?)
.json(new_notification)
.send()?
.json()?;
Ok(result?)
}
/// View a notification configuration.
pub fn read(bucket: &str, notification: &str) -> Result<Self, crate::Error> {
let url = format!(
"{}/b/{}/notificationConfigs/{}",
crate::BASE_URL,
bucket,
notification
);
let client = reqwest::blocking::Client::new();
let result: GoogleResponse<Self> = client
.get(&url)
.headers(crate::get_headers()?)
.send()?
.json()?;
Ok(result?)
}
/// Retrieves a list of notification subscriptions for a given bucket.}
pub fn list(bucket: &str) -> Result<Vec<Self>, crate::Error> {
let url = dbg!(format!("{}/v1/b/{}/notificationConfigs", crate::BASE_URL, bucket));
let client = reqwest::blocking::Client::new();
// let result: GoogleResponse<ListResponse<Self>> = client
// .get(&url)
// .headers(crate::get_headers()?)
// .send()?
// .json()?;
// Ok(result?.items)
let response = dbg!(client
.get(&url)
.headers(crate::get_headers()?)
.send()?
.text()?);
let result: ListResponse<Self> = dbg!(serde_json::from_str(&response)).unwrap();
Ok(result.items)
}
/// Permanently deletes a notification subscription.
pub fn delete(bucket: &str, notification: &str) -> Result<(), crate::Error> {
let url = format!(
"{}/b/{}/notificationConfigs/{}",
crate::BASE_URL,
bucket,
notification
);
let client = reqwest::blocking::Client::new();
let response = client.get(&url).headers(crate::get_headers()?).send()?;
if response.status().is_success() {
Ok(())
} else {
Err(crate::Error::Google(response.json()?))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create() {
let bucket = crate::read_test_bucket();
let topic = format!(
"//pubsub.googleapis.com/projects/{}/topics/{}",
crate::SERVICE_ACCOUNT.project_id,
"testing-is-important",
);
let new_notification = NewNotification {
topic,
payload_format: Some(PayloadFormat::JsonApiV1),
..Default::default()
};
Notification::create(&bucket.name, &new_notification).unwrap();
}
#[test]
fn read() {
let bucket = crate::read_test_bucket();
Notification::read(&bucket.name, "testing-is-important").unwrap();
}
#[test]
fn list() {
let bucket = crate::read_test_bucket();
Notification::list(&bucket.name).unwrap();
}
#[test]
fn delete() {
let bucket = crate::read_test_bucket();
let topic = format!(
"//pubsub.googleapis.com/projects/{}/topics/{}",
crate::SERVICE_ACCOUNT.project_id,
"testing-is-important",
);
let new_notification = NewNotification {
topic,
payload_format: Some(PayloadFormat::JsonApiV1),
..Default::default()
};
Notification::create(&bucket.name, &new_notification).unwrap();
Notification::delete(&bucket.name, "testing-is-important").unwrap();
}
}