59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
use reqwest::Client;
|
|
use std::error::Error;
|
|
|
|
pub async fn create_pad(client: &Client, hedgedoc_url: &str) -> Result<String, Box<dyn Error>> {
|
|
let res = client.get(format!("{}/new", hedgedoc_url)).send().await?;
|
|
|
|
if res.status().is_success() {
|
|
let pad_url = res.url().to_string();
|
|
Ok(pad_url)
|
|
} else {
|
|
Err(format!("Failed to create pad: {} - {}", res.status(), res.text().await?).into())
|
|
}
|
|
}
|
|
|
|
pub async fn import_note(client: &Client, content: String, note_id: Option<&str>, hedgedoc_url: &str) -> Result<String, Box<dyn Error>> {
|
|
let post_url = match note_id {
|
|
Some(id) => format!("{}/new/{}", hedgedoc_url, id),
|
|
None => format!("{}/new", hedgedoc_url),
|
|
};
|
|
|
|
let content_parsed: String = content.clone().to_string();
|
|
|
|
println!("Content Parsed:");
|
|
println!("{}", content_parsed);
|
|
let res = client.post(&post_url)
|
|
.header("Content-Type", "text/markdown")
|
|
.body(content_parsed)
|
|
.send()
|
|
.await?;
|
|
|
|
if res.status().is_success() {
|
|
let final_url = res.url().to_string();
|
|
Ok(final_url)
|
|
} else {
|
|
Err(format!("Failed to import note: {} - {}", res.status(), res.text().await?).into())
|
|
}
|
|
}
|
|
/*
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn Error>> {
|
|
let client = Client::new();
|
|
|
|
match create_pad(&client).await {
|
|
Ok(pad_url) => {
|
|
println!("Pad created successfully at URL: {}", pad_url);
|
|
|
|
let pad_id = pad_url.trim_start_matches(&format!("{}/", HEDGEDOC_URL));
|
|
match import_note(&client, TEMPLATE_CONTENT, Some(pad_id)).await {
|
|
Ok(_) => println!("Pad updated successfully with template content."),
|
|
Err(e) => println!("Failed to update pad: {}", e),
|
|
}
|
|
}
|
|
Err(e) => println!("Failed to create pad: {}", e),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
*/ |