langfuse_rs/apis/
trace_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, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{Deserialize, Serialize};
15
16/// struct for typed errors of method [`trace_get`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum TraceGetError {
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 [`trace_list`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum TraceListError {
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 specific trace
41pub async fn trace_get(configuration: &configuration::Configuration, trace_id: &str) -> Result<models::TraceWithFullDetails, Error<TraceGetError>> {
42	// add a prefix to parameters to efficiently prevent name collisions
43	let p_trace_id = trace_id;
44
45	let uri_str = format!(
46		"{}/api/public/traces/{traceId}",
47		configuration.base_path,
48		traceId = crate::apis::urlencode(p_trace_id)
49	);
50	let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
51
52	if let Some(ref user_agent) = configuration.user_agent {
53		req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
54	}
55	if let Some(ref auth_conf) = configuration.basic_auth {
56		req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
57	};
58
59	let req = req_builder.build()?;
60	let resp = configuration.client.execute(req).await?;
61
62	let status = resp.status();
63
64	if !status.is_client_error() && !status.is_server_error() {
65		let content = resp.text().await?;
66		serde_json::from_str(&content).map_err(Error::from)
67	} else {
68		let content = resp.text().await?;
69		let entity: Option<TraceGetError> = serde_json::from_str(&content).ok();
70		Err(Error::ResponseError(ResponseContent { status, content, entity }))
71	}
72}
73
74/// Get list of traces
75pub async fn trace_list(
76	configuration: &configuration::Configuration,
77	page: Option<i32>,
78	limit: Option<i32>,
79	user_id: Option<&str>,
80	name: Option<&str>,
81	session_id: Option<&str>,
82	from_timestamp: Option<String>,
83	to_timestamp: Option<String>,
84	order_by: Option<&str>,
85	tags: Option<Vec<String>>,
86	version: Option<&str>,
87	release: Option<&str>,
88) -> Result<models::Traces, Error<TraceListError>> {
89	// add a prefix to parameters to efficiently prevent name collisions
90	let p_page = page;
91	let p_limit = limit;
92	let p_user_id = user_id;
93	let p_name = name;
94	let p_session_id = session_id;
95	let p_from_timestamp = from_timestamp;
96	let p_to_timestamp = to_timestamp;
97	let p_order_by = order_by;
98	let p_tags = tags;
99	let p_version = version;
100	let p_release = release;
101
102	let uri_str = format!("{}/api/public/traces", configuration.base_path);
103	let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
104
105	if let Some(ref param_value) = p_page {
106		req_builder = req_builder.query(&[("page", &param_value.to_string())]);
107	}
108	if let Some(ref param_value) = p_limit {
109		req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
110	}
111	if let Some(ref param_value) = p_user_id {
112		req_builder = req_builder.query(&[("userId", &param_value.to_string())]);
113	}
114	if let Some(ref param_value) = p_name {
115		req_builder = req_builder.query(&[("name", &param_value.to_string())]);
116	}
117	if let Some(ref param_value) = p_session_id {
118		req_builder = req_builder.query(&[("sessionId", &param_value.to_string())]);
119	}
120	if let Some(ref param_value) = p_from_timestamp {
121		req_builder = req_builder.query(&[("fromTimestamp", &param_value.to_string())]);
122	}
123	if let Some(ref param_value) = p_to_timestamp {
124		req_builder = req_builder.query(&[("toTimestamp", &param_value.to_string())]);
125	}
126	if let Some(ref param_value) = p_order_by {
127		req_builder = req_builder.query(&[("orderBy", &param_value.to_string())]);
128	}
129	if let Some(ref param_value) = p_tags {
130		req_builder = match "multi" {
131			"multi" => req_builder.query(
132				&param_value
133					.iter()
134					.map(|p| ("tags".to_owned(), p.to_string()))
135					.collect::<Vec<(std::string::String, std::string::String)>>(),
136			),
137			_ => req_builder.query(&[(
138				"tags",
139				&param_value.iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string(),
140			)]),
141		};
142	}
143	if let Some(ref param_value) = p_version {
144		req_builder = req_builder.query(&[("version", &param_value.to_string())]);
145	}
146	if let Some(ref param_value) = p_release {
147		req_builder = req_builder.query(&[("release", &param_value.to_string())]);
148	}
149	if let Some(ref user_agent) = configuration.user_agent {
150		req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
151	}
152	if let Some(ref auth_conf) = configuration.basic_auth {
153		req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
154	};
155
156	let req = req_builder.build()?;
157	let resp = configuration.client.execute(req).await?;
158
159	let status = resp.status();
160
161	if !status.is_client_error() && !status.is_server_error() {
162		let content = resp.text().await?;
163		serde_json::from_str(&content).map_err(Error::from)
164	} else {
165		let content = resp.text().await?;
166		let entity: Option<TraceListError> = serde_json::from_str(&content).ok();
167		Err(Error::ResponseError(ResponseContent { status, content, entity }))
168	}
169}