thehive_client/apis/
query_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum FindEntitiesByQueryError {
22 Status400(models::ErrorResponse),
23 UnknownValue(serde_json::Value),
24}
25
26
27pub async fn find_entities_by_query(configuration: &configuration::Configuration, input_query: models::InputQuery, x_organisation: Option<&str>, name: Option<&str>) -> Result<models::FindEntitiesByQuery200Response, Error<FindEntitiesByQueryError>> {
29 let p_input_query = input_query;
31 let p_x_organisation = x_organisation;
32 let p_name = name;
33
34 let uri_str = format!("{}/v1/query", configuration.base_path);
35 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
36
37 if let Some(ref param_value) = p_name {
38 req_builder = req_builder.query(&[("name", ¶m_value.to_string())]);
39 }
40 if let Some(ref user_agent) = configuration.user_agent {
41 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
42 }
43 if let Some(param_value) = p_x_organisation {
44 req_builder = req_builder.header("X-Organisation", param_value.to_string());
45 }
46 if let Some(ref auth_conf) = configuration.basic_auth {
47 req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
48 };
49 if let Some(ref token) = configuration.bearer_access_token {
50 req_builder = req_builder.bearer_auth(token.to_owned());
51 };
52 req_builder = req_builder.json(&p_input_query);
53
54 let req = req_builder.build()?;
55 let resp = configuration.client.execute(req).await?;
56
57 let status = resp.status();
58 let content_type = resp
59 .headers()
60 .get("content-type")
61 .and_then(|v| v.to_str().ok())
62 .unwrap_or("application/octet-stream");
63 let content_type = super::ContentType::from(content_type);
64
65 if !status.is_client_error() && !status.is_server_error() {
66 let content = resp.text().await?;
67 match content_type {
68 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
69 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FindEntitiesByQuery200Response`"))),
70 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::FindEntitiesByQuery200Response`")))),
71 }
72 } else {
73 let content = resp.text().await?;
74 let entity: Option<FindEntitiesByQueryError> = serde_json::from_str(&content).ok();
75 Err(Error::ResponseError(ResponseContent { status, content, entity }))
76 }
77}
78