Skip to main content

gitee_cli_rs/api/
users.rs

1use super::client::Client;
2use crate::error::Result;
3use crate::models::{Issue, Org, SshKey, UserBasic};
4
5/// User-level operations (no repo scope): the authenticated user and their
6/// cross-repo issue lists.
7pub struct Users<'a> {
8    client: &'a Client,
9}
10
11/// Filter for `GET /user/issues`. `filter` selects the list (Gitee requires
12/// it): `assigned` | `created` | `all`.
13pub struct UserIssueFilter<'a> {
14    pub filter: &'a str,
15    pub state: Option<&'a str>,
16    pub limit: usize,
17}
18
19impl Users<'_> {
20    pub(crate) fn new<'a>(client: &'a Client) -> Users<'a> {
21        Users { client }
22    }
23
24    /// The authenticated user (GET /user).
25    pub fn me(&self) -> Result<UserBasic> {
26        self.client.get("/user", &[])
27    }
28
29    /// Cross-repo issues for the authenticated user (GET /user/issues).
30    pub fn issues(&self, filter: &UserIssueFilter<'_>) -> Result<Vec<Issue>> {
31        let mut q: Vec<(&str, String)> = vec![("filter", filter.filter.to_string())];
32        if let Some(s) = filter.state {
33            q.push(("state", s.to_string()));
34        }
35        let qref = Client::str_refs(&q);
36        self.client.get_paged("/user/issues", &qref, filter.limit)
37    }
38
39    /// Organizations for the authenticated user (GET /user/orgs).
40    pub fn orgs(&self, limit: usize) -> Result<Vec<Org>> {
41        self.client.get_paged("/user/orgs", &[], limit)
42    }
43
44    /// SSH keys for the authenticated user (GET /user/keys).
45    pub fn keys(&self, limit: usize) -> Result<Vec<SshKey>> {
46        self.client.get_paged("/user/keys", &[], limit)
47    }
48
49    /// Add an SSH public key (POST /user/keys).
50    pub fn add_key(&self, key: &str, title: &str) -> Result<SshKey> {
51        self.client
52            .post("/user/keys", &[("key", key), ("title", title)])
53    }
54
55    /// Delete an SSH key (DELETE /user/keys/{id}).
56    pub fn delete_key(&self, id: i64) -> Result<()> {
57        self.client.delete_ok(&format!("/user/keys/{id}"))
58    }
59}