langfuse_client_base/apis/
sessions_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 [`sessions_get`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum SessionsGetError {
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 [`sessions_list`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum SessionsListError {
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 session. Please note that `traces` on this endpoint are not paginated, if you plan to fetch large sessions, consider `GET /api/public/traces?sessionId=<sessionId>`
41#[bon::builder]
42pub async fn sessions_get(
43    configuration: &configuration::Configuration,
44    session_id: &str,
45) -> Result<models::SessionWithTraces, Error<SessionsGetError>> {
46    // add a prefix to parameters to efficiently prevent name collisions
47    let p_path_session_id = session_id;
48
49    let uri_str = format!(
50        "{}/api/public/sessions/{sessionId}",
51        configuration.base_path,
52        sessionId = crate::apis::urlencode(p_path_session_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::SessionWithTraces`"))),
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::SessionWithTraces`")))),
80        }
81    } else {
82        let content = resp.text().await?;
83        let entity: Option<SessionsGetError> = serde_json::from_str(&content).ok();
84        Err(Error::ResponseError(ResponseContent {
85            status,
86            content,
87            entity,
88        }))
89    }
90}
91
92/// Get sessions
93#[bon::builder]
94pub async fn sessions_list(
95    configuration: &configuration::Configuration,
96    page: Option<i32>,
97    limit: Option<i32>,
98    from_timestamp: Option<String>,
99    to_timestamp: Option<String>,
100    environment: Option<Vec<String>>,
101) -> Result<models::PaginatedSessions, Error<SessionsListError>> {
102    // add a prefix to parameters to efficiently prevent name collisions
103    let p_query_page = page;
104    let p_query_limit = limit;
105    let p_query_from_timestamp = from_timestamp;
106    let p_query_to_timestamp = to_timestamp;
107    let p_query_environment = environment;
108
109    let uri_str = format!("{}/api/public/sessions", configuration.base_path);
110    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
111
112    if let Some(ref param_value) = p_query_page {
113        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
114    }
115    if let Some(ref param_value) = p_query_limit {
116        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
117    }
118    if let Some(ref param_value) = p_query_from_timestamp {
119        req_builder = req_builder.query(&[("fromTimestamp", &param_value.to_string())]);
120    }
121    if let Some(ref param_value) = p_query_to_timestamp {
122        req_builder = req_builder.query(&[("toTimestamp", &param_value.to_string())]);
123    }
124    if let Some(ref param_value) = p_query_environment {
125        req_builder = match "multi" {
126            "multi" => req_builder.query(
127                &param_value
128                    .into_iter()
129                    .map(|p| ("environment".to_owned(), p.to_string()))
130                    .collect::<Vec<(std::string::String, std::string::String)>>(),
131            ),
132            _ => req_builder.query(&[(
133                "environment",
134                &param_value
135                    .into_iter()
136                    .map(|p| p.to_string())
137                    .collect::<Vec<String>>()
138                    .join(",")
139                    .to_string(),
140            )]),
141        };
142    }
143    if let Some(ref user_agent) = configuration.user_agent {
144        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
145    }
146    if let Some(ref auth_conf) = configuration.basic_auth {
147        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
148    };
149
150    let req = req_builder.build()?;
151    let resp = configuration.client.execute(req).await?;
152
153    let status = resp.status();
154    let content_type = resp
155        .headers()
156        .get("content-type")
157        .and_then(|v| v.to_str().ok())
158        .unwrap_or("application/octet-stream");
159    let content_type = super::ContentType::from(content_type);
160
161    if !status.is_client_error() && !status.is_server_error() {
162        let content = resp.text().await?;
163        match content_type {
164            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
165            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PaginatedSessions`"))),
166            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::PaginatedSessions`")))),
167        }
168    } else {
169        let content = resp.text().await?;
170        let entity: Option<SessionsListError> = serde_json::from_str(&content).ok();
171        Err(Error::ResponseError(ResponseContent {
172            status,
173            content,
174            entity,
175        }))
176    }
177}