aw_test/services/
avatars.rs

1use crate::client::{Client, ParamType};
2use std::collections::HashMap;
3use crate::services::AppwriteException;
4use crate::models;
5use serde_json::json;
6use std::io::Read;
7
8#[derive(Clone)]
9pub struct Avatars {
10  client: Client
11}
12
13impl Avatars {  
14    pub fn new(client: &Client) -> Self {
15        Self {
16            client: client.clone()
17        }
18    }
19
20    /// You can use this endpoint to show different browser icons to your users.
21    /// The code argument receives the browser code as it appears in your user
22    /// /account/sessions endpoint. Use width, height and quality arguments to
23    /// change the output settings.
24    pub fn get_browser(&self, code: &str, width: Option<i64>, height: Option<i64>, quality: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
25        let path = "/avatars/browsers/code".replace("code", &code);
26        let  headers: HashMap<String, String> = [
27            ("content-type".to_string(), "application/json".to_string()),
28        ].iter().cloned().collect();
29
30        let  params: HashMap<String, ParamType> = [
31            ("width".to_string(),  ParamType::OptionalNumber(width)),
32            ("height".to_string(),  ParamType::OptionalNumber(height)),
33            ("quality".to_string(),  ParamType::OptionalNumber(quality)),
34        ].iter().cloned().collect();
35
36        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
37
38        let processedResponse:Vec<u8> = match response {
39            Ok(mut r) => {
40                let mut buf: Vec<u8> = vec![];
41                match r.copy_to(&mut buf) {
42                    Ok(_) => (),
43                    Err(e) => {
44                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
45                    }
46                };
47                buf
48            }
49            Err(e) => {
50                return Err(e);
51            }
52        };
53
54        Ok(processedResponse)
55    }
56
57    /// The credit card endpoint will return you the icon of the credit card
58    /// provider you need. Use width, height and quality arguments to change the
59    /// output settings.
60    pub fn get_credit_card(&self, code: &str, width: Option<i64>, height: Option<i64>, quality: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
61        let path = "/avatars/credit-cards/code".replace("code", &code);
62        let  headers: HashMap<String, String> = [
63            ("content-type".to_string(), "application/json".to_string()),
64        ].iter().cloned().collect();
65
66        let  params: HashMap<String, ParamType> = [
67            ("width".to_string(),  ParamType::OptionalNumber(width)),
68            ("height".to_string(),  ParamType::OptionalNumber(height)),
69            ("quality".to_string(),  ParamType::OptionalNumber(quality)),
70        ].iter().cloned().collect();
71
72        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
73
74        let processedResponse:Vec<u8> = match response {
75            Ok(mut r) => {
76                let mut buf: Vec<u8> = vec![];
77                match r.copy_to(&mut buf) {
78                    Ok(_) => (),
79                    Err(e) => {
80                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
81                    }
82                };
83                buf
84            }
85            Err(e) => {
86                return Err(e);
87            }
88        };
89
90        Ok(processedResponse)
91    }
92
93    /// Use this endpoint to fetch the favorite icon (AKA favicon) of any remote
94    /// website URL.
95    /// 
96    pub fn get_favicon(&self, url: &str) -> Result<Vec<u8>, AppwriteException> {
97        let path = "/avatars/favicon";
98        let  headers: HashMap<String, String> = [
99            ("content-type".to_string(), "application/json".to_string()),
100        ].iter().cloned().collect();
101
102        let  params: HashMap<String, ParamType> = [
103            ("url".to_string(), ParamType::String(url.to_string())),
104        ].iter().cloned().collect();
105
106        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
107
108        let processedResponse:Vec<u8> = match response {
109            Ok(mut r) => {
110                let mut buf: Vec<u8> = vec![];
111                match r.copy_to(&mut buf) {
112                    Ok(_) => (),
113                    Err(e) => {
114                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
115                    }
116                };
117                buf
118            }
119            Err(e) => {
120                return Err(e);
121            }
122        };
123
124        Ok(processedResponse)
125    }
126
127    /// You can use this endpoint to show different country flags icons to your
128    /// users. The code argument receives the 2 letter country code. Use width,
129    /// height and quality arguments to change the output settings.
130    pub fn get_flag(&self, code: &str, width: Option<i64>, height: Option<i64>, quality: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
131        let path = "/avatars/flags/code".replace("code", &code);
132        let  headers: HashMap<String, String> = [
133            ("content-type".to_string(), "application/json".to_string()),
134        ].iter().cloned().collect();
135
136        let  params: HashMap<String, ParamType> = [
137            ("width".to_string(),  ParamType::OptionalNumber(width)),
138            ("height".to_string(),  ParamType::OptionalNumber(height)),
139            ("quality".to_string(),  ParamType::OptionalNumber(quality)),
140        ].iter().cloned().collect();
141
142        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
143
144        let processedResponse:Vec<u8> = match response {
145            Ok(mut r) => {
146                let mut buf: Vec<u8> = vec![];
147                match r.copy_to(&mut buf) {
148                    Ok(_) => (),
149                    Err(e) => {
150                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
151                    }
152                };
153                buf
154            }
155            Err(e) => {
156                return Err(e);
157            }
158        };
159
160        Ok(processedResponse)
161    }
162
163    /// Use this endpoint to fetch a remote image URL and crop it to any image size
164    /// you want. This endpoint is very useful if you need to crop and display
165    /// remote images in your app or in case you want to make sure a 3rd party
166    /// image is properly served using a TLS protocol.
167    pub fn get_image(&self, url: &str, width: Option<i64>, height: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
168        let path = "/avatars/image";
169        let  headers: HashMap<String, String> = [
170            ("content-type".to_string(), "application/json".to_string()),
171        ].iter().cloned().collect();
172
173        let  params: HashMap<String, ParamType> = [
174            ("url".to_string(), ParamType::String(url.to_string())),
175            ("width".to_string(),  ParamType::OptionalNumber(width)),
176            ("height".to_string(),  ParamType::OptionalNumber(height)),
177        ].iter().cloned().collect();
178
179        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
180
181        let processedResponse:Vec<u8> = match response {
182            Ok(mut r) => {
183                let mut buf: Vec<u8> = vec![];
184                match r.copy_to(&mut buf) {
185                    Ok(_) => (),
186                    Err(e) => {
187                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
188                    }
189                };
190                buf
191            }
192            Err(e) => {
193                return Err(e);
194            }
195        };
196
197        Ok(processedResponse)
198    }
199
200    /// Use this endpoint to show your user initials avatar icon on your website or
201    /// app. By default, this route will try to print your logged-in user name or
202    /// email initials. You can also overwrite the user name if you pass the 'name'
203    /// parameter. If no name is given and no user is logged, an empty avatar will
204    /// be returned.
205    /// 
206    /// You can use the color and background params to change the avatar colors. By
207    /// default, a random theme will be selected. The random theme will persist for
208    /// the user's initials when reloading the same theme will always return for
209    /// the same initials.
210    pub fn get_initials(&self, name: Option<&str>, width: Option<i64>, height: Option<i64>, color: Option<&str>, background: Option<&str>) -> Result<Vec<u8>, AppwriteException> {
211        let path = "/avatars/initials";
212        let  headers: HashMap<String, String> = [
213            ("content-type".to_string(), "application/json".to_string()),
214        ].iter().cloned().collect();
215
216        let name:&str = match name {
217            Some(data) => data,
218            None => ""
219        };
220
221        let color:&str = match color {
222            Some(data) => data,
223            None => ""
224        };
225
226        let background:&str = match background {
227            Some(data) => data,
228            None => ""
229        };
230
231        let  params: HashMap<String, ParamType> = [
232            ("name".to_string(), ParamType::String(name.to_string())),
233            ("width".to_string(),  ParamType::OptionalNumber(width)),
234            ("height".to_string(),  ParamType::OptionalNumber(height)),
235            ("color".to_string(), ParamType::String(color.to_string())),
236            ("background".to_string(), ParamType::String(background.to_string())),
237        ].iter().cloned().collect();
238
239        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
240
241        let processedResponse:Vec<u8> = match response {
242            Ok(mut r) => {
243                let mut buf: Vec<u8> = vec![];
244                match r.copy_to(&mut buf) {
245                    Ok(_) => (),
246                    Err(e) => {
247                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
248                    }
249                };
250                buf
251            }
252            Err(e) => {
253                return Err(e);
254            }
255        };
256
257        Ok(processedResponse)
258    }
259
260    /// Converts a given plain text to a QR code image. You can use the query
261    /// parameters to change the size and style of the resulting image.
262    pub fn get_qr(&self, text: &str, size: Option<i64>, margin: Option<i64>, download: Option<bool>) -> Result<Vec<u8>, AppwriteException> {
263        let path = "/avatars/qr";
264        let  headers: HashMap<String, String> = [
265            ("content-type".to_string(), "application/json".to_string()),
266        ].iter().cloned().collect();
267
268        let  params: HashMap<String, ParamType> = [
269            ("text".to_string(), ParamType::String(text.to_string())),
270            ("size".to_string(),  ParamType::OptionalNumber(size)),
271            ("margin".to_string(),  ParamType::OptionalNumber(margin)),
272            ("download".to_string(), ParamType::OptionalBool(download)),
273        ].iter().cloned().collect();
274
275        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
276
277        let processedResponse:Vec<u8> = match response {
278            Ok(mut r) => {
279                let mut buf: Vec<u8> = vec![];
280                match r.copy_to(&mut buf) {
281                    Ok(_) => (),
282                    Err(e) => {
283                        return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
284                    }
285                };
286                buf
287            }
288            Err(e) => {
289                return Err(e);
290            }
291        };
292
293        Ok(processedResponse)
294    }
295}