langfuse_client_base/apis/
observations_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 [`observations_get`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum ObservationsGetError {
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 [`observations_get_many`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum ObservationsGetManyError {
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/// Get a observation
41#[bon::builder]
42pub async fn observations_get(
43    configuration: &configuration::Configuration,
44    observation_id: &str,
45) -> Result<models::ObservationsView, Error<ObservationsGetError>> {
46    // add a prefix to parameters to efficiently prevent name collisions
47    let p_path_observation_id = observation_id;
48
49    let uri_str = format!(
50        "{}/api/public/observations/{observationId}",
51        configuration.base_path,
52        observationId = crate::apis::urlencode(p_path_observation_id)
53    );
54    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
55
56    if let Some(ref user_agent) = configuration.user_agent {
57        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
58    }
59    if let Some(ref auth_conf) = configuration.basic_auth {
60        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
61    };
62
63    let req = req_builder.build()?;
64    let resp = configuration.client.execute(req).await?;
65
66    let status = resp.status();
67    let content_type = resp
68        .headers()
69        .get("content-type")
70        .and_then(|v| v.to_str().ok())
71        .unwrap_or("application/octet-stream");
72    let content_type = super::ContentType::from(content_type);
73
74    if !status.is_client_error() && !status.is_server_error() {
75        let content = resp.text().await?;
76        match content_type {
77            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
78            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ObservationsView`"))),
79            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::ObservationsView`")))),
80        }
81    } else {
82        let content = resp.text().await?;
83        let entity: Option<ObservationsGetError> = serde_json::from_str(&content).ok();
84        Err(Error::ResponseError(ResponseContent {
85            status,
86            content,
87            entity,
88        }))
89    }
90}
91
92/// Get a list of observations
93#[bon::builder]
94pub async fn observations_get_many(
95    configuration: &configuration::Configuration,
96    page: Option<i32>,
97    limit: Option<i32>,
98    name: Option<&str>,
99    user_id: Option<&str>,
100    r#type: Option<&str>,
101    trace_id: Option<&str>,
102    level: Option<models::ObservationLevel>,
103    parent_observation_id: Option<&str>,
104    environment: Option<Vec<String>>,
105    from_start_time: Option<String>,
106    to_start_time: Option<String>,
107    version: Option<&str>,
108) -> Result<models::ObservationsViews, Error<ObservationsGetManyError>> {
109    // add a prefix to parameters to efficiently prevent name collisions
110    let p_query_page = page;
111    let p_query_limit = limit;
112    let p_query_name = name;
113    let p_query_user_id = user_id;
114    let p_query_type = r#type;
115    let p_query_trace_id = trace_id;
116    let p_query_level = level;
117    let p_query_parent_observation_id = parent_observation_id;
118    let p_query_environment = environment;
119    let p_query_from_start_time = from_start_time;
120    let p_query_to_start_time = to_start_time;
121    let p_query_version = version;
122
123    let uri_str = format!("{}/api/public/observations", configuration.base_path);
124    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
125
126    if let Some(ref param_value) = p_query_page {
127        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
128    }
129    if let Some(ref param_value) = p_query_limit {
130        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
131    }
132    if let Some(ref param_value) = p_query_name {
133        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
134    }
135    if let Some(ref param_value) = p_query_user_id {
136        req_builder = req_builder.query(&[("userId", &param_value.to_string())]);
137    }
138    if let Some(ref param_value) = p_query_type {
139        req_builder = req_builder.query(&[("type", &param_value.to_string())]);
140    }
141    if let Some(ref param_value) = p_query_trace_id {
142        req_builder = req_builder.query(&[("traceId", &param_value.to_string())]);
143    }
144    if let Some(ref param_value) = p_query_level {
145        req_builder = req_builder.query(&[("level", &param_value.to_string())]);
146    }
147    if let Some(ref param_value) = p_query_parent_observation_id {
148        req_builder = req_builder.query(&[("parentObservationId", &param_value.to_string())]);
149    }
150    if let Some(ref param_value) = p_query_environment {
151        req_builder = match "multi" {
152            "multi" => req_builder.query(
153                &param_value
154                    .into_iter()
155                    .map(|p| ("environment".to_owned(), p.to_string()))
156                    .collect::<Vec<(std::string::String, std::string::String)>>(),
157            ),
158            _ => req_builder.query(&[(
159                "environment",
160                &param_value
161                    .into_iter()
162                    .map(|p| p.to_string())
163                    .collect::<Vec<String>>()
164                    .join(",")
165                    .to_string(),
166            )]),
167        };
168    }
169    if let Some(ref param_value) = p_query_from_start_time {
170        req_builder = req_builder.query(&[("fromStartTime", &param_value.to_string())]);
171    }
172    if let Some(ref param_value) = p_query_to_start_time {
173        req_builder = req_builder.query(&[("toStartTime", &param_value.to_string())]);
174    }
175    if let Some(ref param_value) = p_query_version {
176        req_builder = req_builder.query(&[("version", &param_value.to_string())]);
177    }
178    if let Some(ref user_agent) = configuration.user_agent {
179        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
180    }
181    if let Some(ref auth_conf) = configuration.basic_auth {
182        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
183    };
184
185    let req = req_builder.build()?;
186    let resp = configuration.client.execute(req).await?;
187
188    let status = resp.status();
189    let content_type = resp
190        .headers()
191        .get("content-type")
192        .and_then(|v| v.to_str().ok())
193        .unwrap_or("application/octet-stream");
194    let content_type = super::ContentType::from(content_type);
195
196    if !status.is_client_error() && !status.is_server_error() {
197        let content = resp.text().await?;
198        match content_type {
199            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
200            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ObservationsViews`"))),
201            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::ObservationsViews`")))),
202        }
203    } else {
204        let content = resp.text().await?;
205        let entity: Option<ObservationsGetManyError> = serde_json::from_str(&content).ok();
206        Err(Error::ResponseError(ResponseContent {
207            status,
208            content,
209            entity,
210        }))
211    }
212}