langfuse_client/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
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`sessions_get`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum SessionsGetError {
22    Status400(serde_json::Value),
23    Status401(serde_json::Value),
24    Status403(serde_json::Value),
25    Status404(serde_json::Value),
26    Status405(serde_json::Value),
27    UnknownValue(serde_json::Value),
28}
29
30/// struct for typed errors of method [`sessions_list`]
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(untagged)]
33pub enum SessionsListError {
34    Status400(serde_json::Value),
35    Status401(serde_json::Value),
36    Status403(serde_json::Value),
37    Status404(serde_json::Value),
38    Status405(serde_json::Value),
39    UnknownValue(serde_json::Value),
40}
41
42
43/// 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>`
44pub async fn sessions_get(configuration: &configuration::Configuration, session_id: &str) -> Result<models::SessionWithTraces, Error<SessionsGetError>> {
45    // add a prefix to parameters to efficiently prevent name collisions
46    let p_session_id = session_id;
47
48    let uri_str = format!("{}/api/public/sessions/{sessionId}", configuration.base_path, sessionId=crate::apis::urlencode(p_session_id));
49    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
50
51    if let Some(ref user_agent) = configuration.user_agent {
52        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
53    }
54    if let Some(ref auth_conf) = configuration.basic_auth {
55        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
56    };
57
58    let req = req_builder.build()?;
59    let resp = configuration.client.execute(req).await?;
60
61    let status = resp.status();
62    let content_type = resp
63        .headers()
64        .get("content-type")
65        .and_then(|v| v.to_str().ok())
66        .unwrap_or("application/octet-stream");
67    let content_type = super::ContentType::from(content_type);
68
69    if !status.is_client_error() && !status.is_server_error() {
70        let content = resp.text().await?;
71        match content_type {
72            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
73            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SessionWithTraces`"))),
74            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`")))),
75        }
76    } else {
77        let content = resp.text().await?;
78        let entity: Option<SessionsGetError> = serde_json::from_str(&content).ok();
79        Err(Error::ResponseError(ResponseContent { status, content, entity }))
80    }
81}
82
83/// Get sessions
84pub async fn sessions_list(configuration: &configuration::Configuration, page: Option<i32>, limit: Option<i32>, from_timestamp: Option<String>, to_timestamp: Option<String>, environment: Option<Vec<String>>) -> Result<models::PaginatedSessions, Error<SessionsListError>> {
85    // add a prefix to parameters to efficiently prevent name collisions
86    let p_page = page;
87    let p_limit = limit;
88    let p_from_timestamp = from_timestamp;
89    let p_to_timestamp = to_timestamp;
90    let p_environment = environment;
91
92    let uri_str = format!("{}/api/public/sessions", configuration.base_path);
93    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
94
95    if let Some(ref param_value) = p_page {
96        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
97    }
98    if let Some(ref param_value) = p_limit {
99        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
100    }
101    if let Some(ref param_value) = p_from_timestamp {
102        req_builder = req_builder.query(&[("fromTimestamp", &param_value.to_string())]);
103    }
104    if let Some(ref param_value) = p_to_timestamp {
105        req_builder = req_builder.query(&[("toTimestamp", &param_value.to_string())]);
106    }
107    if let Some(ref param_value) = p_environment {
108        req_builder = match "multi" {
109            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("environment".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
110            _ => req_builder.query(&[("environment", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
111        };
112    }
113    if let Some(ref user_agent) = configuration.user_agent {
114        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
115    }
116    if let Some(ref auth_conf) = configuration.basic_auth {
117        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
118    };
119
120    let req = req_builder.build()?;
121    let resp = configuration.client.execute(req).await?;
122
123    let status = resp.status();
124    let content_type = resp
125        .headers()
126        .get("content-type")
127        .and_then(|v| v.to_str().ok())
128        .unwrap_or("application/octet-stream");
129    let content_type = super::ContentType::from(content_type);
130
131    if !status.is_client_error() && !status.is_server_error() {
132        let content = resp.text().await?;
133        match content_type {
134            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
135            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PaginatedSessions`"))),
136            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`")))),
137        }
138    } else {
139        let content = resp.text().await?;
140        let entity: Option<SessionsListError> = serde_json::from_str(&content).ok();
141        Err(Error::ResponseError(ResponseContent { status, content, entity }))
142    }
143}
144