1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::{
    Client,
    Error,
    NoteList,
};
use std::num::NonZeroU64;
use url::Url;

/// A query builder to get notes.
///
/// This is undocumented.
#[derive(Debug)]
pub struct NotesListQueryBuilder<'a> {
    /// The post id to get notes for.
    ///
    /// This is undocumented.
    pub post_id: Option<NonZeroU64>,

    /// The client
    client: &'a Client,
}

impl<'a> NotesListQueryBuilder<'a> {
    /// Make a new [`NotesListQueryBuilder`]
    pub fn new(client: &'a Client) -> Self {
        Self {
            post_id: None,

            client,
        }
    }

    /// Set the post id to get notes for.
    ///
    /// This is undocumented.
    pub fn post_id(&mut self, post_id: Option<NonZeroU64>) -> &mut Self {
        self.post_id = post_id;
        self
    }

    /// Get the url for this query.
    pub fn get_url(&self) -> Result<Url, Error> {
        let mut url = Url::parse_with_params(
            crate::API_BASE_URL,
            &[("page", "dapi"), ("s", "note"), ("q", "index")],
        )?;

        {
            let mut query_pairs = url.query_pairs_mut();

            if let Some(post_id) = self.post_id {
                let mut buffer = itoa::Buffer::new();
                query_pairs.append_pair("post_id", buffer.format(post_id.get()));
            }
        }

        Ok(url)
    }

    /// Execute the query
    pub async fn execute(&self) -> Result<NoteList, Error> {
        let url = self.get_url()?;

        self.client.get_xml(url.as_str()).await
    }
}