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 /// OCR information of the image.
16 pub ocr: Ocr,
17 /// Metadata associated with the image.
18 pub metadata: Metadata,
19 /// Type of the image.
20 pub r#type: String,
21 /// Creation timestamp of the image.
22 pub created_at: String,
23}
24
25#[derive(serde::Deserialize, Debug)]
26/// Metadata for a Gyazo image.
27pub struct Metadata {
28 /// Name of the application used to capture the image.
29 pub app: Option<String>,
30 /// Title of the captured website.
31 pub title: Option<String>,
32 /// URL of the captured website.
33 pub url: Option<String>,
34 /// Description of the image.
35 pub desc: Option<String>,
36 /// Original title of the image.
37 pub original_title: Option<String>,
38 /// Original URL of the image.
39 pub original_url: Option<String>,
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}