coder/builders/images/
get.rs

1imports!();
2
3new_builder!(
4    /// `/api/images/:id`
5    GlobalImage,
6    /// `/api/images`
7    GlobalImages,
8    /// `/api/orgs/:id/images`
9    OrgImages,
10);
11
12use crate::builders::orgs::get::OrgBuilder;
13
14exec!(
15    GlobalImage  -> crate::models::Image,
16    OrgImages -> Vec<crate::models::Image>,
17);
18
19from!(
20    @Org
21        -> OrgImages,
22    @GlobalImages
23        -> GlobalImage,
24);
25
26impl_client!(
27    /// Begins a global image query.
28    -> images ["images"] -> GlobalImages,
29);
30
31impl_builder!(
32    @Org
33        /// Queries all images in an organization.
34        -> images ["images"] -> OrgImages,
35    @GlobalImages
36        /// Queries an image in by its id.
37        => get [] -> GlobalImage = id,
38        /// Option to return the `environments` field on the `Image` struct.
39        ?> with_envs ["envs"] -> v: bool,
40        /// Option to return the `user_ids` field on the `Image` struct.
41        ?> with_user_ids ["user_ids"] -> v: bool,
42    @GlobalImage
43        /// Option to return the `environments` field on the `Image` struct.
44        ?> with_envs ["envs"] -> v: bool,
45        /// Option to return the `user_ids` field on the `Image` struct.
46        ?> with_user_ids ["user_ids"] -> v: bool,
47    @OrgImages
48        /// Option to return the `environments` field on the `Image` struct.
49        ?> with_envs ["envs"] -> v: bool,
50        /// Option to return the `user_ids` field on the `Image` struct.
51        ?> with_user_ids ["user_ids"] -> v: bool,
52);
53
54#[cfg(test)]
55mod test {
56    use crate::client::test::{client, ids::*};
57    use crate::client::Executor;
58
59    #[tokio::test]
60    async fn test_image() {
61        let c = client();
62
63        let res = c
64            .images()
65            .get(IMAGE_ID)
66            .execute()
67            .await
68            .expect("send request")
69            .response
70            .expect("api error returned");
71
72        // id should at least not be empty
73        assert!(!res.id.is_empty(), "id should be a non-empty string");
74    }
75
76    #[tokio::test]
77    async fn test_image_with_environments() {
78        let c = client();
79
80        let res = c
81            .images()
82            .get(IMAGE_ID)
83            .with_envs(true)
84            .execute()
85            .await
86            .expect("send request")
87            .response
88            .expect("api error returned");
89
90        // id should at least not be empty
91        assert!(!res.id.is_empty(), "id should be a non-empty string");
92        assert!(res.environments.is_some(), "envs should be returned");
93
94        let res = c
95            .images()
96            .get(IMAGE_ID)
97            .with_envs(false)
98            .execute()
99            .await
100            .expect("send request")
101            .response
102            .expect("api error returned");
103
104        // id should at least not be empty
105        assert!(!res.id.is_empty(), "id should be a non-empty string");
106        assert!(res.environments.is_none(), "envs should not be returned");
107    }
108
109    mod org {
110        use super::*;
111
112        #[tokio::test]
113        async fn test_org_images() {
114            let c = client();
115
116            let res = c
117                .orgs()
118                .get(ORG_ID)
119                .images()
120                .execute()
121                .await
122                .expect("send request")
123                .response
124                .expect("api error returned");
125
126            // we should get at least 1
127            assert_ne!(res.len(), 0);
128
129            // they should all have non-empty ids
130            let ok = res.iter().fold(false, |ok, img| ok || img.id != "".into());
131            assert_eq!(ok, true);
132        }
133    }
134}