langfuse_client_base/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
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 [`score_create`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum ScoreCreateError {
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 [`score_delete`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum ScoreDeleteError {
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/// Create a score (supports both trace and session scores)
41#[bon::builder]
42pub async fn score_create(
43    configuration: &configuration::Configuration,
44    create_score_request: models::CreateScoreRequest,
45) -> Result<models::CreateScoreResponse, Error<ScoreCreateError>> {
46    // add a prefix to parameters to efficiently prevent name collisions
47    let p_body_create_score_request = create_score_request;
48
49    let uri_str = format!("{}/api/public/scores", configuration.base_path);
50    let mut req_builder = configuration
51        .client
52        .request(reqwest::Method::POST, &uri_str);
53
54    if let Some(ref user_agent) = configuration.user_agent {
55        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
56    }
57    if let Some(ref auth_conf) = configuration.basic_auth {
58        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
59    };
60    req_builder = req_builder.json(&p_body_create_score_request);
61
62    let req = req_builder.build()?;
63    let resp = configuration.client.execute(req).await?;
64
65    let status = resp.status();
66    let content_type = resp
67        .headers()
68        .get("content-type")
69        .and_then(|v| v.to_str().ok())
70        .unwrap_or("application/octet-stream");
71    let content_type = super::ContentType::from(content_type);
72
73    if !status.is_client_error() && !status.is_server_error() {
74        let content = resp.text().await?;
75        match content_type {
76            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
77            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateScoreResponse`"))),
78            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`")))),
79        }
80    } else {
81        let content = resp.text().await?;
82        let entity: Option<ScoreCreateError> = serde_json::from_str(&content).ok();
83        Err(Error::ResponseError(ResponseContent {
84            status,
85            content,
86            entity,
87        }))
88    }
89}
90
91/// Delete a score (supports both trace and session scores)
92#[bon::builder]
93pub async fn score_delete(
94    configuration: &configuration::Configuration,
95    score_id: &str,
96) -> Result<(), Error<ScoreDeleteError>> {
97    // add a prefix to parameters to efficiently prevent name collisions
98    let p_path_score_id = score_id;
99
100    let uri_str = format!(
101        "{}/api/public/scores/{scoreId}",
102        configuration.base_path,
103        scoreId = crate::apis::urlencode(p_path_score_id)
104    );
105    let mut req_builder = configuration
106        .client
107        .request(reqwest::Method::DELETE, &uri_str);
108
109    if let Some(ref user_agent) = configuration.user_agent {
110        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
111    }
112    if let Some(ref auth_conf) = configuration.basic_auth {
113        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
114    };
115
116    let req = req_builder.build()?;
117    let resp = configuration.client.execute(req).await?;
118
119    let status = resp.status();
120
121    if !status.is_client_error() && !status.is_server_error() {
122        Ok(())
123    } else {
124        let content = resp.text().await?;
125        let entity: Option<ScoreDeleteError> = serde_json::from_str(&content).ok();
126        Err(Error::ResponseError(ResponseContent {
127            status,
128            content,
129            entity,
130        }))
131    }
132}