gyazo_api/
get.rs

1use super::access_policy::AccessPolicy;
2use super::Gyazo;
3
4#[derive(serde::Deserialize, Debug)]
5/// Information about a Gyazo image.
6pub struct ImageInfo {
7    /// Unique identifier of the image.
8    pub image_id: String,
9    /// URL to the image permalink.
10    pub permalink_url: Option<String>,
11    /// Direct URL to the image.
12    pub url: String,
13    /// Access policy of the image.
14    pub access_policy: Option<AccessPolicy>,
15    /// Metadata associated with the image.
16    pub metadata: Metadata,
17    /// Type of the image.
18    pub r#type: String,
19    /// Creation timestamp of the image.
20    pub created_at: String,
21}
22
23#[derive(serde::Deserialize, Debug)]
24/// Metadata for a Gyazo image.
25pub struct Metadata {
26    /// Name of the application used to capture the image.
27    pub app: Option<String>,
28    /// Title of the captured website.
29    pub title: Option<String>,
30    /// URL of the captured website.
31    pub url: Option<String>,
32    /// Description of the image.
33    pub desc: Option<String>,
34    /// Original title of the image.
35    pub original_title: Option<String>,
36    /// Original URL of the image.
37    pub original_url: Option<String>,
38    /// OCR information of the image.
39    pub ocr: Option<Ocr>,
40}
41
42#[derive(serde::Deserialize, Debug)]
43/// OCR information for a Gyazo image.
44pub struct Ocr {
45    /// The locale used for OCR text recognition (e.g., "en" for English).
46    pub locale: String,
47    /// Description extracted by OCR.
48    pub description: String,
49}
50
51impl Gyazo {
52    /// Retrieves a list of images from Gyazo.
53    ///
54    /// # Arguments
55    ///
56    /// * `page` - The page number to retrieve.
57    /// * `per_page` - Number of images per page.
58    ///
59    /// # Returns
60    ///
61    /// A `Result` containing a vector of `ImageInfo` on success or a `reqwest::Error` on failure.
62    // TODO: test
63    pub async fn list(&self, page: u32, per_page: u32) -> Result<Vec<ImageInfo>, reqwest::Error> {
64        let res = self
65            .client
66            .get("https://api.gyazo.com/api/images")
67            .query(&[
68                ("access_token", &self.access_token),
69                ("page", &page.to_string()),
70                ("per_page", &per_page.to_string()),
71            ])
72            .send()
73            .await?
74            .error_for_status()?
75            .json::<Vec<ImageInfo>>()
76            .await?;
77
78        Ok(res)
79    }
80
81    /// Retrieves information about a specific image.
82    ///
83    /// # Arguments
84    ///
85    /// * `image_id` - The ID of the image to retrieve.
86    ///
87    /// # Returns
88    ///
89    /// A `Result` containing `ImageInfo` on success or a `reqwest::Error` on failure.
90    // TODO: test
91    pub async fn image(&self, image_id: &str) -> Result<ImageInfo, reqwest::Error> {
92        let url = format!("https://api.gyazo.com/api/images/{}", image_id);
93        let res = self
94            .client
95            .get(url)
96            .query(&[("access_token", &self.access_token)])
97            .send()
98            .await?
99            .error_for_status()?
100            .json::<ImageInfo>()
101            .await?;
102
103        Ok(res)
104    }
105}