instances_social/instances/
sample.rs

1//! Represents a request to the https://instances.social/api/1.0/instances/sample endpoint
2//!
3//! ```no_run
4//! use instances_social::Client;
5//!
6//! const TOKEN: &'static str = "...";
7//!
8//! fn main() -> Result<(), Box<std::error::Error>> {
9//!     let client = Client::new(TOKEN);
10//!
11//!     let result = client.instances()
12//!         .sample() // returns this builder
13//!         .count(100) // sets the ?count=100 querystring param
14//!         // ...etc
15//!         .send()?; // actually sends the request
16//!     Ok(())
17//! }
18//! ```
19
20use std::error::Error;
21
22use crate::instances::response::{Instance, SampleResponse};
23use crate::Client;
24use crate::ItemIter;
25use crate::ItemIterator;
26
27/// Represents a request to the https://instances.social/api/1.0/instances/sample endpoint
28///
29/// ```no_run
30/// use instances_social::Client;
31///
32/// const TOKEN: &'static str = "...";
33///
34/// fn main() -> Result<(), Box<std::error::Error>> {
35///     let client = Client::new(TOKEN);
36///
37///     let result = client.instances()
38///         .sample() // returns this builder
39///         .count(100) // sets the ?count=100 querystring param
40///         // ...etc
41///         .send()?; // actually sends the request
42///     Ok(())
43/// }
44/// ```
45#[derive(Serialize)]
46pub struct SampleRequestBuilder<'a> {
47    #[serde(skip)]
48    client: &'a Client,
49    #[serde(skip_serializing_if = "Option::is_none")]
50    count: Option<u64>,
51    #[serde(skip_serializing_if = "Option::is_none")]
52    include_dead: Option<bool>,
53}
54
55impl<'a, 'b: 'a> ItemIter for &'a mut SampleRequestBuilder<'b> {
56    type Response = SampleResponse;
57
58    fn get_page(&mut self, _: Option<&String>) -> Result<Self::Response, Box<Error>> {
59        Ok(self.send()?)
60    }
61}
62
63impl<'a> SampleRequestBuilder<'a> {
64    pub(crate) fn new(client: &'a Client) -> SampleRequestBuilder<'a> {
65        SampleRequestBuilder {
66            client,
67            count: None,
68            include_dead: None,
69        }
70    }
71
72    /// Sets the `count` parameter for the request
73    pub fn count(&mut self, count: u64) -> &mut Self {
74        self.count = Some(count);
75        self
76    }
77
78    /// Sets the `include_dead` parameter for the request
79    pub fn include_dead(&mut self, include_dead: bool) -> &mut Self {
80        self.include_dead = Some(include_dead);
81        self
82    }
83
84    /// Returns an iterator over the returned `Instance`s
85    pub fn iter(&'a mut self) -> impl Iterator<Item = Instance> + 'a {
86        ItemIterator::new(self)
87    }
88
89    /// Send the request
90    pub fn send(&self) -> Result<SampleResponse, Box<Error>> {
91        let req = self.client.get_qs("instances/sample", self)?;
92        Ok(self.client.send(req)?)
93    }
94}