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 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 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 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 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 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 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 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}