digitalocean_rs/builder/
list_images_builder.rs

1use crate::{
2    data::image::DigitalOceanImagesRoot, DigitalOceanApi, DigitalOceanError, DigitalOceanImage,
3};
4use serde::Serialize;
5
6#[derive(Serialize, Debug)]
7struct ListImagesConfig {
8    #[serde(skip_serializing_if = "Option::is_none")]
9    #[serde(rename = "type")]
10    image_type: Option<ImageType>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    private: Option<bool>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    tag_name: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    per_page: Option<u32>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    page: Option<u32>,
19}
20
21/// Builder struct for creating droplets.
22///
23/// A detailed documentation can be found at <https://docs.digitalocean.com/reference/api/api-reference/#operation/images_list>
24pub struct ListImagesBuilder {
25    api: DigitalOceanApi,
26    config: ListImagesConfig,
27}
28
29#[derive(Serialize, Debug)]
30#[serde(rename_all = "lowercase")]
31pub enum ImageType {
32    Application,
33    Distribution,
34}
35
36impl ListImagesBuilder {
37    pub fn new(api: DigitalOceanApi) -> Self {
38        let instancebuilder = ListImagesBuilder {
39            api,
40            config: ListImagesConfig {
41                image_type: None,
42                private: None,
43                tag_name: None,
44                per_page: None,
45                page: None,
46            },
47        };
48        instancebuilder
49    }
50
51    /// Filters results based on image type which can be either application or distribution.
52    pub fn image_type(mut self, image_type: ImageType) -> Self {
53        self.config.image_type = Some(image_type);
54        self
55    }
56
57    /// Used to filter only user images.
58    pub fn private(mut self, private: bool) -> Self {
59        self.config.private = Some(private);
60        self
61    }
62
63    /// Used to filter images by a specific tag.
64    ///
65    /// Example: tag_name=base-image
66    pub fn tag_name(mut self, tag_name: &str) -> Self {
67        self.config.tag_name = Some(tag_name.to_string());
68        self
69    }
70
71    /// Number of items returned per page
72    ///
73    /// Default: 20
74    /// Range: [1 .. 200]
75    pub fn per_page(mut self, per_page: u32) -> Self {
76        self.config.per_page = Some(per_page);
77        self
78    }
79
80    /// Which 'page' of paginated results to return.
81    pub fn page(mut self, page: u32) -> Self {
82        self.config.page = Some(page);
83        self
84    }
85
86    #[cfg(feature = "blocking")]
87    pub fn run(self) -> Result<Vec<DigitalOceanImage>, DigitalOceanError> {
88        let images = self
89            .api
90            .get("https://api.digitalocean.com/v2/images", self.config)?
91            .json::<DigitalOceanImagesRoot>()?
92            .images;
93
94        Ok(images)
95    }
96
97    pub async fn run_async(self) -> Result<Vec<DigitalOceanImage>, DigitalOceanError> {
98        let images = self
99            .api
100            .get_async("https://api.digitalocean.com/v2/images", self.config)
101            .await?
102            .json::<DigitalOceanImagesRoot>()
103            .await?
104            .images;
105
106        Ok(images)
107    }
108}