misp_client_rs/apis/
collections_api.rs

1//!
2//! MISP Automation API
3//!
4//!  ### Getting Started  MISP API allows you to query, create, modify data models, such as [Events](https://www.circl.lu/doc/misp/GLOSSARY.html#misp-event), [Objects](https://www.circl.lu/doc/misp/misp-objects/), [Attributes](https://www.circl.lu/doc/misp/GLOSSARY.html#misp-attribute). This is extremly useful for interconnecting MISP with external tools and feeding other systems with threat intel data.  It also lets you perform administrative tasks such as creating users, organisations, altering MISP settings, and much more.  To get an API key there are several options: * **[UI]** Go to [My Profile -> Auth Keys](/auth_keys/index) section and click on `+ Add authentication key`  * **[UI]** As an admin go to the the [Administration -> List Users -> View](/admin/users/view/[id]) page of the user you want to create an auth key for and on the `Auth keys` section click on `+ Add authentication key`  * **[CLI]** Use the following command: `./app/Console/cake user change_authkey [e-mail/user_id]`  * **API** Provided you already have an admin level API key, you can create an API key for another user using the `[POST]/auth_keys/add/{{user_id}}` endpoint.  > **NOTE:** The authentication key will only be displayed once, so take note of it or store it properly in your application secrets.  #### Accept and Content-Type headers  When performing your request, depending on the type of request, you might need to explicitly specify in what content  type you want to get your results. This is done by setting one of the below `Accept` headers:      Accept: application/json     Accept: application/xml  When submitting data in a `POST`, `PUT` or `DELETE` operation you also need to specify in what content-type you encoded the payload.  This is done by setting one of the below `Content-Type` headers:      Content-Type: application/json     Content-Type: application/xml  Example: ``` curl  --header \"Authorization: YOUR_API_KEY\" \\       --header \"Accept: application/json\" \\       --header \"Content-Type: application/json\" https://<misp url>/  ```  > **NOTE**: By appending .json or .xml the content type can also be set without the need for a header.  #### Automation using PyMISP  [PyMISP](https://github.com/MISP/PyMISP) is a Python library to access MISP platforms via their REST [API](https://www.circl.lu/doc/misp/GLOSSARY.html#api). It allows you to fetch events, add or update events/attributes, add or update samples or search for attributes.  ### FAQ * [Dev FAQ](https://www.circl.lu/doc/misp/dev-faq/) * [GitHub project FAQ](https://github.com/MISP/MISP/wiki/Frequently-Asked-Questions) 
5//!
6//! The version of the OpenAPI document: 2.4
7//! 
8//! Generated by: https://openapi-generator.tech
9//! 
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`add_collection`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum AddCollectionError {
22    Status403(models::UnauthorizedApiError),
23    DefaultResponse(models::ApiError),
24    UnknownValue(serde_json::Value),
25}
26
27/// struct for typed errors of method [`delete_collection`]
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum DeleteCollectionError {
31    Status403(models::UnauthorizedApiError),
32    Status404(models::NotFoundApiError),
33    DefaultResponse(models::ApiError),
34    UnknownValue(serde_json::Value),
35}
36
37/// struct for typed errors of method [`edit_collection`]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum EditCollectionError {
41    Status403(models::UnauthorizedApiError),
42    Status404(models::NotFoundApiError),
43    DefaultResponse(models::ApiError),
44    UnknownValue(serde_json::Value),
45}
46
47/// struct for typed errors of method [`get_collection_by_id`]
48#[derive(Debug, Clone, Serialize, Deserialize)]
49#[serde(untagged)]
50pub enum GetCollectionByIdError {
51    Status403(models::UnauthorizedApiError),
52    Status404(models::NotFoundApiError),
53    DefaultResponse(models::ApiError),
54    UnknownValue(serde_json::Value),
55}
56
57/// struct for typed errors of method [`get_collections`]
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(untagged)]
60pub enum GetCollectionsError {
61    Status403(models::UnauthorizedApiError),
62    DefaultResponse(models::ApiError),
63    UnknownValue(serde_json::Value),
64}
65
66
67pub async fn add_collection(configuration: &configuration::Configuration, add_collection_request: models::AddCollectionRequest) -> Result<models::AddCollection200Response, Error<AddCollectionError>> {
68    // add a prefix to parameters to efficiently prevent name collisions
69    let p_add_collection_request = add_collection_request;
70
71    let uri_str = format!("{}/collections/add", configuration.base_path);
72    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
73
74    if let Some(ref user_agent) = configuration.user_agent {
75        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
76    }
77    if let Some(ref apikey) = configuration.api_key {
78        let key = apikey.key.clone();
79        let value = match apikey.prefix {
80            Some(ref prefix) => format!("{} {}", prefix, key),
81            None => key,
82        };
83        req_builder = req_builder.header("Authorization", value);
84    };
85    req_builder = req_builder.json(&p_add_collection_request);
86
87    let req = req_builder.build()?;
88    let resp = configuration.client.execute(req).await?;
89
90    let status = resp.status();
91    let content_type = resp
92        .headers()
93        .get("content-type")
94        .and_then(|v| v.to_str().ok())
95        .unwrap_or("application/octet-stream");
96    let content_type = super::ContentType::from(content_type);
97
98    if !status.is_client_error() && !status.is_server_error() {
99        let content = resp.text().await?;
100        match content_type {
101            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
102            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AddCollection200Response`"))),
103            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AddCollection200Response`")))),
104        }
105    } else {
106        let content = resp.text().await?;
107        let entity: Option<AddCollectionError> = serde_json::from_str(&content).ok();
108        Err(Error::ResponseError(ResponseContent { status, content, entity }))
109    }
110}
111
112pub async fn delete_collection(configuration: &configuration::Configuration, collection_id: &str) -> Result<models::DeleteCollection200Response, Error<DeleteCollectionError>> {
113    // add a prefix to parameters to efficiently prevent name collisions
114    let p_collection_id = collection_id;
115
116    let uri_str = format!("{}/collections/delete/{collectionId}", configuration.base_path, collectionId=p_collection_id.to_string());
117    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
118
119    if let Some(ref user_agent) = configuration.user_agent {
120        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
121    }
122    if let Some(ref apikey) = configuration.api_key {
123        let key = apikey.key.clone();
124        let value = match apikey.prefix {
125            Some(ref prefix) => format!("{} {}", prefix, key),
126            None => key,
127        };
128        req_builder = req_builder.header("Authorization", value);
129    };
130
131    let req = req_builder.build()?;
132    let resp = configuration.client.execute(req).await?;
133
134    let status = resp.status();
135    let content_type = resp
136        .headers()
137        .get("content-type")
138        .and_then(|v| v.to_str().ok())
139        .unwrap_or("application/octet-stream");
140    let content_type = super::ContentType::from(content_type);
141
142    if !status.is_client_error() && !status.is_server_error() {
143        let content = resp.text().await?;
144        match content_type {
145            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
146            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeleteCollection200Response`"))),
147            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::DeleteCollection200Response`")))),
148        }
149    } else {
150        let content = resp.text().await?;
151        let entity: Option<DeleteCollectionError> = serde_json::from_str(&content).ok();
152        Err(Error::ResponseError(ResponseContent { status, content, entity }))
153    }
154}
155
156pub async fn edit_collection(configuration: &configuration::Configuration, collection_id: &str, base_collection: models::BaseCollection) -> Result<models::AddCollection200Response, Error<EditCollectionError>> {
157    // add a prefix to parameters to efficiently prevent name collisions
158    let p_collection_id = collection_id;
159    let p_base_collection = base_collection;
160
161    let uri_str = format!("{}/collections/edit/{collectionId}", configuration.base_path, collectionId=p_collection_id.to_string());
162    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
163
164    if let Some(ref user_agent) = configuration.user_agent {
165        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
166    }
167    if let Some(ref apikey) = configuration.api_key {
168        let key = apikey.key.clone();
169        let value = match apikey.prefix {
170            Some(ref prefix) => format!("{} {}", prefix, key),
171            None => key,
172        };
173        req_builder = req_builder.header("Authorization", value);
174    };
175    req_builder = req_builder.json(&p_base_collection);
176
177    let req = req_builder.build()?;
178    let resp = configuration.client.execute(req).await?;
179
180    let status = resp.status();
181    let content_type = resp
182        .headers()
183        .get("content-type")
184        .and_then(|v| v.to_str().ok())
185        .unwrap_or("application/octet-stream");
186    let content_type = super::ContentType::from(content_type);
187
188    if !status.is_client_error() && !status.is_server_error() {
189        let content = resp.text().await?;
190        match content_type {
191            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
192            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AddCollection200Response`"))),
193            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AddCollection200Response`")))),
194        }
195    } else {
196        let content = resp.text().await?;
197        let entity: Option<EditCollectionError> = serde_json::from_str(&content).ok();
198        Err(Error::ResponseError(ResponseContent { status, content, entity }))
199    }
200}
201
202pub async fn get_collection_by_id(configuration: &configuration::Configuration, collection_id: &str) -> Result<models::GetCollectionById200Response, Error<GetCollectionByIdError>> {
203    // add a prefix to parameters to efficiently prevent name collisions
204    let p_collection_id = collection_id;
205
206    let uri_str = format!("{}/collections/view/{collectionId}", configuration.base_path, collectionId=p_collection_id.to_string());
207    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
208
209    if let Some(ref user_agent) = configuration.user_agent {
210        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
211    }
212    if let Some(ref apikey) = configuration.api_key {
213        let key = apikey.key.clone();
214        let value = match apikey.prefix {
215            Some(ref prefix) => format!("{} {}", prefix, key),
216            None => key,
217        };
218        req_builder = req_builder.header("Authorization", value);
219    };
220
221    let req = req_builder.build()?;
222    let resp = configuration.client.execute(req).await?;
223
224    let status = resp.status();
225    let content_type = resp
226        .headers()
227        .get("content-type")
228        .and_then(|v| v.to_str().ok())
229        .unwrap_or("application/octet-stream");
230    let content_type = super::ContentType::from(content_type);
231
232    if !status.is_client_error() && !status.is_server_error() {
233        let content = resp.text().await?;
234        match content_type {
235            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
236            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetCollectionById200Response`"))),
237            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::GetCollectionById200Response`")))),
238        }
239    } else {
240        let content = resp.text().await?;
241        let entity: Option<GetCollectionByIdError> = serde_json::from_str(&content).ok();
242        Err(Error::ResponseError(ResponseContent { status, content, entity }))
243    }
244}
245
246pub async fn get_collections(configuration: &configuration::Configuration, filter: &str, get_collections_request: Option<models::GetCollectionsRequest>) -> Result<Vec<models::GetCollections200ResponseInner>, Error<GetCollectionsError>> {
247    // add a prefix to parameters to efficiently prevent name collisions
248    let p_filter = filter;
249    let p_get_collections_request = get_collections_request;
250
251    let uri_str = format!("{}/collections/index/{filter}", configuration.base_path, filter=crate::apis::urlencode(p_filter));
252    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
253
254    if let Some(ref user_agent) = configuration.user_agent {
255        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
256    }
257    if let Some(ref apikey) = configuration.api_key {
258        let key = apikey.key.clone();
259        let value = match apikey.prefix {
260            Some(ref prefix) => format!("{} {}", prefix, key),
261            None => key,
262        };
263        req_builder = req_builder.header("Authorization", value);
264    };
265    req_builder = req_builder.json(&p_get_collections_request);
266
267    let req = req_builder.build()?;
268    let resp = configuration.client.execute(req).await?;
269
270    let status = resp.status();
271    let content_type = resp
272        .headers()
273        .get("content-type")
274        .and_then(|v| v.to_str().ok())
275        .unwrap_or("application/octet-stream");
276    let content_type = super::ContentType::from(content_type);
277
278    if !status.is_client_error() && !status.is_server_error() {
279        let content = resp.text().await?;
280        match content_type {
281            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
282            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::GetCollections200ResponseInner&gt;`"))),
283            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec&lt;models::GetCollections200ResponseInner&gt;`")))),
284        }
285    } else {
286        let content = resp.text().await?;
287        let entity: Option<GetCollectionsError> = serde_json::from_str(&content).ok();
288        Err(Error::ResponseError(ResponseContent { status, content, entity }))
289    }
290}
291