jenkins_sdk/api/
system.rs

1use crate::transport::request::{Request, RequestBody};
2use crate::{Crumb, Error};
3use http::HeaderValue;
4use serde_json::Value;
5
6/// Jenkins system-level (core) APIs.
7#[derive(Clone)]
8#[cfg(feature = "async")]
9pub struct SystemService {
10    client: crate::Client,
11}
12
13#[cfg(feature = "async")]
14impl SystemService {
15    pub(crate) fn new(client: crate::Client) -> Self {
16        Self { client }
17    }
18}
19
20#[cfg(feature = "async")]
21impl SystemService {
22    /// `GET /api/json`
23    pub async fn root(&self, tree: Option<&str>) -> Result<Value, Error> {
24        let mut req = Request::get(["api", "json"]);
25        if let Some(tree) = tree {
26            req = req.query_pair("tree", tree);
27        }
28        self.client.send_json(req).await
29    }
30
31    /// `GET /overallLoad/api/json`
32    pub async fn overall_load(&self) -> Result<Value, Error> {
33        self.client
34            .send_json(Request::get(["overallLoad", "api", "json"]))
35            .await
36    }
37
38    /// `GET /loadStatistics/api/json`
39    pub async fn load_statistics(&self) -> Result<Value, Error> {
40        self.client
41            .send_json(Request::get(["loadStatistics", "api", "json"]))
42            .await
43    }
44
45    /// `GET /whoAmI/api/json`
46    pub async fn who_am_i(&self) -> Result<Value, Error> {
47        self.client
48            .send_json(Request::get(["whoAmI", "api", "json"]))
49            .await
50    }
51
52    /// `GET /crumbIssuer/api/json`
53    pub async fn crumb(&self) -> Result<Crumb, Error> {
54        self.client
55            .send_json(Request::get(["crumbIssuer", "api", "json"]))
56            .await
57    }
58
59    /// `GET /jnlpJars/agent.jar`
60    pub async fn agent_jar(&self) -> Result<Vec<u8>, Error> {
61        self.client
62            .send_bytes(Request::get(["jnlpJars", "agent.jar"]))
63            .await
64    }
65
66    /// `GET /jnlpJars/jenkins-cli.jar`
67    pub async fn cli_jar(&self) -> Result<Vec<u8>, Error> {
68        self.client
69            .send_bytes(Request::get(["jnlpJars", "jenkins-cli.jar"]))
70            .await
71    }
72
73    /// `GET /config.xml`
74    pub async fn get_config_xml(&self) -> Result<Vec<u8>, Error> {
75        self.client.send_bytes(Request::get(["config.xml"])).await
76    }
77
78    /// `POST /config.xml` with XML body.
79    pub async fn update_config_xml(&self, xml: impl Into<Vec<u8>>) -> Result<(), Error> {
80        self.client
81            .send_unit(
82                Request::post(["config.xml"]).body(RequestBody::bytes_with_content_type(
83                    xml.into(),
84                    HeaderValue::from_static("application/xml"),
85                )),
86            )
87            .await
88    }
89
90    /// `POST /quietDown`
91    pub async fn quiet_down(&self) -> Result<(), Error> {
92        self.client.send_unit(Request::post(["quietDown"])).await
93    }
94
95    /// `POST /cancelQuietDown`
96    pub async fn cancel_quiet_down(&self) -> Result<(), Error> {
97        self.client
98            .send_unit(Request::post(["cancelQuietDown"]))
99            .await
100    }
101
102    /// `POST /reload`
103    pub async fn reload_configuration(&self) -> Result<(), Error> {
104        self.client.send_unit(Request::post(["reload"])).await
105    }
106
107    /// `POST /safeRestart`
108    pub async fn safe_restart(&self) -> Result<(), Error> {
109        self.client.send_unit(Request::post(["safeRestart"])).await
110    }
111
112    /// `POST /restart`
113    pub async fn restart(&self) -> Result<(), Error> {
114        self.client.send_unit(Request::post(["restart"])).await
115    }
116
117    /// `POST /exit`
118    pub async fn exit(&self) -> Result<(), Error> {
119        self.client.send_unit(Request::post(["exit"])).await
120    }
121}
122
123/// Jenkins system-level (core) APIs (blocking).
124#[cfg(feature = "blocking")]
125#[derive(Clone)]
126pub struct BlockingSystemService {
127    client: crate::BlockingClient,
128}
129
130#[cfg(feature = "blocking")]
131impl BlockingSystemService {
132    pub(crate) fn new(client: crate::BlockingClient) -> Self {
133        Self { client }
134    }
135}
136
137#[cfg(feature = "blocking")]
138impl BlockingSystemService {
139    /// `GET /api/json`
140    pub fn root(&self, tree: Option<&str>) -> Result<Value, Error> {
141        let mut req = Request::get(["api", "json"]);
142        if let Some(tree) = tree {
143            req = req.query_pair("tree", tree);
144        }
145        self.client.send_json(req)
146    }
147
148    /// `GET /overallLoad/api/json`
149    pub fn overall_load(&self) -> Result<Value, Error> {
150        self.client
151            .send_json(Request::get(["overallLoad", "api", "json"]))
152    }
153
154    /// `GET /loadStatistics/api/json`
155    pub fn load_statistics(&self) -> Result<Value, Error> {
156        self.client
157            .send_json(Request::get(["loadStatistics", "api", "json"]))
158    }
159
160    /// `GET /whoAmI/api/json`
161    pub fn who_am_i(&self) -> Result<Value, Error> {
162        self.client
163            .send_json(Request::get(["whoAmI", "api", "json"]))
164    }
165
166    /// `GET /crumbIssuer/api/json`
167    pub fn crumb(&self) -> Result<Crumb, Error> {
168        self.client
169            .send_json(Request::get(["crumbIssuer", "api", "json"]))
170    }
171
172    /// `GET /jnlpJars/agent.jar`
173    pub fn agent_jar(&self) -> Result<Vec<u8>, Error> {
174        self.client
175            .send_bytes(Request::get(["jnlpJars", "agent.jar"]))
176    }
177
178    /// `GET /jnlpJars/jenkins-cli.jar`
179    pub fn cli_jar(&self) -> Result<Vec<u8>, Error> {
180        self.client
181            .send_bytes(Request::get(["jnlpJars", "jenkins-cli.jar"]))
182    }
183
184    /// `GET /config.xml`
185    pub fn get_config_xml(&self) -> Result<Vec<u8>, Error> {
186        self.client.send_bytes(Request::get(["config.xml"]))
187    }
188
189    /// `POST /config.xml` with XML body.
190    pub fn update_config_xml(&self, xml: impl Into<Vec<u8>>) -> Result<(), Error> {
191        self.client.send_unit(Request::post(["config.xml"]).body(
192            RequestBody::bytes_with_content_type(
193                xml.into(),
194                HeaderValue::from_static("application/xml"),
195            ),
196        ))
197    }
198
199    /// `POST /quietDown`
200    pub fn quiet_down(&self) -> Result<(), Error> {
201        self.client.send_unit(Request::post(["quietDown"]))
202    }
203
204    /// `POST /cancelQuietDown`
205    pub fn cancel_quiet_down(&self) -> Result<(), Error> {
206        self.client.send_unit(Request::post(["cancelQuietDown"]))
207    }
208
209    /// `POST /reload`
210    pub fn reload_configuration(&self) -> Result<(), Error> {
211        self.client.send_unit(Request::post(["reload"]))
212    }
213
214    /// `POST /safeRestart`
215    pub fn safe_restart(&self) -> Result<(), Error> {
216        self.client.send_unit(Request::post(["safeRestart"]))
217    }
218
219    /// `POST /restart`
220    pub fn restart(&self) -> Result<(), Error> {
221        self.client.send_unit(Request::post(["restart"]))
222    }
223
224    /// `POST /exit`
225    pub fn exit(&self) -> Result<(), Error> {
226        self.client.send_unit(Request::post(["exit"]))
227    }
228}