use crate::error::GoogleResponse;
use crate::resources::common::ListResponse;
pub use crate::resources::topic::Topic;
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Notification {
id: String,
topic: Topic,
event_types: Option<Vec<String>>,
custom_attributes: Option<std::collections::HashMap<String, String>>,
payload_format: String,
object_name_prefix: Option<String>,
etag: String,
#[serde(rename = "selfLink")]
self_link: String,
kind: String,
}
#[derive(Debug, PartialEq, Default, serde::Serialize)]
pub struct NewNotification {
topic: String,
event_types: Option<Vec<String>>,
custom_attributes: Option<std::collections::HashMap<String, String>>,
payload_format: Option<PayloadFormat>,
object_name_prefix: Option<String>,
}
#[derive(Debug, PartialEq, serde::Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PayloadFormat {
JsonApiV1,
None,
}
impl Notification {
pub fn create(bucket: &str, new_notification: &NewNotification) -> Result<Self, crate::Error> {
let url = format!("{}/b/{}/notificationConfigs", crate::BASE_URL, bucket);
let result: GoogleResponse<Self> = crate::CLIENT
.post(&url)
.headers(crate::get_headers()?)
.json(new_notification)
.send()?
.json()?;
match result {
GoogleResponse::Success(s) => Ok(s),
GoogleResponse::Error(e) => Err(e.into()),
}
}
pub fn read(bucket: &str, notification: &str) -> Result<Self, crate::Error> {
let url = format!(
"{}/b/{}/notificationConfigs/{}",
crate::BASE_URL,
bucket,
notification
);
let result: GoogleResponse<Self> = crate::CLIENT;
.get(&url)
.headers(crate::get_headers()?)
.send()?
.json()?;
match result {
GoogleResponse::Success(s) => Ok(s),
GoogleResponse::Error(e) => Err(e.into()),
}
}
pub fn list(bucket: &str) -> Result<Vec<Self>, crate::Error> {
let url = format!("{}/v1/b/{}/notificationConfigs", crate::BASE_URL, bucket);
let result: GoogleResponse<ListResponse<Self>> = crate::CLIENT
.get(&url)
.headers(crate::get_headers()?)
.send()?
.json()?;
match result {
GoogleResponse::Success(s) => Ok(s.items),
GoogleResponse::Error(e) => Err(e.into()),
}
}
pub fn delete(bucket: &str, notification: &str) -> Result<(), crate::Error> {
let url = format!(
"{}/b/{}/notificationConfigs/{}",
crate::BASE_URL,
bucket,
notification
);
let response = crate::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();
}
}