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