agentcat_api/apis/
events_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17#[derive(Clone, Debug)]
19pub struct PublishEventParams {
20 pub publish_event_request: Option<models::PublishEventRequest>
21}
22
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum PublishEventError {
28 Status400(serde_json::Value),
29 Status404(serde_json::Value),
30 Status401(models::Error),
31 Status403(models::Error),
32 UnknownValue(serde_json::Value),
33}
34
35
36pub async fn publish_event(configuration: &configuration::Configuration, params: PublishEventParams) -> Result<models::EventAcceptedResponse, Error<PublishEventError>> {
38
39 let uri_str = format!("{}/v1/public/events/", configuration.base_path);
40 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
41
42 if let Some(ref user_agent) = configuration.user_agent {
43 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
44 }
45 if let Some(ref apikey) = configuration.api_key {
46 let key = apikey.key.clone();
47 let value = match apikey.prefix {
48 Some(ref prefix) => format!("{} {}", prefix, key),
49 None => key,
50 };
51 req_builder = req_builder.header("Authorization", value);
52 };
53 req_builder = req_builder.json(¶ms.publish_event_request);
54
55 let req = req_builder.build()?;
56 let resp = configuration.client.execute(req).await?;
57
58 let status = resp.status();
59 let content_type = resp
60 .headers()
61 .get("content-type")
62 .and_then(|v| v.to_str().ok())
63 .unwrap_or("application/octet-stream");
64 let content_type = super::ContentType::from(content_type);
65
66 if !status.is_client_error() && !status.is_server_error() {
67 let content = resp.text().await?;
68 match content_type {
69 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
70 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::EventAcceptedResponse`"))),
71 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::EventAcceptedResponse`")))),
72 }
73 } else {
74 let content = resp.text().await?;
75 let entity: Option<PublishEventError> = serde_json::from_str(&content).ok();
76 Err(Error::ResponseError(ResponseContent { status, content, entity }))
77 }
78}
79