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