hanzo_client/apis/
ask_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 PostAskError {
22 UnknownValue(serde_json::Value),
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum ResearchWebError {
29 UnknownValue(serde_json::Value),
30}
31
32
33pub async fn post_ask(configuration: &configuration::Configuration, ask_request: Option<models::AskRequest>) -> Result<(), Error<PostAskError>> {
35 let p_ask_request = ask_request;
37
38 let uri_str = format!("{}/v1/ask", configuration.base_path);
39 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
40
41 if let Some(ref user_agent) = configuration.user_agent {
42 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
43 }
44 if let Some(ref token) = configuration.bearer_access_token {
45 req_builder = req_builder.bearer_auth(token.to_owned());
46 };
47 req_builder = req_builder.json(&p_ask_request);
48
49 let req = req_builder.build()?;
50 let resp = configuration.client.execute(req).await?;
51
52 let status = resp.status();
53
54 if !status.is_client_error() && !status.is_server_error() {
55 Ok(())
56 } else {
57 let content = resp.text().await?;
58 let entity: Option<PostAskError> = serde_json::from_str(&content).ok();
59 Err(Error::ResponseError(ResponseContent { status, content, entity }))
60 }
61}
62
63pub async fn research_web(configuration: &configuration::Configuration, web_question: models::WebQuestion) -> Result<models::Report, Error<ResearchWebError>> {
65 let p_web_question = web_question;
67
68 let uri_str = format!("{}/v1/ask/web", configuration.base_path);
69 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
70
71 if let Some(ref user_agent) = configuration.user_agent {
72 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
73 }
74 if let Some(ref token) = configuration.bearer_access_token {
75 req_builder = req_builder.bearer_auth(token.to_owned());
76 };
77 req_builder = req_builder.json(&p_web_question);
78
79 let req = req_builder.build()?;
80 let resp = configuration.client.execute(req).await?;
81
82 let status = resp.status();
83 let content_type = resp
84 .headers()
85 .get("content-type")
86 .and_then(|v| v.to_str().ok())
87 .unwrap_or("application/octet-stream");
88 let content_type = super::ContentType::from(content_type);
89
90 if !status.is_client_error() && !status.is_server_error() {
91 let content = resp.text().await?;
92 match content_type {
93 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
94 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Report`"))),
95 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::Report`")))),
96 }
97 } else {
98 let content = resp.text().await?;
99 let entity: Option<ResearchWebError> = serde_json::from_str(&content).ok();
100 Err(Error::ResponseError(ResponseContent { status, content, entity }))
101 }
102}
103