mastodon_api\methods/
instance.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::{Account, Activity, Instance, Rule};
4
5/// Handler for instance-related API endpoints.
6pub struct InstanceHandler<'a> {
7    client: &'a MastodonClient,
8}
9
10impl<'a> InstanceHandler<'a> {
11    /// Creates a new `InstanceHandler` for the given client.
12    pub fn new(client: &'a MastodonClient) -> Self {
13        Self { client }
14    }
15
16    /// Fetches metadata about the Mastodon instance.
17    ///
18    /// Returns:
19    /// - `Result<Instance>`: The instance metadata.
20    ///
21    /// Corresponds to `GET /api/v1/instance`.
22    pub async fn get(&self) -> Result<Instance> {
23        let url = format!("{}/api/v1/instance", self.client.base_url());
24        let req = self.client.http_client().get(&url);
25        self.client.send(req).await
26    }
27
28    /// Fetches the list of domains this instance is aware of.
29    ///
30    /// Returns:
31    /// - `Result<Vec<String>>`: A list of domain names.
32    ///
33    /// Corresponds to `GET /api/v1/instance/peers`.
34    pub async fn peers(&self) -> Result<Vec<String>> {
35        let url = format!("{}/api/v1/instance/peers", self.client.base_url());
36        let req = self.client.http_client().get(&url);
37        self.client.send(req).await
38    }
39
40    /// Fetches weekly usage statistics for the instance.
41    ///
42    /// Returns:
43    /// - `Result<Vec<Activity>>`: A list of weekly activity statistics.
44    ///
45    /// Corresponds to `GET /api/v1/instance/activity`.
46    pub async fn activity(&self) -> Result<Vec<Activity>> {
47        let url = format!("{}/api/v1/instance/activity", self.client.base_url());
48        let req = self.client.http_client().get(&url);
49        self.client.send(req).await
50    }
51
52    /// Fetches the formal rules established by the server.
53    ///
54    /// Returns:
55    /// - `Result<Vec<Rule>>`: A list of instance rules.
56    ///
57    /// Corresponds to `GET /api/v1/instance/rules`.
58    pub async fn rules(&self) -> Result<Vec<Rule>> {
59        let url = format!("{}/api/v1/instance/rules", self.client.base_url());
60        let req = self.client.http_client().get(&url);
61        self.client.send(req).await
62    }
63
64    /// Fetches the instance-level user directory.
65    ///
66    /// Returns:
67    /// - `Result<Vec<Account>>`: A list of accounts in the directory.
68    ///
69    /// Corresponds to `GET /api/v1/directories`.
70    pub async fn directories(&self) -> Result<Vec<Account>> {
71        let url = format!("{}/api/v1/directories", self.client.base_url());
72        let req = self.client.http_client().get(&url);
73        self.client.send(req).await
74    }
75}