cortex_client/apis/
stream_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 CreateStreamError {
22 Status401(models::Error),
23 Status403(models::Error),
24 UnknownValue(serde_json::Value),
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum GetStreamEventsError {
31 Status400(models::Error),
32 Status401(models::Error),
33 Status500(),
34 UnknownValue(serde_json::Value),
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum GetStreamSessionStatusError {
41 UnknownValue(serde_json::Value),
42}
43
44
45pub async fn create_stream(configuration: &configuration::Configuration, ) -> Result<String, Error<CreateStreamError>> {
46
47 let uri_str = format!("{}/stream", configuration.base_path);
48 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
49
50 if let Some(ref user_agent) = configuration.user_agent {
51 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
52 }
53 if let Some(ref token) = configuration.bearer_access_token {
54 req_builder = req_builder.bearer_auth(token.to_owned());
55 };
56
57 let req = req_builder.build()?;
58 let resp = configuration.client.execute(req).await?;
59
60 let status = resp.status();
61 let content_type = resp
62 .headers()
63 .get("content-type")
64 .and_then(|v| v.to_str().ok())
65 .unwrap_or("application/octet-stream");
66 let content_type = super::ContentType::from(content_type);
67
68 if !status.is_client_error() && !status.is_server_error() {
69 let content = resp.text().await?;
70 match content_type {
71 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
72 ContentType::Text => return Ok(content),
73 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `String`")))),
74 }
75 } else {
76 let content = resp.text().await?;
77 let entity: Option<CreateStreamError> = serde_json::from_str(&content).ok();
78 Err(Error::ResponseError(ResponseContent { status, content, entity }))
79 }
80}
81
82pub async fn get_stream_events(configuration: &configuration::Configuration, id: &str) -> Result<models::StreamMessagesResponse, Error<GetStreamEventsError>> {
83 let p_id = id;
85
86 let uri_str = format!("{}/stream/{id}", configuration.base_path, id=crate::apis::urlencode(p_id));
87 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
88
89 if let Some(ref user_agent) = configuration.user_agent {
90 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
91 }
92 if let Some(ref token) = configuration.bearer_access_token {
93 req_builder = req_builder.bearer_auth(token.to_owned());
94 };
95
96 let req = req_builder.build()?;
97 let resp = configuration.client.execute(req).await?;
98
99 let status = resp.status();
100 let content_type = resp
101 .headers()
102 .get("content-type")
103 .and_then(|v| v.to_str().ok())
104 .unwrap_or("application/octet-stream");
105 let content_type = super::ContentType::from(content_type);
106
107 if !status.is_client_error() && !status.is_server_error() {
108 let content = resp.text().await?;
109 match content_type {
110 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
111 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::StreamMessagesResponse`"))),
112 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::StreamMessagesResponse`")))),
113 }
114 } else {
115 let content = resp.text().await?;
116 let entity: Option<GetStreamEventsError> = serde_json::from_str(&content).ok();
117 Err(Error::ResponseError(ResponseContent { status, content, entity }))
118 }
119}
120
121pub async fn get_stream_session_status(configuration: &configuration::Configuration, ) -> Result<models::StreamStatusResponse, Error<GetStreamSessionStatusError>> {
122
123 let uri_str = format!("{}/stream/status", configuration.base_path);
124 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
125
126 if let Some(ref user_agent) = configuration.user_agent {
127 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
128 }
129 if let Some(ref token) = configuration.bearer_access_token {
130 req_builder = req_builder.bearer_auth(token.to_owned());
131 };
132
133 let req = req_builder.build()?;
134 let resp = configuration.client.execute(req).await?;
135
136 let status = resp.status();
137 let content_type = resp
138 .headers()
139 .get("content-type")
140 .and_then(|v| v.to_str().ok())
141 .unwrap_or("application/octet-stream");
142 let content_type = super::ContentType::from(content_type);
143
144 if !status.is_client_error() && !status.is_server_error() {
145 let content = resp.text().await?;
146 match content_type {
147 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
148 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::StreamStatusResponse`"))),
149 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::StreamStatusResponse`")))),
150 }
151 } else {
152 let content = resp.text().await?;
153 let entity: Option<GetStreamSessionStatusError> = serde_json::from_str(&content).ok();
154 Err(Error::ResponseError(ResponseContent { status, content, entity }))
155 }
156}
157