Skip to main content

ytmapi_rs/query/
rate.rs

1use super::{PostMethod, PostQuery, Query};
2use crate::auth::LoggedIn;
3use crate::common::{LikeStatus, PlaylistID, VideoID, YoutubeID};
4use serde_json::json;
5use std::borrow::Cow;
6
7pub struct RateSongQuery<'a> {
8    video_id: VideoID<'a>,
9    rating: LikeStatus,
10}
11impl<'a> RateSongQuery<'a> {
12    pub fn new(video_id: VideoID<'a>, rating: LikeStatus) -> Self {
13        Self { video_id, rating }
14    }
15}
16pub struct RatePlaylistQuery<'a> {
17    playlist_id: PlaylistID<'a>,
18    rating: LikeStatus,
19}
20impl<'a> RatePlaylistQuery<'a> {
21    pub fn new(playlist_id: PlaylistID<'a>, rating: LikeStatus) -> Self {
22        Self {
23            playlist_id,
24            rating,
25        }
26    }
27}
28
29impl<A: LoggedIn> Query<A> for RateSongQuery<'_> {
30    type Output = ();
31    type Method = PostMethod;
32}
33impl PostQuery for RateSongQuery<'_> {
34    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
35        serde_json::Map::from_iter([(
36            "target".to_string(),
37            json!({"videoId" : self.video_id.get_raw()} ),
38        )])
39    }
40    fn params(&self) -> Vec<(&str, Cow<'_, str>)> {
41        vec![]
42    }
43    fn path(&self) -> &str {
44        like_endpoint(&self.rating)
45    }
46}
47
48impl<A: LoggedIn> Query<A> for RatePlaylistQuery<'_> {
49    type Output = ();
50    type Method = PostMethod;
51}
52
53impl PostQuery for RatePlaylistQuery<'_> {
54    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
55        serde_json::Map::from_iter([(
56            "target".to_string(),
57            json!({"playlistId" : self.playlist_id.get_raw()} ),
58        )])
59    }
60    fn params(&self) -> Vec<(&str, Cow<'_, str>)> {
61        vec![]
62    }
63    fn path(&self) -> &str {
64        like_endpoint(&self.rating)
65    }
66}
67
68fn like_endpoint(rating: &LikeStatus) -> &'static str {
69    match *rating {
70        LikeStatus::Liked => "like/like",
71        LikeStatus::Disliked => "like/dislike",
72        LikeStatus::Indifferent => "like/removelike",
73    }
74}