Skip to content

Commit

Permalink
Add post_unfavorite
Browse files Browse the repository at this point in the history
  • Loading branch information
playfulkittykat committed Oct 19, 2024
1 parent 660c1b7 commit 4aa96bc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,39 @@ impl Client {
.await
}

pub(crate) async fn delete(&self, endpoint: &str) -> Result<()> {
let url = self.url(endpoint)?;
let mut request = self.client.delete(url.clone());

if let Some((ref username, ref password)) = self.login {
request = request.basic_auth(username, Some(password));
}

let request_fut = request.headers(self.headers.clone()).send();

self.rate_limit
.clone()
.check(async move {
let res = request_fut
.await
.map_err(|e| Error::CannotSendRequest(format!("{}", e)))?;

if res.status().is_success() {
Ok(())
} else {
Err(Error::Http {
url,
code: res.status().as_u16(),
reason: match res.json::<serde_json::Value>().await {
Ok(v) => v["reason"].as_str().map(ToString::to_string),
Err(_) => None,
},
})
}
})
.await
}

pub fn get_json_endpoint(
&self,
endpoint: &str,
Expand Down
33 changes: 33 additions & 0 deletions src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,25 @@ impl Client {
serde_json::from_value(value).map_err(|e| Error::Serial(format!("{}", e)))
}

/// Mark a [`Post`] (identified by `id`) as no longer particularly liked.
///
/// ```no_run
/// # use {
/// # rs621::client::Client,
/// # futures::prelude::*,
/// # };
/// # #[tokio::main]
/// # async fn main() -> rs621::error::Result<()> {
/// let client = Client::new("https://e926.net", "MyProject/1.0 (by username on e621)")?;
///
/// client.post_unfavorite(1234).await?;
/// # Ok(()) }
/// ```
pub async fn post_unfavorite(&self, id: u64) -> Result<(), Error> {
self.delete(&format!("/favorites/{}.json", id)).await?;
Ok(())
}

/// Vote a [`Post`] (identified by `id`) up or down.
///
/// Use [`VoteDir::Toggle`] to clear an existing vote.
Expand Down Expand Up @@ -746,6 +765,20 @@ mod tests {
);
}

#[tokio::test]
async fn post_unfavorite() {
let mut client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();
client.login("foo".into(), "bar".into());

let _m = mock(
"DELETE",
Matcher::Exact("/favorites/3758515.json?login=foo&api_key=bar".into()),
)
.create();

client.post_unfavorite(3758515).await.unwrap();
}

#[tokio::test]
async fn search_ordered() {
let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();
Expand Down

0 comments on commit 4aa96bc

Please sign in to comment.