diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e3bcc00 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## Unreleased + +### ADDED + +- comment posting via `Client::create_comment(u64, String, bool)` diff --git a/src/comment.rs b/src/comment.rs index fec0b8c..e127e3d 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -42,6 +42,44 @@ impl crate::Client { Ok(resp.comment) } + /// Create a new comment for a given image + pub async fn create_comment( + &self, + image_id: u64, + body: String, + anonymous: bool, + ) -> Result { + #[derive(Default, Serialize, Debug, Clone)] + struct MakeComment { + body: String, + anonymous: bool, + } + + #[derive(Default, Serialize, Debug, Clone)] + struct Body { + comment: MakeComment, + } + + let resp: Response = self + .request( + reqwest::Method::POST, + &format!("api/v1/json/images/{}/comments", image_id), + ) + .json(&Body { + comment: MakeComment { + body: body, + anonymous: anonymous, + }, + }) + .send() + .await? + .error_for_status()? + .json() + .await?; + + Ok(resp.comment) + } + /// Search for comments. pub async fn comment_search>( &self, @@ -81,6 +119,27 @@ mod tests { cli.comment(1).await.unwrap(); } + #[tokio::test] + async fn create_comment() { + let _ = pretty_env_logger::try_init(); + let data: serde_json::Value = + serde_json::from_slice(include_bytes!("../testdata/comment_1.json")).unwrap(); + let server = Server::run(); + server.expect( + Expectation::matching(request::method_path( + "POST", + "/api/v1/json/images/1/comments", + )) + .respond_with(json_encoded(data)), + ); + + let cli = + crate::Client::with_baseurl("test", "42069", &format!("{}", server.url("/"))).unwrap(); + cli.create_comment(1, "Hey what's up guys its scarce here".to_string(), false) + .await + .unwrap(); + } + #[tokio::test] async fn comment_search() { let _ = pretty_env_logger::try_init();