langfuse_client_base/apis/
media_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 [`media_get`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum MediaGetError {
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 [`media_get_upload_url`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum MediaGetUploadUrlError {
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/// struct for typed errors of method [`media_patch`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum MediaPatchError {
44    Status400(serde_json::Value),
45    Status401(serde_json::Value),
46    Status403(serde_json::Value),
47    Status404(serde_json::Value),
48    Status405(serde_json::Value),
49    UnknownValue(serde_json::Value),
50}
51
52/// Get a media record
53#[bon::builder]
54pub async fn media_get(
55    configuration: &configuration::Configuration,
56    media_id: &str,
57) -> Result<models::GetMediaResponse, Error<MediaGetError>> {
58    // add a prefix to parameters to efficiently prevent name collisions
59    let p_path_media_id = media_id;
60
61    let uri_str = format!(
62        "{}/api/public/media/{mediaId}",
63        configuration.base_path,
64        mediaId = crate::apis::urlencode(p_path_media_id)
65    );
66    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
67
68    if let Some(ref user_agent) = configuration.user_agent {
69        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
70    }
71    if let Some(ref auth_conf) = configuration.basic_auth {
72        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
73    };
74
75    let req = req_builder.build()?;
76    let resp = configuration.client.execute(req).await?;
77
78    let status = resp.status();
79    let content_type = resp
80        .headers()
81        .get("content-type")
82        .and_then(|v| v.to_str().ok())
83        .unwrap_or("application/octet-stream");
84    let content_type = super::ContentType::from(content_type);
85
86    if !status.is_client_error() && !status.is_server_error() {
87        let content = resp.text().await?;
88        match content_type {
89            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
90            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetMediaResponse`"))),
91            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::GetMediaResponse`")))),
92        }
93    } else {
94        let content = resp.text().await?;
95        let entity: Option<MediaGetError> = serde_json::from_str(&content).ok();
96        Err(Error::ResponseError(ResponseContent {
97            status,
98            content,
99            entity,
100        }))
101    }
102}
103
104/// Get a presigned upload URL for a media record
105#[bon::builder]
106pub async fn media_get_upload_url(
107    configuration: &configuration::Configuration,
108    get_media_upload_url_request: models::GetMediaUploadUrlRequest,
109) -> Result<models::GetMediaUploadUrlResponse, Error<MediaGetUploadUrlError>> {
110    // add a prefix to parameters to efficiently prevent name collisions
111    let p_body_get_media_upload_url_request = get_media_upload_url_request;
112
113    let uri_str = format!("{}/api/public/media", configuration.base_path);
114    let mut req_builder = configuration
115        .client
116        .request(reqwest::Method::POST, &uri_str);
117
118    if let Some(ref user_agent) = configuration.user_agent {
119        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
120    }
121    if let Some(ref auth_conf) = configuration.basic_auth {
122        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
123    };
124    req_builder = req_builder.json(&p_body_get_media_upload_url_request);
125
126    let req = req_builder.build()?;
127    let resp = configuration.client.execute(req).await?;
128
129    let status = resp.status();
130    let content_type = resp
131        .headers()
132        .get("content-type")
133        .and_then(|v| v.to_str().ok())
134        .unwrap_or("application/octet-stream");
135    let content_type = super::ContentType::from(content_type);
136
137    if !status.is_client_error() && !status.is_server_error() {
138        let content = resp.text().await?;
139        match content_type {
140            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
141            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetMediaUploadUrlResponse`"))),
142            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::GetMediaUploadUrlResponse`")))),
143        }
144    } else {
145        let content = resp.text().await?;
146        let entity: Option<MediaGetUploadUrlError> = serde_json::from_str(&content).ok();
147        Err(Error::ResponseError(ResponseContent {
148            status,
149            content,
150            entity,
151        }))
152    }
153}
154
155/// Patch a media record
156#[bon::builder]
157pub async fn media_patch(
158    configuration: &configuration::Configuration,
159    media_id: &str,
160    patch_media_body: models::PatchMediaBody,
161) -> Result<(), Error<MediaPatchError>> {
162    // add a prefix to parameters to efficiently prevent name collisions
163    let p_path_media_id = media_id;
164    let p_body_patch_media_body = patch_media_body;
165
166    let uri_str = format!(
167        "{}/api/public/media/{mediaId}",
168        configuration.base_path,
169        mediaId = crate::apis::urlencode(p_path_media_id)
170    );
171    let mut req_builder = configuration
172        .client
173        .request(reqwest::Method::PATCH, &uri_str);
174
175    if let Some(ref user_agent) = configuration.user_agent {
176        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
177    }
178    if let Some(ref auth_conf) = configuration.basic_auth {
179        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
180    };
181    req_builder = req_builder.json(&p_body_patch_media_body);
182
183    let req = req_builder.build()?;
184    let resp = configuration.client.execute(req).await?;
185
186    let status = resp.status();
187
188    if !status.is_client_error() && !status.is_server_error() {
189        Ok(())
190    } else {
191        let content = resp.text().await?;
192        let entity: Option<MediaPatchError> = serde_json::from_str(&content).ok();
193        Err(Error::ResponseError(ResponseContent {
194            status,
195            content,
196            entity,
197        }))
198    }
199}