misp_client_rs/apis/
sightings_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_sighting`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum AddSightingError {
22    Status403(models::UnauthorizedApiError),
23    Status404(models::NotFoundApiError),
24    DefaultResponse(models::ApiError),
25    UnknownValue(serde_json::Value),
26}
27
28/// struct for typed errors of method [`add_sighting_by_value`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum AddSightingByValueError {
32    Status403(models::UnauthorizedApiError),
33    Status404(models::NotFoundApiError),
34    DefaultResponse(models::ApiError),
35    UnknownValue(serde_json::Value),
36}
37
38/// struct for typed errors of method [`delete_sighting`]
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[serde(untagged)]
41pub enum DeleteSightingError {
42    Status403(models::UnauthorizedApiError),
43    Status404(models::NotFoundApiError),
44    DefaultResponse(models::ApiError),
45    UnknownValue(serde_json::Value),
46}
47
48/// struct for typed errors of method [`get_sightings_by_event_id`]
49#[derive(Debug, Clone, Serialize, Deserialize)]
50#[serde(untagged)]
51pub enum GetSightingsByEventIdError {
52    Status403(models::UnauthorizedApiError),
53    Status404(models::NotFoundApiError),
54    DefaultResponse(models::ApiError),
55    UnknownValue(serde_json::Value),
56}
57
58
59pub async fn add_sighting(configuration: &configuration::Configuration, attribute_id: &str) -> Result<models::Sighting, Error<AddSightingError>> {
60    // add a prefix to parameters to efficiently prevent name collisions
61    let p_attribute_id = attribute_id;
62
63    let uri_str = format!("{}/sightings/add/{attributeId}", configuration.base_path, attributeId=p_attribute_id.to_string());
64    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
65
66    if let Some(ref user_agent) = configuration.user_agent {
67        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
68    }
69    if let Some(ref apikey) = configuration.api_key {
70        let key = apikey.key.clone();
71        let value = match apikey.prefix {
72            Some(ref prefix) => format!("{} {}", prefix, key),
73            None => key,
74        };
75        req_builder = req_builder.header("Authorization", value);
76    };
77
78    let req = req_builder.build()?;
79    let resp = configuration.client.execute(req).await?;
80
81    let status = resp.status();
82    let content_type = resp
83        .headers()
84        .get("content-type")
85        .and_then(|v| v.to_str().ok())
86        .unwrap_or("application/octet-stream");
87    let content_type = super::ContentType::from(content_type);
88
89    if !status.is_client_error() && !status.is_server_error() {
90        let content = resp.text().await?;
91        match content_type {
92            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
93            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Sighting`"))),
94            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::Sighting`")))),
95        }
96    } else {
97        let content = resp.text().await?;
98        let entity: Option<AddSightingError> = serde_json::from_str(&content).ok();
99        Err(Error::ResponseError(ResponseContent { status, content, entity }))
100    }
101}
102
103pub async fn add_sighting_by_value(configuration: &configuration::Configuration, add_sighting_by_value_request: Option<models::AddSightingByValueRequest>) -> Result<models::Sighting, Error<AddSightingByValueError>> {
104    // add a prefix to parameters to efficiently prevent name collisions
105    let p_add_sighting_by_value_request = add_sighting_by_value_request;
106
107    let uri_str = format!("{}/sightings/add", configuration.base_path);
108    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
109
110    if let Some(ref user_agent) = configuration.user_agent {
111        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
112    }
113    if let Some(ref apikey) = configuration.api_key {
114        let key = apikey.key.clone();
115        let value = match apikey.prefix {
116            Some(ref prefix) => format!("{} {}", prefix, key),
117            None => key,
118        };
119        req_builder = req_builder.header("Authorization", value);
120    };
121    req_builder = req_builder.json(&p_add_sighting_by_value_request);
122
123    let req = req_builder.build()?;
124    let resp = configuration.client.execute(req).await?;
125
126    let status = resp.status();
127    let content_type = resp
128        .headers()
129        .get("content-type")
130        .and_then(|v| v.to_str().ok())
131        .unwrap_or("application/octet-stream");
132    let content_type = super::ContentType::from(content_type);
133
134    if !status.is_client_error() && !status.is_server_error() {
135        let content = resp.text().await?;
136        match content_type {
137            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
138            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Sighting`"))),
139            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::Sighting`")))),
140        }
141    } else {
142        let content = resp.text().await?;
143        let entity: Option<AddSightingByValueError> = serde_json::from_str(&content).ok();
144        Err(Error::ResponseError(ResponseContent { status, content, entity }))
145    }
146}
147
148pub async fn delete_sighting(configuration: &configuration::Configuration, sighting_id: &str) -> Result<models::DeleteSighting200Response, Error<DeleteSightingError>> {
149    // add a prefix to parameters to efficiently prevent name collisions
150    let p_sighting_id = sighting_id;
151
152    let uri_str = format!("{}/sightings/delete/{sightingId}", configuration.base_path, sightingId=crate::apis::urlencode(p_sighting_id));
153    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
154
155    if let Some(ref user_agent) = configuration.user_agent {
156        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
157    }
158    if let Some(ref apikey) = configuration.api_key {
159        let key = apikey.key.clone();
160        let value = match apikey.prefix {
161            Some(ref prefix) => format!("{} {}", prefix, key),
162            None => key,
163        };
164        req_builder = req_builder.header("Authorization", value);
165    };
166
167    let req = req_builder.build()?;
168    let resp = configuration.client.execute(req).await?;
169
170    let status = resp.status();
171    let content_type = resp
172        .headers()
173        .get("content-type")
174        .and_then(|v| v.to_str().ok())
175        .unwrap_or("application/octet-stream");
176    let content_type = super::ContentType::from(content_type);
177
178    if !status.is_client_error() && !status.is_server_error() {
179        let content = resp.text().await?;
180        match content_type {
181            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
182            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeleteSighting200Response`"))),
183            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::DeleteSighting200Response`")))),
184        }
185    } else {
186        let content = resp.text().await?;
187        let entity: Option<DeleteSightingError> = serde_json::from_str(&content).ok();
188        Err(Error::ResponseError(ResponseContent { status, content, entity }))
189    }
190}
191
192pub async fn get_sightings_by_event_id(configuration: &configuration::Configuration, event_id: &str) -> Result<Vec<models::Sighting>, Error<GetSightingsByEventIdError>> {
193    // add a prefix to parameters to efficiently prevent name collisions
194    let p_event_id = event_id;
195
196    let uri_str = format!("{}/sightings/index/{eventId}", configuration.base_path, eventId=p_event_id.to_string());
197    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
198
199    if let Some(ref user_agent) = configuration.user_agent {
200        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
201    }
202    if let Some(ref apikey) = configuration.api_key {
203        let key = apikey.key.clone();
204        let value = match apikey.prefix {
205            Some(ref prefix) => format!("{} {}", prefix, key),
206            None => key,
207        };
208        req_builder = req_builder.header("Authorization", value);
209    };
210
211    let req = req_builder.build()?;
212    let resp = configuration.client.execute(req).await?;
213
214    let status = resp.status();
215    let content_type = resp
216        .headers()
217        .get("content-type")
218        .and_then(|v| v.to_str().ok())
219        .unwrap_or("application/octet-stream");
220    let content_type = super::ContentType::from(content_type);
221
222    if !status.is_client_error() && !status.is_server_error() {
223        let content = resp.text().await?;
224        match content_type {
225            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
226            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::Sighting&gt;`"))),
227            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::Sighting&gt;`")))),
228        }
229    } else {
230        let content = resp.text().await?;
231        let entity: Option<GetSightingsByEventIdError> = serde_json::from_str(&content).ok();
232        Err(Error::ResponseError(ResponseContent { status, content, entity }))
233    }
234}
235