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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use reqwest::multipart::{Form, Part};
use reqwest::{Client, StatusCode};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Error types for the Gyazo API client
#[derive(Error, Debug)]
pub enum GyazoError {
    #[error("HTTP request failed: {0}")]
    RequestFailed(#[from] reqwest::Error),
    #[error("Failed to parse JSON: {0}")]
    JsonParseError(#[from] serde_json::Error),
    #[error("API error: {status}, message: {message}")]
    ApiError { status: StatusCode, message: String },
    #[error("Unexpected error: {0}")]
    Other(String),
    #[error("Invalid input: {0}")]
    InvalidInput(String),
    #[error("Invalid url: {0}")]
    InvalidUrl(String),
}

/// Gyazo API client
pub struct GyazoClient {
    client: Client,
    access_token: String,
}

impl GyazoClient {
    /// Create a new GyazoClient instance
    pub fn new(access_token: String) -> Self {
        GyazoClient {
            client: Client::new(),
            access_token,
        }
    }

    async fn request<T: for<'de> Deserialize<'de>>(
        &self,
        url: &str,
        method: reqwest::Method,
        form: Option<Form>,
    ) -> Result<T, GyazoError> {
        let mut request = self
            .client
            .request(method, url)
            .bearer_auth(&self.access_token);

        if let Some(form) = form {
            request = request.multipart(form);
        }

        let response = request.send().await?;

        match response.status() {
            StatusCode::OK | StatusCode::CREATED | StatusCode::NO_CONTENT => {
                Ok(response.json().await?)
            }
            status => {
                let message = response
                    .text()
                    .await
                    .unwrap_or_else(|_| "Unknown error".to_string());
                Err(GyazoError::ApiError { status, message })
            }
        }
    }

    /// Get an image by its ID
    pub async fn get_image(&self, image_id: &str) -> Result<GyazoImageResponse, GyazoError> {
        let url = format!("https://api.gyazo.com/api/images/{}", image_id);
        self.request(&url, reqwest::Method::GET, None).await
    }

    /// Get a list of images
    pub async fn list_images(&self) -> Result<Vec<GyazoImageResponse>, GyazoError> {
        let url = "https://api.gyazo.com/api/images".to_string();
        self.request(&url, reqwest::Method::GET, None).await
    }

    /// Upload an image
    pub async fn upload_image(
        &self,
        param: UploadParams,
    ) -> Result<UploadImageResponse, GyazoError> {
        let url = "https://upload.gyazo.com/api/upload".to_string();
        let mut form = Form::new().part(
            "imagedata",
            Part::bytes(param.imagedata.clone()).file_name("image.png"),
        );

        for (key, value) in param.into_form_params() {
            form = form.text(key, value);
        }

        self.request(&url, reqwest::Method::POST, Some(form)).await
    }

    /// Delete an image by its ID
    pub async fn delete_image(&self, image_id: &str) -> Result<DeleteImageResponse, GyazoError> {
        let url = format!("https://api.gyazo.com/api/images/{}", image_id);
        self.request(&url, reqwest::Method::DELETE, None).await
    }

    /// OnEnbed an image
    pub async fn onembed_image(&self, url: &str) -> Result<OembedResponse, GyazoError> {
        if !url.starts_with("https://gyazo.com/") {
            return Err(GyazoError::InvalidUrl(
                "URL must start with 'https://gyazo.com/'".to_string(),
            ));
        }
        let url = format!("https://api.gyazo.com/api/oembed?url={}", url);
        self.request(&url, reqwest::Method::GET, None).await
    }
}

/// Image response from Gyazo API
#[derive(Debug, Deserialize)]
pub struct GyazoImageResponse {
    pub image_id: String,
    pub permalink_url: Option<String>,
    pub thumb_url: Option<String>,
    #[serde(rename = "type")]
    pub image_type: String,
    pub created_at: String,
    pub metadata: ImageMetadata,
    pub ocr: Option<ImageOcr>,
}

#[derive(Debug, Deserialize)]
pub struct ImageMetadata {
    pub app: Option<String>,
    pub title: Option<String>,
    pub url: Option<String>,
    pub desc: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ImageOcr {
    pub locale: String,
    pub description: String,
}

/// Response after uploading an image
#[derive(Debug, Deserialize)]
pub struct UploadImageResponse {
    pub image_id: String,
    pub permalink_url: String,
    pub thumb_url: String,
    pub url: String,
    #[serde(rename = "type")]
    pub image_type: String,
}

/// Response after deleting an image
#[derive(Debug, Deserialize)]
pub struct DeleteImageResponse {
    pub image_id: String,
    #[serde(rename = "type")]
    pub image_type: String,
}

/// Parameters for uploading an image
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct UploadParams {
    pub imagedata: Vec<u8>,
    pub access_policy: Option<String>,
    pub metadata_is_public: Option<String>,
    pub referer_url: Option<String>,
    pub app: Option<String>,
    pub title: Option<String>,
    pub desc: Option<String>,
    pub created_at: Option<String>,
    pub collection_id: Option<String>,
}

impl UploadParams {
    fn into_form_params(self) -> Vec<(String, String)> {
        let mut params = Vec::new();
        if let Some(access_policy) = self.access_policy {
            params.push(("access_policy".to_string(), access_policy));
        }
        params.push((
            "metadata_is_public".to_string(),
            self.metadata_is_public
                .unwrap_or_else(|| "true".to_string()),
        ));
        if let Some(referer_url) = self.referer_url {
            params.push(("referer_url".to_string(), referer_url));
        }
        if let Some(app) = self.app {
            params.push(("app".to_string(), app));
        }
        if let Some(title) = self.title {
            params.push(("title".to_string(), title));
        }
        if let Some(desc) = self.desc {
            params.push(("desc".to_string(), desc));
        }
        if let Some(created_at) = self.created_at {
            params.push(("created_at".to_string(), created_at));
        }
        if let Some(collection_id) = self.collection_id {
            params.push(("collection_id".to_string(), collection_id));
        }
        params
    }
}

/// Builder for UploadParams
pub struct UploadParamsBuilder {
    imagedata: Vec<u8>,
    access_policy: Option<String>,
    metadata_is_public: Option<String>,
    referer_url: Option<String>,
    app: Option<String>,
    title: Option<String>,
    desc: Option<String>,
    created_at: Option<String>,
    collection_id: Option<String>,
}

impl UploadParamsBuilder {
    pub fn new(imagedata: Vec<u8>) -> Self {
        Self {
            imagedata,
            access_policy: None,
            metadata_is_public: None,
            referer_url: None,
            app: None,
            title: None,
            desc: None,
            created_at: None,
            collection_id: None,
        }
    }

    pub fn access_policy(mut self, access_policy: impl Into<String>) -> Result<Self, GyazoError> {
        let access_policy = access_policy.into();
        if access_policy != "anyone" && access_policy != "only_me" {
            return Err(GyazoError::InvalidInput(
                "access_policy must be 'anyone' or 'only_me'".to_string(),
            ));
        }
        self.access_policy = Some(access_policy);
        Ok(self)
    }

    pub fn metadata_is_public(
        mut self,
        metadata_is_public: impl Into<String>,
    ) -> Result<Self, GyazoError> {
        let metadata_is_public = metadata_is_public.into();
        if metadata_is_public != "true" && metadata_is_public != "false" {
            return Err(GyazoError::InvalidInput(
                "metadata_is_public must be 'true' or 'false'".to_string(),
            ));
        }
        self.metadata_is_public = Some(metadata_is_public);
        Ok(self)
    }

    pub fn referer_url(mut self, referer_url: impl Into<String>) -> Self {
        self.referer_url = Some(referer_url.into());
        self
    }

    pub fn app(mut self, app: impl Into<String>) -> Self {
        self.app = Some(app.into());
        self
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn desc(mut self, desc: impl Into<String>) -> Self {
        self.desc = Some(desc.into());
        self
    }

    pub fn created_at(mut self, created_at: impl Into<String>) -> Self {
        self.created_at = Some(created_at.into());
        self
    }

    pub fn collection_id(mut self, collection_id: impl Into<String>) -> Self {
        self.collection_id = Some(collection_id.into());
        self
    }

    pub fn build(self) -> Result<UploadParams, GyazoError> {
        Ok(UploadParams {
            imagedata: self.imagedata,
            access_policy: self.access_policy,
            metadata_is_public: self.metadata_is_public,
            referer_url: self.referer_url,
            app: self.app,
            title: self.title,
            desc: self.desc,
            created_at: self.created_at,
            collection_id: self.collection_id,
        })
    }
}

/// Oembed response from Gyazo API
#[derive(Debug, Deserialize)]
pub struct OembedResponse {
    pub version: String,
    #[serde(rename = "type")]
    pub image_type: String,
    pub provider_name: String,
    pub provider_url: String,
    pub url: String,
    pub width: u32,
    pub height: u32,
}