use ghost_io_api::auth::admin::AdminApiKey;
use ghost_io_api::client::admin::GhostAdminClient;
use ghost_io_api::error::Result;
use ghost_io_api::models::post::{PostCreate, PostStatus, PostUpdate, TagRef};
#[tokio::main]
async fn main() -> Result<()> {
let url = require_env("GHOST_URL");
let raw_key = require_env("GHOST_ADMIN_KEY");
let key = AdminApiKey::new(raw_key)?;
let client = GhostAdminClient::new(&url, key)?;
println!(
"── Ghost Admin API: publish_post example {}",
"─".repeat(30)
);
println!("Site: {url}");
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let title = format!("publish_post example — {now}");
println!("\n[1/3] Creating draft post …");
let draft = client
.create_post(PostCreate {
title: title.clone(),
status: Some(PostStatus::Draft),
custom_excerpt: Some("Created by the ghost-io-api publish_post example.".to_string()),
tags: Some(vec![TagRef::by_name("ghost-io-api-example")]),
..Default::default()
})
.await?;
println!(" id : {}", draft.id);
println!(" title : {}", draft.title);
println!(" slug : {}", draft.slug);
println!(
" status: {}",
status_label(draft.status == PostStatus::Published)
);
let updated_at = draft.updated_at.clone().unwrap_or_default();
println!("\n[2/3] Publishing the draft …");
let published = client
.update_post(
&draft.id,
PostUpdate {
updated_at,
status: Some(PostStatus::Published),
..Default::default()
},
)
.await?;
println!(
" status: {}",
status_label(published.status == PostStatus::Published)
);
if let Some(url) = &published.url {
println!(" url : {url}");
}
println!("\n[3/3] Cleaning up — deleting post {} …", published.id);
client.delete_post(&published.id).await?;
println!(" Done.");
println!("\n{}", "─".repeat(60));
println!("── Finished {}", "─".repeat(49));
Ok(())
}
fn require_env(name: &str) -> String {
std::env::var(name).unwrap_or_else(|_| {
eprintln!("ERROR: environment variable {name} is required.");
eprintln!(" Set it before running this example.");
std::process::exit(1);
})
}
fn status_label(is_published: bool) -> &'static str {
if is_published {
"published"
} else {
"draft"
}
}