use crate::error::Error;
use crate::http::Method;
use crate::services::write_info;
pub use crate::generated::services::snippets::*;
impl<'a> Snippets<'a> {
pub async fn create(&self, name: &str, content: &str) -> Result<(), Error> {
let mut operation = self.client().form(Method::POST, "/snippets")?;
operation.info(write_info("Snippets", "CreateSnippet", "snippet", None));
operation.form(&snippet_fields(name, content));
self.client().send_unit(operation).await
}
pub async fn update(&self, snippet_id: i64, name: &str, content: &str) -> Result<(), Error> {
let mut operation = self
.client()
.form(Method::PATCH, &format!("/snippets/{snippet_id}"))?;
operation.info(write_info(
"Snippets",
"UpdateSnippet",
"snippet",
Some(snippet_id),
));
operation.form(&snippet_fields(name, content));
self.client().send_unit(operation).await
}
pub async fn delete(&self, snippet_id: i64) -> Result<(), Error> {
let mut operation = self
.client()
.form(Method::DELETE, &format!("/snippets/{snippet_id}"))?;
operation.info(write_info(
"Snippets",
"DeleteSnippet",
"snippet",
Some(snippet_id),
));
self.client().send_unit(operation).await
}
}
fn snippet_fields<'a>(name: &'a str, content: &'a str) -> Vec<(&'a str, &'a str)> {
let mut fields = Vec::new();
if !name.is_empty() {
fields.push(("snippet[name]", name));
}
if !content.is_empty() {
fields.push(("snippet[content]", content));
}
fields
}