d_id/endpoints/resources/
images.rs1use super::*;
5
6const IMAGES_PATH: &str = "/images";
7
8#[derive(Serialize, Deserialize, Debug)]
10pub struct ImageResponse {
11 pub id: String,
12 pub url: String,
13}
14
15pub async fn upload_image_by_file(path: &str) -> Result<ImageResponse> {
20 let mime_subtype = path.split(".").last().ok_or("Invalid file path")?;
21 let mut form = MultipartFormData::new();
22 form.add_file(&format!("image/{}", mime_subtype), "image", path)?;
23 form.end_body()?;
24
25 let c = ClientBuilder::new()?
26 .method(POST)?
27 .path(IMAGES_PATH)?
28 .header(ACCEPT, APPLICATION_JSON)?
29 .header(CONTENT_TYPE, &format!("{}{}", MULTIPART_FORM_DATA_BOUNDARY, form.boundary))?
30 .build()?;
31
32 let resp = c.send_request(Full::<Bytes>::new(form.body.into())).await?;
33
34 let json = serde_json::from_slice::<ImageResponse>(&resp.as_ref())?;
35
36 Ok(json)
37}
38
39pub async fn delete_image(id: &str) -> Result<()> {
40 let c = ClientBuilder::new()?
41 .method(DELETE)?
42 .path(&format!("{}/{}", IMAGES_PATH, id))?
43 .header(CONTENT_TYPE, APPLICATION_JSON)?
44 .build()?;
45
46 let _resp = c.send_request(Empty::<Bytes>::new()).await?;
47
48 Ok(())
49}
50