langfuse_client_base/apis/
comments_api.rs

1/*
2 * langfuse
3 *
4 * ## Authentication  Authenticate with the API using [Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication), get API keys in the project settings:  - username: Langfuse Public Key - password: Langfuse Secret Key  ## Exports  - OpenAPI spec: https://cloud.langfuse.com/generated/api/openapi.yml - Postman collection: https://cloud.langfuse.com/generated/postman/collection.json
5 *
6 * The version of the OpenAPI document:
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16/// struct for typed errors of method [`comments_create`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum CommentsCreateError {
20    Status400(serde_json::Value),
21    Status401(serde_json::Value),
22    Status403(serde_json::Value),
23    Status404(serde_json::Value),
24    Status405(serde_json::Value),
25    UnknownValue(serde_json::Value),
26}
27
28/// struct for typed errors of method [`comments_get`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum CommentsGetError {
32    Status400(serde_json::Value),
33    Status401(serde_json::Value),
34    Status403(serde_json::Value),
35    Status404(serde_json::Value),
36    Status405(serde_json::Value),
37    UnknownValue(serde_json::Value),
38}
39
40/// struct for typed errors of method [`comments_get_by_id`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum CommentsGetByIdError {
44    Status400(serde_json::Value),
45    Status401(serde_json::Value),
46    Status403(serde_json::Value),
47    Status404(serde_json::Value),
48    Status405(serde_json::Value),
49    UnknownValue(serde_json::Value),
50}
51
52/// Create a comment. Comments may be attached to different object types (trace, observation, session, prompt).
53pub async fn comments_create(
54    configuration: &configuration::Configuration,
55    create_comment_request: models::CreateCommentRequest,
56) -> Result<models::CreateCommentResponse, Error<CommentsCreateError>> {
57    // add a prefix to parameters to efficiently prevent name collisions
58    let p_body_create_comment_request = create_comment_request;
59
60    let uri_str = format!("{}/api/public/comments", configuration.base_path);
61    let mut req_builder = configuration
62        .client
63        .request(reqwest::Method::POST, &uri_str);
64
65    if let Some(ref user_agent) = configuration.user_agent {
66        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
67    }
68    if let Some(ref auth_conf) = configuration.basic_auth {
69        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
70    };
71    req_builder = req_builder.json(&p_body_create_comment_request);
72
73    let req = req_builder.build()?;
74    let resp = configuration.client.execute(req).await?;
75
76    let status = resp.status();
77    let content_type = resp
78        .headers()
79        .get("content-type")
80        .and_then(|v| v.to_str().ok())
81        .unwrap_or("application/octet-stream");
82    let content_type = super::ContentType::from(content_type);
83
84    if !status.is_client_error() && !status.is_server_error() {
85        let content = resp.text().await?;
86        match content_type {
87            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
88            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateCommentResponse`"))),
89            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::CreateCommentResponse`")))),
90        }
91    } else {
92        let content = resp.text().await?;
93        let entity: Option<CommentsCreateError> = serde_json::from_str(&content).ok();
94        Err(Error::ResponseError(ResponseContent {
95            status,
96            content,
97            entity,
98        }))
99    }
100}
101
102/// Get all comments
103pub async fn comments_get(
104    configuration: &configuration::Configuration,
105    page: Option<i32>,
106    limit: Option<i32>,
107    object_type: Option<&str>,
108    object_id: Option<&str>,
109    author_user_id: Option<&str>,
110) -> Result<models::GetCommentsResponse, Error<CommentsGetError>> {
111    // add a prefix to parameters to efficiently prevent name collisions
112    let p_query_page = page;
113    let p_query_limit = limit;
114    let p_query_object_type = object_type;
115    let p_query_object_id = object_id;
116    let p_query_author_user_id = author_user_id;
117
118    let uri_str = format!("{}/api/public/comments", configuration.base_path);
119    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
120
121    if let Some(ref param_value) = p_query_page {
122        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
123    }
124    if let Some(ref param_value) = p_query_limit {
125        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
126    }
127    if let Some(ref param_value) = p_query_object_type {
128        req_builder = req_builder.query(&[("objectType", &param_value.to_string())]);
129    }
130    if let Some(ref param_value) = p_query_object_id {
131        req_builder = req_builder.query(&[("objectId", &param_value.to_string())]);
132    }
133    if let Some(ref param_value) = p_query_author_user_id {
134        req_builder = req_builder.query(&[("authorUserId", &param_value.to_string())]);
135    }
136    if let Some(ref user_agent) = configuration.user_agent {
137        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
138    }
139    if let Some(ref auth_conf) = configuration.basic_auth {
140        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
141    };
142
143    let req = req_builder.build()?;
144    let resp = configuration.client.execute(req).await?;
145
146    let status = resp.status();
147    let content_type = resp
148        .headers()
149        .get("content-type")
150        .and_then(|v| v.to_str().ok())
151        .unwrap_or("application/octet-stream");
152    let content_type = super::ContentType::from(content_type);
153
154    if !status.is_client_error() && !status.is_server_error() {
155        let content = resp.text().await?;
156        match content_type {
157            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
158            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetCommentsResponse`"))),
159            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::GetCommentsResponse`")))),
160        }
161    } else {
162        let content = resp.text().await?;
163        let entity: Option<CommentsGetError> = serde_json::from_str(&content).ok();
164        Err(Error::ResponseError(ResponseContent {
165            status,
166            content,
167            entity,
168        }))
169    }
170}
171
172/// Get a comment by id
173pub async fn comments_get_by_id(
174    configuration: &configuration::Configuration,
175    comment_id: &str,
176) -> Result<models::Comment, Error<CommentsGetByIdError>> {
177    // add a prefix to parameters to efficiently prevent name collisions
178    let p_path_comment_id = comment_id;
179
180    let uri_str = format!(
181        "{}/api/public/comments/{commentId}",
182        configuration.base_path,
183        commentId = crate::apis::urlencode(p_path_comment_id)
184    );
185    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
186
187    if let Some(ref user_agent) = configuration.user_agent {
188        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
189    }
190    if let Some(ref auth_conf) = configuration.basic_auth {
191        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
192    };
193
194    let req = req_builder.build()?;
195    let resp = configuration.client.execute(req).await?;
196
197    let status = resp.status();
198    let content_type = resp
199        .headers()
200        .get("content-type")
201        .and_then(|v| v.to_str().ok())
202        .unwrap_or("application/octet-stream");
203    let content_type = super::ContentType::from(content_type);
204
205    if !status.is_client_error() && !status.is_server_error() {
206        let content = resp.text().await?;
207        match content_type {
208            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
209            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Comment`"))),
210            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::Comment`")))),
211        }
212    } else {
213        let content = resp.text().await?;
214        let entity: Option<CommentsGetByIdError> = serde_json::from_str(&content).ok();
215        Err(Error::ResponseError(ResponseContent {
216            status,
217            content,
218            entity,
219        }))
220    }
221}