langfuse_client/apis/
score_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
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`score_create`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum ScoreCreateError {
22    Status400(serde_json::Value),
23    Status401(serde_json::Value),
24    Status403(serde_json::Value),
25    Status404(serde_json::Value),
26    Status405(serde_json::Value),
27    UnknownValue(serde_json::Value),
28}
29
30/// struct for typed errors of method [`score_delete`]
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(untagged)]
33pub enum ScoreDeleteError {
34    Status400(serde_json::Value),
35    Status401(serde_json::Value),
36    Status403(serde_json::Value),
37    Status404(serde_json::Value),
38    Status405(serde_json::Value),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method [`score_get`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum ScoreGetError {
46    Status400(serde_json::Value),
47    Status401(serde_json::Value),
48    Status403(serde_json::Value),
49    Status404(serde_json::Value),
50    Status405(serde_json::Value),
51    UnknownValue(serde_json::Value),
52}
53
54/// struct for typed errors of method [`score_get_by_id`]
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(untagged)]
57pub enum ScoreGetByIdError {
58    Status400(serde_json::Value),
59    Status401(serde_json::Value),
60    Status403(serde_json::Value),
61    Status404(serde_json::Value),
62    Status405(serde_json::Value),
63    UnknownValue(serde_json::Value),
64}
65
66
67/// Create a score
68pub async fn score_create(configuration: &configuration::Configuration, create_score_request: models::CreateScoreRequest) -> Result<models::CreateScoreResponse, Error<ScoreCreateError>> {
69    // add a prefix to parameters to efficiently prevent name collisions
70    let p_create_score_request = create_score_request;
71
72    let uri_str = format!("{}/api/public/scores", configuration.base_path);
73    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
74
75    if let Some(ref user_agent) = configuration.user_agent {
76        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
77    }
78    if let Some(ref auth_conf) = configuration.basic_auth {
79        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
80    };
81    req_builder = req_builder.json(&p_create_score_request);
82
83    let req = req_builder.build()?;
84    let resp = configuration.client.execute(req).await?;
85
86    let status = resp.status();
87    let content_type = resp
88        .headers()
89        .get("content-type")
90        .and_then(|v| v.to_str().ok())
91        .unwrap_or("application/octet-stream");
92    let content_type = super::ContentType::from(content_type);
93
94    if !status.is_client_error() && !status.is_server_error() {
95        let content = resp.text().await?;
96        match content_type {
97            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
98            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateScoreResponse`"))),
99            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::CreateScoreResponse`")))),
100        }
101    } else {
102        let content = resp.text().await?;
103        let entity: Option<ScoreCreateError> = serde_json::from_str(&content).ok();
104        Err(Error::ResponseError(ResponseContent { status, content, entity }))
105    }
106}
107
108/// Delete a score
109pub async fn score_delete(configuration: &configuration::Configuration, score_id: &str) -> Result<(), Error<ScoreDeleteError>> {
110    // add a prefix to parameters to efficiently prevent name collisions
111    let p_score_id = score_id;
112
113    let uri_str = format!("{}/api/public/scores/{scoreId}", configuration.base_path, scoreId=crate::apis::urlencode(p_score_id));
114    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
115
116    if let Some(ref user_agent) = configuration.user_agent {
117        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
118    }
119    if let Some(ref auth_conf) = configuration.basic_auth {
120        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
121    };
122
123    let req = req_builder.build()?;
124    let resp = configuration.client.execute(req).await?;
125
126    let status = resp.status();
127
128    if !status.is_client_error() && !status.is_server_error() {
129        Ok(())
130    } else {
131        let content = resp.text().await?;
132        let entity: Option<ScoreDeleteError> = serde_json::from_str(&content).ok();
133        Err(Error::ResponseError(ResponseContent { status, content, entity }))
134    }
135}
136
137/// Get a list of scores
138pub async fn score_get(configuration: &configuration::Configuration, page: Option<i32>, limit: Option<i32>, user_id: Option<&str>, name: Option<&str>, from_timestamp: Option<String>, to_timestamp: Option<String>, environment: Option<Vec<String>>, source: Option<models::ScoreSource>, operator: Option<&str>, value: Option<f64>, score_ids: Option<&str>, config_id: Option<&str>, queue_id: Option<&str>, data_type: Option<models::ScoreDataType>, trace_tags: Option<Vec<String>>) -> Result<models::GetScoresResponse, Error<ScoreGetError>> {
139    // add a prefix to parameters to efficiently prevent name collisions
140    let p_page = page;
141    let p_limit = limit;
142    let p_user_id = user_id;
143    let p_name = name;
144    let p_from_timestamp = from_timestamp;
145    let p_to_timestamp = to_timestamp;
146    let p_environment = environment;
147    let p_source = source;
148    let p_operator = operator;
149    let p_value = value;
150    let p_score_ids = score_ids;
151    let p_config_id = config_id;
152    let p_queue_id = queue_id;
153    let p_data_type = data_type;
154    let p_trace_tags = trace_tags;
155
156    let uri_str = format!("{}/api/public/scores", configuration.base_path);
157    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
158
159    if let Some(ref param_value) = p_page {
160        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
161    }
162    if let Some(ref param_value) = p_limit {
163        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
164    }
165    if let Some(ref param_value) = p_user_id {
166        req_builder = req_builder.query(&[("userId", &param_value.to_string())]);
167    }
168    if let Some(ref param_value) = p_name {
169        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
170    }
171    if let Some(ref param_value) = p_from_timestamp {
172        req_builder = req_builder.query(&[("fromTimestamp", &param_value.to_string())]);
173    }
174    if let Some(ref param_value) = p_to_timestamp {
175        req_builder = req_builder.query(&[("toTimestamp", &param_value.to_string())]);
176    }
177    if let Some(ref param_value) = p_environment {
178        req_builder = match "multi" {
179            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("environment".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
180            _ => req_builder.query(&[("environment", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
181        };
182    }
183    if let Some(ref param_value) = p_source {
184        req_builder = req_builder.query(&[("source", &param_value.to_string())]);
185    }
186    if let Some(ref param_value) = p_operator {
187        req_builder = req_builder.query(&[("operator", &param_value.to_string())]);
188    }
189    if let Some(ref param_value) = p_value {
190        req_builder = req_builder.query(&[("value", &param_value.to_string())]);
191    }
192    if let Some(ref param_value) = p_score_ids {
193        req_builder = req_builder.query(&[("scoreIds", &param_value.to_string())]);
194    }
195    if let Some(ref param_value) = p_config_id {
196        req_builder = req_builder.query(&[("configId", &param_value.to_string())]);
197    }
198    if let Some(ref param_value) = p_queue_id {
199        req_builder = req_builder.query(&[("queueId", &param_value.to_string())]);
200    }
201    if let Some(ref param_value) = p_data_type {
202        req_builder = req_builder.query(&[("dataType", &param_value.to_string())]);
203    }
204    if let Some(ref param_value) = p_trace_tags {
205        req_builder = match "multi" {
206            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("traceTags".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
207            _ => req_builder.query(&[("traceTags", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
208        };
209    }
210    if let Some(ref user_agent) = configuration.user_agent {
211        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
212    }
213    if let Some(ref auth_conf) = configuration.basic_auth {
214        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
215    };
216
217    let req = req_builder.build()?;
218    let resp = configuration.client.execute(req).await?;
219
220    let status = resp.status();
221    let content_type = resp
222        .headers()
223        .get("content-type")
224        .and_then(|v| v.to_str().ok())
225        .unwrap_or("application/octet-stream");
226    let content_type = super::ContentType::from(content_type);
227
228    if !status.is_client_error() && !status.is_server_error() {
229        let content = resp.text().await?;
230        match content_type {
231            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
232            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetScoresResponse`"))),
233            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::GetScoresResponse`")))),
234        }
235    } else {
236        let content = resp.text().await?;
237        let entity: Option<ScoreGetError> = serde_json::from_str(&content).ok();
238        Err(Error::ResponseError(ResponseContent { status, content, entity }))
239    }
240}
241
242/// Get a score
243pub async fn score_get_by_id(configuration: &configuration::Configuration, score_id: &str) -> Result<models::Score, Error<ScoreGetByIdError>> {
244    // add a prefix to parameters to efficiently prevent name collisions
245    let p_score_id = score_id;
246
247    let uri_str = format!("{}/api/public/scores/{scoreId}", configuration.base_path, scoreId=crate::apis::urlencode(p_score_id));
248    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
249
250    if let Some(ref user_agent) = configuration.user_agent {
251        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
252    }
253    if let Some(ref auth_conf) = configuration.basic_auth {
254        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
255    };
256
257    let req = req_builder.build()?;
258    let resp = configuration.client.execute(req).await?;
259
260    let status = resp.status();
261    let content_type = resp
262        .headers()
263        .get("content-type")
264        .and_then(|v| v.to_str().ok())
265        .unwrap_or("application/octet-stream");
266    let content_type = super::ContentType::from(content_type);
267
268    if !status.is_client_error() && !status.is_server_error() {
269        let content = resp.text().await?;
270        match content_type {
271            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
272            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Score`"))),
273            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::Score`")))),
274        }
275    } else {
276        let content = resp.text().await?;
277        let entity: Option<ScoreGetByIdError> = serde_json::from_str(&content).ok();
278        Err(Error::ResponseError(ResponseContent { status, content, entity }))
279    }
280}
281