langsmith_rust/client/
http.rs1use crate::config::Config;
2use crate::error::{LangSmithError, Result};
3use crate::models::run::{Run, RunUpdate};
4use reqwest::Client;
5use uuid::Uuid;
6
7pub struct LangSmithClient {
8 client: Client,
9 config: Config,
10}
11
12impl LangSmithClient {
13 pub fn new() -> Result<Self> {
14 let config = Config::get()?;
15 let client = Client::new();
16 Ok(Self { client, config })
17 }
18
19 pub fn with_config(config: Config) -> Self {
20 let client = Client::new();
21 Self { client, config }
22 }
23
24 pub async fn post_run(&self, run: &Run) -> Result<()> {
25 if !self.config.tracing_enabled {
26 return Err(LangSmithError::TracingDisabled);
27 }
28
29 let url = format!("{}/runs", self.config.endpoint);
30
31 let mut request = self
32 .client
33 .post(&url)
34 .header("x-api-key", &self.config.api_key)
35 .json(run);
36
37 if let Some(tenant_id) = &self.config.tenant_id {
38 request = request.header("x-tenant-id", tenant_id);
39 }
40
41 let response = request.send().await?;
42
43 if !response.status().is_success() {
44 let status = response.status();
45 let text = response.text().await.unwrap_or_default();
46 return Err(LangSmithError::Other(format!(
47 "HTTP {}: {}",
48 status.as_u16(),
49 text
50 )));
51 }
52
53 Ok(())
54 }
55
56 pub async fn patch_run(&self, run_id: Uuid, updates: &RunUpdate) -> Result<()> {
57 if !self.config.tracing_enabled {
58 return Err(LangSmithError::TracingDisabled);
59 }
60
61 let url = format!("{}/runs/{}", self.config.endpoint, run_id);
62
63 let mut request = self
64 .client
65 .patch(&url)
66 .header("x-api-key", &self.config.api_key)
67 .json(updates);
68
69 if let Some(tenant_id) = &self.config.tenant_id {
70 request = request.header("x-tenant-id", tenant_id);
71 }
72
73 let response = request.send().await?;
74
75 if !response.status().is_success() {
76 let status = response.status();
77 let text = response.text().await.unwrap_or_default();
78 return Err(LangSmithError::Other(format!(
79 "HTTP {}: {}",
80 status.as_u16(),
81 text
82 )));
83 }
84
85 Ok(())
86 }
87}
88