Skip to main content

tongbal_api/restriction/
mod.rs

1mod builder;
2mod response;
3mod types;
4
5pub use builder::GetRestriction;
6pub use response::RestrictionChannelResponse;
7pub use types::RestrictionChannel;
8
9use std::future::Future;
10
11use types::RestrictionBody;
12
13use crate::{
14    BaseClient, Error, UserClient,
15    types::{ChannelId, constants::RESTRICT_CHANNELS},
16};
17
18pub trait RestrictionAPI: BaseClient {
19    /// See <https://chzzk.gitbook.io/chzzk/chzzk-api/restriction#undefined>
20    fn add_restraction(&self, target: &ChannelId)
21    -> impl Future<Output = Result<(), Error>> + Send;
22
23    /// See <https://chzzk.gitbook.io/chzzk/chzzk-api/restriction#undefined-1>
24    fn remove_restriction(
25        &self,
26        target: &ChannelId,
27    ) -> impl Future<Output = Result<(), Error>> + Send;
28
29    /// See <https://chzzk.gitbook.io/chzzk/chzzk-api/restriction#undefined-2>
30    fn get_restrictions<'a>(&'a self) -> GetRestriction<'a>;
31}
32
33impl RestrictionAPI for UserClient {
34    async fn add_restraction(&self, target: &ChannelId) -> Result<(), Error> {
35        let mut url = self.base_url();
36
37        url.path_segments_mut().unwrap().push(RESTRICT_CHANNELS);
38
39        self.http_client()
40            .post(url)
41            .json(&RestrictionBody {
42                target_channel_id: target,
43            })
44            .send()
45            .await?;
46
47        Ok(())
48    }
49
50    async fn remove_restriction(&self, target: &ChannelId) -> Result<(), Error> {
51        let mut url = self.base_url();
52
53        url.path_segments_mut().unwrap().push(RESTRICT_CHANNELS);
54
55        self.http_client()
56            .delete(url)
57            .json(&RestrictionBody {
58                target_channel_id: target,
59            })
60            .send()
61            .await?;
62
63        Ok(())
64    }
65
66    fn get_restrictions<'a>(&'a self) -> GetRestriction<'a> {
67        GetRestriction::new(self)
68    }
69}