1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::{
error::OpenAIError,
types::{
CreateImageEditRequest, CreateImageRequest, CreateImageVariationRequest, ImageResponse,
},
util::create_file_part,
Client,
};
pub struct Images<'c> {
client: &'c Client,
}
impl<'c> Images<'c> {
pub fn new(client: &'c Client) -> Self {
Self { client }
}
pub async fn create(&self, request: CreateImageRequest) -> Result<ImageResponse, OpenAIError> {
self.client.post("/images/generations", request).await
}
pub async fn create_edit(
&self,
request: CreateImageEditRequest,
) -> Result<ImageResponse, OpenAIError> {
let image_part = create_file_part(&request.image.path).await?;
let mut form = reqwest::multipart::Form::new()
.part("image", image_part)
.text("prompt", request.prompt);
if let Some(mask) = request.mask {
let mask_part = create_file_part(&mask.path).await?;
form = form.part("mask", mask_part);
}
if request.n.is_some() {
form = form.text("n", request.n.unwrap().to_string())
}
if request.size.is_some() {
form = form.text("size", request.size.unwrap().to_string())
}
if request.response_format.is_some() {
form = form.text(
"response_format",
request.response_format.unwrap().to_string(),
)
}
if request.user.is_some() {
form = form.text("user", request.user.unwrap())
}
self.client.post_form("/images/edits", form).await
}
pub async fn create_variation(
&self,
request: CreateImageVariationRequest,
) -> Result<ImageResponse, OpenAIError> {
let image_part = create_file_part(&request.image.path).await?;
let mut form = reqwest::multipart::Form::new().part("image", image_part);
if request.n.is_some() {
form = form.text("n", request.n.unwrap().to_string())
}
if request.size.is_some() {
form = form.text("size", request.size.unwrap().to_string())
}
if request.response_format.is_some() {
form = form.text(
"response_format",
request.response_format.unwrap().to_string(),
)
}
if request.user.is_some() {
form = form.text("user", request.user.unwrap())
}
self.client.post_form("/images/variations", form).await
}
}