use super::client::DiscourseClient;
use super::error::http_error;
use super::models::{CreatePostResponse, TopicResponse};
use anyhow::{Context, Result, anyhow};
use serde_json::Value;
impl DiscourseClient {
pub fn fetch_topic(&self, topic_id: u64, include_raw: bool) -> Result<TopicResponse> {
let path = if include_raw {
format!("/t/{}.json?include_raw=1", topic_id)
} else {
format!("/t/{}.json", topic_id)
};
let response = self.get(&path)?;
let status = response.status();
let text = response.text().context("reading topic response body")?;
if !status.is_success() {
return Err(http_error("topic request", status, &text));
}
let body: TopicResponse = serde_json::from_str(&text).context("parsing topic json")?;
Ok(body)
}
pub fn fetch_post_raw(&self, post_id: u64) -> Result<Option<String>> {
let path = format!("/posts/{}.json?include_raw=1", post_id);
let response = self.get(&path)?;
let status = response.status();
let text = response.text().context("reading post response body")?;
if !status.is_success() {
return Err(http_error("post request", status, &text));
}
let value: Value = serde_json::from_str(&text).context("parsing post response")?;
Ok(value
.get("raw")
.and_then(|raw| raw.as_str())
.map(|raw| raw.to_string()))
}
pub fn update_post(&self, post_id: u64, raw: &str) -> Result<()> {
let payload = [("post[raw]", raw)];
let response = self
.put(&format!("/posts/{}.json", post_id))?
.form(&payload)
.send()
.context("updating post")?;
if !response.status().is_success() {
return Err(anyhow!("update post failed with {}", response.status()));
}
Ok(())
}
pub fn create_topic(&self, category_id: u64, title: &str, raw: &str) -> Result<u64> {
let payload = [
("title", title),
("raw", raw),
("category", &category_id.to_string()),
];
let response = self
.post("/posts.json")?
.form(&payload)
.send()
.context("creating topic")?;
let status = response.status();
let text = response.text().context("reading create response body")?;
if !status.is_success() {
return Err(http_error("create topic request", status, &text));
}
let body: CreatePostResponse =
serde_json::from_str(&text).context("parsing create topic response")?;
Ok(body.topic_id)
}
pub fn create_post(&self, topic_id: u64, raw: &str) -> Result<u64> {
let payload = [("topic_id", topic_id.to_string()), ("raw", raw.to_string())];
let response = self
.post("/posts.json")?
.form(&payload)
.send()
.context("creating post")?;
let status = response.status();
let text = response.text().context("reading create response body")?;
if !status.is_success() {
return Err(http_error("create post request", status, &text));
}
let body: CreatePostResponse =
serde_json::from_str(&text).context("parsing create post response")?;
Ok(body.id)
}
}