gitea_sdk/api/user/
tokens.rs

1use build_it::Builder;
2use serde::Serialize;
3
4use crate::error::Result;
5use crate::model::user::AccessToken;
6
7#[derive(Debug, Clone, Builder, Serialize)]
8pub struct ListAccessTokensBuilder {
9    /// The username of the user to list access tokens forj.
10    #[skip]
11    #[serde(skip)]
12    username: String,
13    /// Page number of results to return (1-based).
14    page: Option<i64>,
15    /// Page size of results.
16    limit: Option<i64>,
17}
18
19#[derive(Debug, Clone, Serialize)]
20pub struct CreateAccessTokenBuilder {
21    #[serde(skip)]
22    /// The username of the user to create the access token for.
23    pub user: String,
24    /// Access token name.
25    pub name: String,
26    /// Optional scopes for the access token.
27    pub scopes: Vec<String>,
28}
29
30#[derive(Debug, Clone)]
31pub struct DeleteAccessTokenBuilder {
32    /// The username of the user to delete the access token for.
33    pub user: String,
34    /// The access token to delete.
35    pub token: String,
36}
37
38impl ListAccessTokensBuilder {
39    pub fn new(username: impl ToString) -> Self {
40        Self {
41            username: username.to_string(),
42            page: None,
43            limit: None,
44        }
45    }
46    /// Sends the request to list access tokens.
47    pub async fn send(&self, client: &crate::Client) -> Result<Vec<AccessToken>> {
48        let username = &self.username;
49        let req = client
50            .get(format!("users/{username}/tokens"))
51            .query(self)
52            .build()?;
53        let res = client.make_request(req).await?;
54        client.parse_response(res).await
55    }
56}
57
58impl CreateAccessTokenBuilder {
59    pub fn new(user: impl ToString, name: impl ToString, scopes: Vec<impl ToString>) -> Self {
60        Self {
61            user: user.to_string(),
62            name: name.to_string(),
63            scopes: scopes.into_iter().map(|s| s.to_string()).collect(),
64        }
65    }
66
67    /// Sends the request to create the access token.
68    pub async fn send(&self, client: &crate::Client) -> Result<AccessToken> {
69        let username = &self.user;
70        let req = client
71            .post(format!("users/{username}/tokens"))
72            .json(self)
73            .build()?;
74        let res = client.make_request(req).await?;
75        client.parse_response(res).await
76    }
77}
78
79impl DeleteAccessTokenBuilder {
80    pub fn new(user: impl ToString, token: impl ToString) -> Self {
81        Self {
82            user: user.to_string(),
83            token: token.to_string(),
84        }
85    }
86    /// Sends the request to delete the access token.
87    pub async fn send(&self, client: &crate::Client) -> Result<()> {
88        let DeleteAccessTokenBuilder { user, token } = self;
89        let req = client
90            .delete(format!("users/{user}/tokens/{token}"))
91            .build()?;
92        client.make_request(req).await?;
93        Ok(())
94    }
95}