jenkins_sdk/api/
users.rs

1use crate::transport::request::{Request, RequestBody};
2use crate::{Error, UserId};
3use http::HeaderValue;
4use serde_json::Value;
5
6/// Jenkins users (core) APIs.
7#[derive(Clone)]
8#[cfg(feature = "async")]
9pub struct UsersService {
10    client: crate::Client,
11}
12
13#[cfg(feature = "async")]
14impl UsersService {
15    pub(crate) fn new(client: crate::Client) -> Self {
16        Self { client }
17    }
18}
19
20#[cfg(feature = "async")]
21impl UsersService {
22    /// `GET /user/<id>/api/json`
23    pub async fn get(&self, id: impl Into<UserId>, tree: Option<&str>) -> Result<Value, Error> {
24        let id = id.into();
25        let mut req = Request::get(["user", id.as_str(), "api", "json"]);
26        if let Some(tree) = tree {
27            req = req.query_pair("tree", tree);
28        }
29        self.client.send_json(req).await
30    }
31
32    /// `GET /whoAmI/api/json`
33    pub async fn who_am_i(&self) -> Result<Value, Error> {
34        self.client
35            .send_json(Request::get(["whoAmI", "api", "json"]))
36            .await
37    }
38
39    /// `GET /user/<id>/config.xml`
40    pub async fn get_config_xml(&self, id: impl Into<UserId>) -> Result<Vec<u8>, Error> {
41        let id = id.into();
42        self.client
43            .send_bytes(Request::get(["user", id.as_str(), "config.xml"]))
44            .await
45    }
46
47    /// `POST /user/<id>/config.xml` with XML body.
48    pub async fn update_config_xml(
49        &self,
50        id: impl Into<UserId>,
51        xml: impl Into<Vec<u8>>,
52    ) -> Result<(), Error> {
53        let id = id.into();
54        self.client
55            .send_unit(Request::post(["user", id.as_str(), "config.xml"]).body(
56                RequestBody::bytes_with_content_type(
57                    xml.into(),
58                    HeaderValue::from_static("application/xml"),
59                ),
60            ))
61            .await
62    }
63}
64
65/// Jenkins users (core) APIs (blocking).
66#[cfg(feature = "blocking")]
67#[derive(Clone)]
68pub struct BlockingUsersService {
69    client: crate::BlockingClient,
70}
71
72#[cfg(feature = "blocking")]
73impl BlockingUsersService {
74    pub(crate) fn new(client: crate::BlockingClient) -> Self {
75        Self { client }
76    }
77}
78
79#[cfg(feature = "blocking")]
80impl BlockingUsersService {
81    /// `GET /user/<id>/api/json`
82    pub fn get(&self, id: impl Into<UserId>, tree: Option<&str>) -> Result<Value, Error> {
83        let id = id.into();
84        let mut req = Request::get(["user", id.as_str(), "api", "json"]);
85        if let Some(tree) = tree {
86            req = req.query_pair("tree", tree);
87        }
88        self.client.send_json(req)
89    }
90
91    /// `GET /whoAmI/api/json`
92    pub fn who_am_i(&self) -> Result<Value, Error> {
93        self.client
94            .send_json(Request::get(["whoAmI", "api", "json"]))
95    }
96
97    /// `GET /user/<id>/config.xml`
98    pub fn get_config_xml(&self, id: impl Into<UserId>) -> Result<Vec<u8>, Error> {
99        let id = id.into();
100        self.client
101            .send_bytes(Request::get(["user", id.as_str(), "config.xml"]))
102    }
103
104    /// `POST /user/<id>/config.xml` with XML body.
105    pub fn update_config_xml(
106        &self,
107        id: impl Into<UserId>,
108        xml: impl Into<Vec<u8>>,
109    ) -> Result<(), Error> {
110        let id = id.into();
111        self.client
112            .send_unit(Request::post(["user", id.as_str(), "config.xml"]).body(
113                RequestBody::bytes_with_content_type(
114                    xml.into(),
115                    HeaderValue::from_static("application/xml"),
116                ),
117            ))
118    }
119}