1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Avatars service for Appwrite SDK
use crate::client::Client;
use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;
/// The Avatars service aims to help you complete everyday tasks related to
/// your app image, icons, and avatars.
#[derive(Debug, Clone)]
pub struct Avatars {
client: Client,
}
impl Avatars {
pub fn new(client: &Client) -> Self {
Self { client: client.clone() }
}
pub fn client(&self) -> &Client {
&self.client
}
/// You can use this endpoint to show different browser icons to your users.
/// The code argument receives the browser code as it appears in your user [GET
/// /account/sessions](https://appwrite.io/docs/references/cloud/client-web/account#getSessions)
/// endpoint. Use width, height and quality arguments to change the output
/// settings.
///
/// When one dimension is specified and the other is 0, the image is scaled
/// with preserved aspect ratio. If both dimensions are 0, the API provides an
/// image at source quality. If dimensions are not specified, the default size
/// of image returned is 100x100px.
pub async fn get_browser(
&self,
code: crate::enums::Browser,
width: Option<i64>,
height: Option<i64>,
quality: Option<i64>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
if let Some(value) = width {
params.insert("width".to_string(), json!(value));
}
if let Some(value) = height {
params.insert("height".to_string(), json!(value));
}
if let Some(value) = quality {
params.insert("quality".to_string(), json!(value));
}
let path = "/avatars/browsers/{code}".to_string().replace("{code}", &code.to_string());
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// The credit card endpoint will return you the icon of the credit card
/// provider you need. Use width, height and quality arguments to change the
/// output settings.
///
/// When one dimension is specified and the other is 0, the image is scaled
/// with preserved aspect ratio. If both dimensions are 0, the API provides an
/// image at source quality. If dimensions are not specified, the default size
/// of image returned is 100x100px.
pub async fn get_credit_card(
&self,
code: crate::enums::CreditCard,
width: Option<i64>,
height: Option<i64>,
quality: Option<i64>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
if let Some(value) = width {
params.insert("width".to_string(), json!(value));
}
if let Some(value) = height {
params.insert("height".to_string(), json!(value));
}
if let Some(value) = quality {
params.insert("quality".to_string(), json!(value));
}
let path = "/avatars/credit-cards/{code}".to_string().replace("{code}", &code.to_string());
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// Use this endpoint to fetch the favorite icon (AKA favicon) of any remote
/// website URL.
///
/// This endpoint does not follow HTTP redirects.
pub async fn get_favicon(
&self,
url: impl Into<String>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
params.insert("url".to_string(), json!(url.into()));
let path = "/avatars/favicon".to_string();
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// You can use this endpoint to show different country flags icons to your
/// users. The code argument receives the 2 letter country code. Use width,
/// height and quality arguments to change the output settings. Country codes
/// follow the [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) standard.
///
/// When one dimension is specified and the other is 0, the image is scaled
/// with preserved aspect ratio. If both dimensions are 0, the API provides an
/// image at source quality. If dimensions are not specified, the default size
/// of image returned is 100x100px.
pub async fn get_flag(
&self,
code: crate::enums::Flag,
width: Option<i64>,
height: Option<i64>,
quality: Option<i64>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
if let Some(value) = width {
params.insert("width".to_string(), json!(value));
}
if let Some(value) = height {
params.insert("height".to_string(), json!(value));
}
if let Some(value) = quality {
params.insert("quality".to_string(), json!(value));
}
let path = "/avatars/flags/{code}".to_string().replace("{code}", &code.to_string());
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// Use this endpoint to fetch a remote image URL and crop it to any image size
/// you want. This endpoint is very useful if you need to crop and display
/// remote images in your app or in case you want to make sure a 3rd party
/// image is properly served using a TLS protocol.
///
/// When one dimension is specified and the other is 0, the image is scaled
/// with preserved aspect ratio. If both dimensions are 0, the API provides an
/// image at source quality. If dimensions are not specified, the default size
/// of image returned is 400x400px.
///
/// This endpoint does not follow HTTP redirects.
pub async fn get_image(
&self,
url: impl Into<String>,
width: Option<i64>,
height: Option<i64>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
params.insert("url".to_string(), json!(url.into()));
if let Some(value) = width {
params.insert("width".to_string(), json!(value));
}
if let Some(value) = height {
params.insert("height".to_string(), json!(value));
}
let path = "/avatars/image".to_string();
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// Use this endpoint to show your user initials avatar icon on your website or
/// app. By default, this route will try to print your logged-in user name or
/// email initials. You can also overwrite the user name if you pass the 'name'
/// parameter. If no name is given and no user is logged, an empty avatar will
/// be returned.
///
/// You can use the color and background params to change the avatar colors. By
/// default, a random theme will be selected. The random theme will persist for
/// the user's initials when reloading the same theme will always return for
/// the same initials.
///
/// When one dimension is specified and the other is 0, the image is scaled
/// with preserved aspect ratio. If both dimensions are 0, the API provides an
/// image at source quality. If dimensions are not specified, the default size
/// of image returned is 100x100px.
pub async fn get_initials(
&self,
name: Option<&str>,
width: Option<i64>,
height: Option<i64>,
background: Option<&str>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
if let Some(value) = name {
params.insert("name".to_string(), json!(value));
}
if let Some(value) = width {
params.insert("width".to_string(), json!(value));
}
if let Some(value) = height {
params.insert("height".to_string(), json!(value));
}
if let Some(value) = background {
params.insert("background".to_string(), json!(value));
}
let path = "/avatars/initials".to_string();
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// Converts a given plain text to a QR code image. You can use the query
/// parameters to change the size and style of the resulting image.
pub async fn get_qr(
&self,
text: impl Into<String>,
size: Option<i64>,
margin: Option<i64>,
download: Option<bool>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
params.insert("text".to_string(), json!(text.into()));
if let Some(value) = size {
params.insert("size".to_string(), json!(value));
}
if let Some(value) = margin {
params.insert("margin".to_string(), json!(value));
}
if let Some(value) = download {
params.insert("download".to_string(), json!(value));
}
let path = "/avatars/qr".to_string();
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
/// Use this endpoint to capture a screenshot of any website URL. This endpoint
/// uses a headless browser to render the webpage and capture it as an image.
///
/// You can configure the browser viewport size, theme, user agent,
/// geolocation, permissions, and more. Capture either just the viewport or the
/// full page scroll.
///
/// When width and height are specified, the image is resized accordingly. If
/// both dimensions are 0, the API provides an image at original size. If
/// dimensions are not specified, the default viewport size is 1280x720px.
#[allow(clippy::too_many_arguments)]
pub async fn get_screenshot(
&self,
url: impl Into<String>,
headers: Option<serde_json::Value>,
viewport_width: Option<i64>,
viewport_height: Option<i64>,
scale: Option<f64>,
theme: Option<crate::enums::Theme>,
user_agent: Option<&str>,
fullpage: Option<bool>,
locale: Option<&str>,
timezone: Option<crate::enums::Timezone>,
latitude: Option<f64>,
longitude: Option<f64>,
accuracy: Option<f64>,
touch: Option<bool>,
permissions: Option<Vec<crate::enums::BrowserPermission>>,
sleep: Option<i64>,
width: Option<i64>,
height: Option<i64>,
quality: Option<i64>,
output: Option<crate::enums::ImageFormat>,
) -> crate::error::Result<Vec<u8>> {
let mut params = HashMap::new();
params.insert("url".to_string(), json!(url.into()));
if let Some(value) = headers {
params.insert("headers".to_string(), json!(value));
}
if let Some(value) = viewport_width {
params.insert("viewportWidth".to_string(), json!(value));
}
if let Some(value) = viewport_height {
params.insert("viewportHeight".to_string(), json!(value));
}
if let Some(value) = scale {
params.insert("scale".to_string(), json!(value));
}
if let Some(value) = theme {
params.insert("theme".to_string(), json!(value));
}
if let Some(value) = user_agent {
params.insert("userAgent".to_string(), json!(value));
}
if let Some(value) = fullpage {
params.insert("fullpage".to_string(), json!(value));
}
if let Some(value) = locale {
params.insert("locale".to_string(), json!(value));
}
if let Some(value) = timezone {
params.insert("timezone".to_string(), json!(value));
}
if let Some(value) = latitude {
params.insert("latitude".to_string(), json!(value));
}
if let Some(value) = longitude {
params.insert("longitude".to_string(), json!(value));
}
if let Some(value) = accuracy {
params.insert("accuracy".to_string(), json!(value));
}
if let Some(value) = touch {
params.insert("touch".to_string(), json!(value));
}
if let Some(value) = permissions {
params.insert("permissions".to_string(), json!(value));
}
if let Some(value) = sleep {
params.insert("sleep".to_string(), json!(value));
}
if let Some(value) = width {
params.insert("width".to_string(), json!(value));
}
if let Some(value) = height {
params.insert("height".to_string(), json!(value));
}
if let Some(value) = quality {
params.insert("quality".to_string(), json!(value));
}
if let Some(value) = output {
params.insert("output".to_string(), json!(value));
}
let path = "/avatars/screenshots".to_string();
self.client.call_bytes(Method::GET, &path, None, Some(params)).await
}
}
impl crate::services::Service for Avatars {
fn client(&self) -> &Client {
&self.client
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_avatars_creation() {
let client = Client::new();
let service = Avatars::new(&client);
assert!(service.client().endpoint().contains("cloud.appwrite.io/v1"));
}
}