mastodon_api\methods/
apps.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use serde::{Deserialize, Serialize};
4
5/// Handler for application-related API endpoints.
6pub struct AppsHandler<'a> {
7    client: &'a MastodonClient,
8}
9
10/// Parameters for registering a new application.
11#[derive(Serialize)]
12pub struct RegisterAppParams {
13    /// The name of your application.
14    pub client_name: String,
15    /// Where to redirect the user after authorization. Use `urn:ietf:wg:oauth:2.0:oob` for local apps.
16    pub redirect_uris: String,
17    /// Space-separated list of scopes (e.g., "read write follow").
18    pub scopes: String,
19    /// The website of your application.
20    pub website: Option<String>,
21}
22
23/// Response from a successful application registration.
24#[derive(Deserialize)]
25pub struct AppRegistration {
26    pub id: String,
27    pub name: String,
28    pub website: Option<String>,
29    pub redirect_uri: String,
30    pub client_id: String,
31    pub client_secret: String,
32    pub vapid_key: Option<String>,
33}
34
35impl<'a> AppsHandler<'a> {
36    pub fn new(client: &'a MastodonClient) -> Self {
37        Self { client }
38    }
39
40    /// Registers a new application with the Mastodon instance.
41    ///
42    /// Parameters:
43    /// - `params`: The parameters for the application to register.
44    ///
45    /// Returns:
46    /// - `Result<AppRegistration>`: The registered application.
47    ///
48    /// Corresponds to `POST /api/v1/apps`.
49    pub async fn register(&self, params: &RegisterAppParams) -> Result<AppRegistration> {
50        let url = format!("{}/api/v1/apps", self.client.base_url());
51        let req = self.client.http_client().post(&url).json(params);
52        self.client.send(req).await
53    }
54}