use std::fmt::Debug;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::ucare::{encode_json, rest::Client, Result};
pub struct Service<'a> {
client: &'a Client,
}
pub fn new_svc(client: &Client) -> Service {
Service { client }
}
impl Service<'_> {
pub fn list(&self) -> Result<List> {
self.client
.call::<String, String, List>(Method::GET, format!("/webhooks/"), None, None)
}
pub fn create(&self, mut params: CreateParams) -> Result<Info> {
if params.is_active.is_none() {
params.is_active = Some(true);
}
let json = encode_json(¶ms)?;
self.client.call::<String, Vec<u8>, Info>(
Method::POST,
format!("/webhooks/"),
None,
Some(json),
)
}
pub fn update(&self, params: UpdateParams) -> Result<Info> {
let json = encode_json(¶ms)?;
self.client.call::<String, Vec<u8>, Info>(
Method::PUT,
format!("/webhooks/{}/", params.id),
None,
Some(json),
)
}
pub fn delete(&self, params: DeleteParams) -> Result<()> {
let json = encode_json(¶ms)?;
let res = self.client.call::<String, Vec<u8>, String>(
Method::DELETE,
format!("/webhooks/unsubscribe/"),
None,
Some(json),
);
if let Err(err) = res {
if !err.to_string().contains("EOF") {
return Err(err);
}
}
Ok(())
}
}
pub type List = Vec<Info>;
#[derive(Deserialize, Debug)]
pub struct Info {
pub id: i32,
pub created: String,
pub updated: String,
pub event: String,
pub target_url: String,
pub project: i32,
pub is_active: bool,
}
#[derive(Debug, Serialize)]
pub struct CreateParams {
pub event: Event,
pub target_url: String,
pub is_active: Option<bool>,
}
#[derive(Debug, Serialize)]
pub enum Event {
#[serde(rename = "file.uploaded")]
FileUploaded,
}
#[derive(Debug, Serialize)]
pub struct UpdateParams {
pub id: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub event: Option<Event>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_active: Option<bool>,
}
#[derive(Debug, Serialize)]
pub struct DeleteParams {
pub target_url: String,
}