1use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8pub struct MissionsClient {
10 base_url: String,
11 client: Client,
12}
13
14impl MissionsClient {
15 pub fn new(base_url: String, client: Client) -> Self {
17 Self { base_url, client }
18 }
19
20 pub fn with_auth(self, token: &str) -> Self {
22 self
24 }
25
26 pub async fn get_mission_mission_view(
28 &self,
29 mission: String,
30 id: String,
31 ) -> Result<Value> {
32 let path = format!("/{}/-/mission/view", mission);
33 let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
34
35 url.query_pairs_mut().append_pair("id", &id.to_string());
36
37 let request = self.client.request(
38 reqwest::Method::GET,
39 url
40 );
41
42
43
44
45 let response = request.send().await?;
46
47 if response.status().is_success() {
48 let json: Value = response.json().await?;
49 Ok(json)
50 } else {
51 Err(ApiError::HttpError(response.status().as_u16()))
52 }
53 }
54
55 pub async fn post_mission_mission_view(
57 &self,
58 mission: String,
59 request_data: serde_json::Value,
60 ) -> Result<Value> {
61 let path = format!("/{}/-/mission/view", mission);
62 let url = Url::parse(&format!("{}{}", self.base_url, path))?;
63
64
65
66 let mut request = self.client.request(
67 reqwest::Method::POST,
68 url
69 );
70
71
72
73 request = request.json(&request_data);
74
75 let response = request.send().await?;
76
77 if response.status().is_success() {
78 let json: Value = response.json().await?;
79 Ok(json)
80 } else {
81 Err(ApiError::HttpError(response.status().as_u16()))
82 }
83 }
84
85 pub async fn get_mission_mission_view_list(
87 &self,
88 mission: String,
89 ) -> Result<Value> {
90 let path = format!("/{}/-/mission/view-list", mission);
91 let url = Url::parse(&format!("{}{}", self.base_url, path))?;
92
93
94 let request = self.client.request(
95 reqwest::Method::GET,
96 url
97 );
98
99
100
101
102 let response = request.send().await?;
103
104 if response.status().is_success() {
105 let json: Value = response.json().await?;
106 Ok(json)
107 } else {
108 Err(ApiError::HttpError(response.status().as_u16()))
109 }
110 }
111
112 pub async fn post_mission_mission_view_list(
114 &self,
115 mission: String,
116 request_data: serde_json::Value,
117 ) -> Result<Value> {
118 let path = format!("/{}/-/mission/view-list", mission);
119 let url = Url::parse(&format!("{}{}", self.base_url, path))?;
120
121
122
123 let mut request = self.client.request(
124 reqwest::Method::POST,
125 url
126 );
127
128
129
130 request = request.json(&request_data);
131
132 let response = request.send().await?;
133
134 if response.status().is_success() {
135 let json: Value = response.json().await?;
136 Ok(json)
137 } else {
138 Err(ApiError::HttpError(response.status().as_u16()))
139 }
140 }
141
142 pub async fn put_mission_mission_view_list(
144 &self,
145 mission: String,
146 request_data: serde_json::Value,
147 ) -> Result<Value> {
148 let path = format!("/{}/-/mission/view-list", mission);
149 let url = Url::parse(&format!("{}{}", self.base_url, path))?;
150
151
152
153 let mut request = self.client.request(
154 reqwest::Method::PUT,
155 url
156 );
157
158
159
160 request = request.json(&request_data);
161
162 let response = request.send().await?;
163
164 if response.status().is_success() {
165 let json: Value = response.json().await?;
166 Ok(json)
167 } else {
168 Err(ApiError::HttpError(response.status().as_u16()))
169 }
170 }
171
172 pub async fn post_mission_settings_set_visibility(
174 &self,
175 mission: String,
176 visibility: String,
177 ) -> Result<Value> {
178 let path = format!("/{}/-/settings/set_visibility", mission);
179 let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
180
181 url.query_pairs_mut().append_pair("visibility", &visibility.to_string());
182
183 let request = self.client.request(
184 reqwest::Method::POST,
185 url
186 );
187
188
189
190
191 let response = request.send().await?;
192
193 if response.status().is_success() {
194 let json: Value = response.json().await?;
195 Ok(json)
196 } else {
197 Err(ApiError::HttpError(response.status().as_u16()))
198 }
199 }
200
201 pub async fn delete_mission(
203 &self,
204 mission: String,
205 x_cnb_identity_ticket: Option<String>,
206 ) -> Result<Value> {
207 let path = format!("/{}", mission);
208 let url = Url::parse(&format!("{}{}", self.base_url, path))?;
209
210
211 let mut request = self.client.request(
212 reqwest::Method::DELETE,
213 url
214 );
215
216
217 if let Some(value) = x_cnb_identity_ticket {
218 request = request.header("x-cnb-identity-ticket", value);
219 }
220
221
222 let response = request.send().await?;
223
224 if response.status().is_success() {
225 let json: Value = response.json().await?;
226 Ok(json)
227 } else {
228 Err(ApiError::HttpError(response.status().as_u16()))
229 }
230 }
231
232}