aw_test/services/
database.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 Database {
10  client: Client
11}
12
13impl Database {  
14    pub fn new(client: &Client) -> Self {
15        Self {
16            client: client.clone()
17        }
18    }
19
20    /// Get a list of all the user collections. You can use the query params to
21    /// filter your results. On admin mode, this endpoint will return a list of all
22    /// of the project's collections. [Learn more about different API
23    /// modes](/docs/admin).
24    pub fn list_collections(&self, search: Option<&str>, limit: Option<i64>, offset: Option<i64>, cursor: Option<&str>, cursor_direction: Option<&str>, order_type: Option<&str>) -> Result<models::CollectionList, AppwriteException> {
25        let path = "/database/collections";
26        let  headers: HashMap<String, String> = [
27            ("content-type".to_string(), "application/json".to_string()),
28        ].iter().cloned().collect();
29
30        let search:&str = match search {
31            Some(data) => data,
32            None => ""
33        };
34
35        let cursor:&str = match cursor {
36            Some(data) => data,
37            None => ""
38        };
39
40        let cursor_direction:&str = match cursor_direction {
41            Some(data) => data,
42            None => ""
43        };
44
45        let order_type:&str = match order_type {
46            Some(data) => data,
47            None => ""
48        };
49
50        let  params: HashMap<String, ParamType> = [
51            ("search".to_string(), ParamType::String(search.to_string())),
52            ("limit".to_string(),  ParamType::OptionalNumber(limit)),
53            ("offset".to_string(),  ParamType::OptionalNumber(offset)),
54            ("cursor".to_string(), ParamType::String(cursor.to_string())),
55            ("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
56            ("orderType".to_string(), ParamType::String(order_type.to_string())),
57        ].iter().cloned().collect();
58
59        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
60
61        let processedResponse:models::CollectionList = match response {
62            Ok(r) => {
63                match r.json() {
64                    Ok(json) => json,
65                    Err(e) => {
66                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
67                    }
68                }
69            }
70            Err(e) => {
71                return Err(e);
72            }
73        };
74
75        Ok(processedResponse)
76    }
77
78    /// Create a new Collection.
79    pub fn create_collection(&self, collection_id: &str, name: &str, permission: &str, read: &[&str], write: &[&str]) -> Result<models::Collection, AppwriteException> {
80        let path = "/database/collections";
81        let  headers: HashMap<String, String> = [
82            ("content-type".to_string(), "application/json".to_string()),
83        ].iter().cloned().collect();
84
85        let  params: HashMap<String, ParamType> = [
86            ("collectionId".to_string(), ParamType::String(collection_id.to_string())),
87            ("name".to_string(), ParamType::String(name.to_string())),
88            ("permission".to_string(), ParamType::String(permission.to_string())),
89            ("read".to_string(), ParamType::Array(read.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
90            ("write".to_string(), ParamType::Array(write.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
91        ].iter().cloned().collect();
92
93        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
94
95        let processedResponse:models::Collection = match response {
96            Ok(r) => {
97                match r.json() {
98                    Ok(json) => json,
99                    Err(e) => {
100                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
101                    }
102                }
103            }
104            Err(e) => {
105                return Err(e);
106            }
107        };
108
109        Ok(processedResponse)
110    }
111
112    /// Get a collection by its unique ID. This endpoint response returns a JSON
113    /// object with the collection metadata.
114    pub fn get_collection(&self, collection_id: &str) -> Result<models::Collection, AppwriteException> {
115        let path = "/database/collections/collectionId".replace("collectionId", &collection_id);
116        let  headers: HashMap<String, String> = [
117            ("content-type".to_string(), "application/json".to_string()),
118        ].iter().cloned().collect();
119
120        let  params: HashMap<String, ParamType> = [
121        ].iter().cloned().collect();
122
123        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
124
125        let processedResponse:models::Collection = match response {
126            Ok(r) => {
127                match r.json() {
128                    Ok(json) => json,
129                    Err(e) => {
130                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
131                    }
132                }
133            }
134            Err(e) => {
135                return Err(e);
136            }
137        };
138
139        Ok(processedResponse)
140    }
141
142    /// Update a collection by its unique ID.
143    pub fn update_collection(&self, collection_id: &str, name: &str, permission: &str, read: Option<&[&str]>, write: Option<&[&str]>, enabled: Option<bool>) -> Result<models::Collection, AppwriteException> {
144        let path = "/database/collections/collectionId".replace("collectionId", &collection_id);
145        let  headers: HashMap<String, String> = [
146            ("content-type".to_string(), "application/json".to_string()),
147        ].iter().cloned().collect();
148
149        let read:&[&str] = match read {
150            Some(data) => data,
151            None => &[]
152        };
153
154        let write:&[&str] = match write {
155            Some(data) => data,
156            None => &[]
157        };
158
159        let  params: HashMap<String, ParamType> = [
160            ("name".to_string(), ParamType::String(name.to_string())),
161            ("permission".to_string(), ParamType::String(permission.to_string())),
162            ("read".to_string(), ParamType::Array(read.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
163            ("write".to_string(), ParamType::Array(write.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
164            ("enabled".to_string(), ParamType::OptionalBool(enabled)),
165        ].iter().cloned().collect();
166
167        let response = self.client.clone().call("PUT", &path, Some(headers), Some(params) );
168
169        let processedResponse:models::Collection = match response {
170            Ok(r) => {
171                match r.json() {
172                    Ok(json) => json,
173                    Err(e) => {
174                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
175                    }
176                }
177            }
178            Err(e) => {
179                return Err(e);
180            }
181        };
182
183        Ok(processedResponse)
184    }
185
186    /// Delete a collection by its unique ID. Only users with write permissions
187    /// have access to delete this resource.
188    pub fn delete_collection(&self, collection_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
189        let path = "/database/collections/collectionId".replace("collectionId", &collection_id);
190        let  headers: HashMap<String, String> = [
191            ("content-type".to_string(), "application/json".to_string()),
192        ].iter().cloned().collect();
193
194        let  params: HashMap<String, ParamType> = [
195        ].iter().cloned().collect();
196
197        let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
198
199        match response {
200            Ok(r) => {
201                let status_code = r.status();
202                if status_code == reqwest::StatusCode::NO_CONTENT {
203                    Ok(json!(true))
204                } else {
205                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
206                }
207            }
208            Err(e) => {
209                Err(e)
210            }
211        }
212    }
213
214    pub fn list_attributes(&self, collection_id: &str) -> Result<models::AttributeList, AppwriteException> {
215        let path = "/database/collections/collectionId/attributes".replace("collectionId", &collection_id);
216        let  headers: HashMap<String, String> = [
217            ("content-type".to_string(), "application/json".to_string()),
218        ].iter().cloned().collect();
219
220        let  params: HashMap<String, ParamType> = [
221        ].iter().cloned().collect();
222
223        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
224
225        let processedResponse:models::AttributeList = match response {
226            Ok(r) => {
227                match r.json() {
228                    Ok(json) => json,
229                    Err(e) => {
230                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
231                    }
232                }
233            }
234            Err(e) => {
235                return Err(e);
236            }
237        };
238
239        Ok(processedResponse)
240    }
241
242    /// Create a boolean attribute.
243    /// 
244    pub fn create_boolean_attribute(&self, collection_id: &str, key: &str, required: bool, default: Option<bool>, array: Option<bool>) -> Result<models::AttributeBoolean, AppwriteException> {
245        let path = "/database/collections/collectionId/attributes/boolean".replace("collectionId", &collection_id);
246        let  headers: HashMap<String, String> = [
247            ("content-type".to_string(), "application/json".to_string()),
248        ].iter().cloned().collect();
249
250        let  params: HashMap<String, ParamType> = [
251            ("key".to_string(), ParamType::String(key.to_string())),
252            ("required".to_string(), ParamType::Bool(required)),
253            ("default".to_string(), ParamType::OptionalBool(default)),
254            ("array".to_string(), ParamType::OptionalBool(array)),
255        ].iter().cloned().collect();
256
257        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
258
259        let processedResponse:models::AttributeBoolean = match response {
260            Ok(r) => {
261                match r.json() {
262                    Ok(json) => json,
263                    Err(e) => {
264                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
265                    }
266                }
267            }
268            Err(e) => {
269                return Err(e);
270            }
271        };
272
273        Ok(processedResponse)
274    }
275
276    /// Create an email attribute.
277    /// 
278    pub fn create_email_attribute(&self, collection_id: &str, key: &str, required: bool, default: Option<&str>, array: Option<bool>) -> Result<models::AttributeEmail, AppwriteException> {
279        let path = "/database/collections/collectionId/attributes/email".replace("collectionId", &collection_id);
280        let  headers: HashMap<String, String> = [
281            ("content-type".to_string(), "application/json".to_string()),
282        ].iter().cloned().collect();
283
284        let default:&str = match default {
285            Some(data) => data,
286            None => ""
287        };
288
289        let  params: HashMap<String, ParamType> = [
290            ("key".to_string(), ParamType::String(key.to_string())),
291            ("required".to_string(), ParamType::Bool(required)),
292            ("default".to_string(), ParamType::String(default.to_string())),
293            ("array".to_string(), ParamType::OptionalBool(array)),
294        ].iter().cloned().collect();
295
296        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
297
298        let processedResponse:models::AttributeEmail = match response {
299            Ok(r) => {
300                match r.json() {
301                    Ok(json) => json,
302                    Err(e) => {
303                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
304                    }
305                }
306            }
307            Err(e) => {
308                return Err(e);
309            }
310        };
311
312        Ok(processedResponse)
313    }
314
315    pub fn create_enum_attribute(&self, collection_id: &str, key: &str, elements: &[&str], required: bool, default: Option<&str>, array: Option<bool>) -> Result<models::AttributeEnum, AppwriteException> {
316        let path = "/database/collections/collectionId/attributes/enum".replace("collectionId", &collection_id);
317        let  headers: HashMap<String, String> = [
318            ("content-type".to_string(), "application/json".to_string()),
319        ].iter().cloned().collect();
320
321        let default:&str = match default {
322            Some(data) => data,
323            None => ""
324        };
325
326        let  params: HashMap<String, ParamType> = [
327            ("key".to_string(), ParamType::String(key.to_string())),
328            ("elements".to_string(), ParamType::Array(elements.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
329            ("required".to_string(), ParamType::Bool(required)),
330            ("default".to_string(), ParamType::String(default.to_string())),
331            ("array".to_string(), ParamType::OptionalBool(array)),
332        ].iter().cloned().collect();
333
334        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
335
336        let processedResponse:models::AttributeEnum = match response {
337            Ok(r) => {
338                match r.json() {
339                    Ok(json) => json,
340                    Err(e) => {
341                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
342                    }
343                }
344            }
345            Err(e) => {
346                return Err(e);
347            }
348        };
349
350        Ok(processedResponse)
351    }
352
353    /// Create a float attribute. Optionally, minimum and maximum values can be
354    /// provided.
355    /// 
356    pub fn create_float_attribute(&self, collection_id: &str, key: &str, required: bool, min: Option<f64>, max: Option<f64>, default: Option<f64>, array: Option<bool>) -> Result<models::AttributeFloat, AppwriteException> {
357        let path = "/database/collections/collectionId/attributes/float".replace("collectionId", &collection_id);
358        let  headers: HashMap<String, String> = [
359            ("content-type".to_string(), "application/json".to_string()),
360        ].iter().cloned().collect();
361
362        let  params: HashMap<String, ParamType> = [
363            ("key".to_string(), ParamType::String(key.to_string())),
364            ("required".to_string(), ParamType::Bool(required)),
365            ("min".to_string(),  ParamType::OptionalFloat(min)),
366            ("max".to_string(),  ParamType::OptionalFloat(max)),
367            ("default".to_string(),  ParamType::OptionalFloat(default)),
368            ("array".to_string(), ParamType::OptionalBool(array)),
369        ].iter().cloned().collect();
370
371        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
372
373        let processedResponse:models::AttributeFloat = match response {
374            Ok(r) => {
375                match r.json() {
376                    Ok(json) => json,
377                    Err(e) => {
378                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
379                    }
380                }
381            }
382            Err(e) => {
383                return Err(e);
384            }
385        };
386
387        Ok(processedResponse)
388    }
389
390    /// Create an integer attribute. Optionally, minimum and maximum values can be
391    /// provided.
392    /// 
393    pub fn create_integer_attribute(&self, collection_id: &str, key: &str, required: bool, min: Option<i64>, max: Option<i64>, default: Option<i64>, array: Option<bool>) -> Result<models::AttributeInteger, AppwriteException> {
394        let path = "/database/collections/collectionId/attributes/integer".replace("collectionId", &collection_id);
395        let  headers: HashMap<String, String> = [
396            ("content-type".to_string(), "application/json".to_string()),
397        ].iter().cloned().collect();
398
399        let  params: HashMap<String, ParamType> = [
400            ("key".to_string(), ParamType::String(key.to_string())),
401            ("required".to_string(), ParamType::Bool(required)),
402            ("min".to_string(),  ParamType::OptionalNumber(min)),
403            ("max".to_string(),  ParamType::OptionalNumber(max)),
404            ("default".to_string(),  ParamType::OptionalNumber(default)),
405            ("array".to_string(), ParamType::OptionalBool(array)),
406        ].iter().cloned().collect();
407
408        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
409
410        let processedResponse:models::AttributeInteger = match response {
411            Ok(r) => {
412                match r.json() {
413                    Ok(json) => json,
414                    Err(e) => {
415                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
416                    }
417                }
418            }
419            Err(e) => {
420                return Err(e);
421            }
422        };
423
424        Ok(processedResponse)
425    }
426
427    /// Create IP address attribute.
428    /// 
429    pub fn create_ip_attribute(&self, collection_id: &str, key: &str, required: bool, default: Option<&str>, array: Option<bool>) -> Result<models::AttributeIp, AppwriteException> {
430        let path = "/database/collections/collectionId/attributes/ip".replace("collectionId", &collection_id);
431        let  headers: HashMap<String, String> = [
432            ("content-type".to_string(), "application/json".to_string()),
433        ].iter().cloned().collect();
434
435        let default:&str = match default {
436            Some(data) => data,
437            None => ""
438        };
439
440        let  params: HashMap<String, ParamType> = [
441            ("key".to_string(), ParamType::String(key.to_string())),
442            ("required".to_string(), ParamType::Bool(required)),
443            ("default".to_string(), ParamType::String(default.to_string())),
444            ("array".to_string(), ParamType::OptionalBool(array)),
445        ].iter().cloned().collect();
446
447        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
448
449        let processedResponse:models::AttributeIp = match response {
450            Ok(r) => {
451                match r.json() {
452                    Ok(json) => json,
453                    Err(e) => {
454                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
455                    }
456                }
457            }
458            Err(e) => {
459                return Err(e);
460            }
461        };
462
463        Ok(processedResponse)
464    }
465
466    /// Create a string attribute.
467    /// 
468    pub fn create_string_attribute(&self, collection_id: &str, key: &str, size: i64, required: bool, default: Option<&str>, array: Option<bool>) -> Result<models::AttributeString, AppwriteException> {
469        let path = "/database/collections/collectionId/attributes/string".replace("collectionId", &collection_id);
470        let  headers: HashMap<String, String> = [
471            ("content-type".to_string(), "application/json".to_string()),
472        ].iter().cloned().collect();
473
474        let default:&str = match default {
475            Some(data) => data,
476            None => ""
477        };
478
479        let  params: HashMap<String, ParamType> = [
480            ("key".to_string(), ParamType::String(key.to_string())),
481            ("size".to_string(),  ParamType::Number(size)),
482            ("required".to_string(), ParamType::Bool(required)),
483            ("default".to_string(), ParamType::String(default.to_string())),
484            ("array".to_string(), ParamType::OptionalBool(array)),
485        ].iter().cloned().collect();
486
487        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
488
489        let processedResponse:models::AttributeString = match response {
490            Ok(r) => {
491                match r.json() {
492                    Ok(json) => json,
493                    Err(e) => {
494                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
495                    }
496                }
497            }
498            Err(e) => {
499                return Err(e);
500            }
501        };
502
503        Ok(processedResponse)
504    }
505
506    /// Create a URL attribute.
507    /// 
508    pub fn create_url_attribute(&self, collection_id: &str, key: &str, required: bool, default: Option<&str>, array: Option<bool>) -> Result<models::AttributeUrl, AppwriteException> {
509        let path = "/database/collections/collectionId/attributes/url".replace("collectionId", &collection_id);
510        let  headers: HashMap<String, String> = [
511            ("content-type".to_string(), "application/json".to_string()),
512        ].iter().cloned().collect();
513
514        let default:&str = match default {
515            Some(data) => data,
516            None => ""
517        };
518
519        let  params: HashMap<String, ParamType> = [
520            ("key".to_string(), ParamType::String(key.to_string())),
521            ("required".to_string(), ParamType::Bool(required)),
522            ("default".to_string(), ParamType::String(default.to_string())),
523            ("array".to_string(), ParamType::OptionalBool(array)),
524        ].iter().cloned().collect();
525
526        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
527
528        let processedResponse:models::AttributeUrl = match response {
529            Ok(r) => {
530                match r.json() {
531                    Ok(json) => json,
532                    Err(e) => {
533                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
534                    }
535                }
536            }
537            Err(e) => {
538                return Err(e);
539            }
540        };
541
542        Ok(processedResponse)
543    }
544
545    pub fn get_attribute(&self, collection_id: &str, key: &str) -> Result<serde_json::value::Value, AppwriteException> {
546        let path = "/database/collections/collectionId/attributes/key".replace("collectionId", &collection_id).replace("key", &key);
547        let  headers: HashMap<String, String> = [
548            ("content-type".to_string(), "application/json".to_string()),
549        ].iter().cloned().collect();
550
551        let  params: HashMap<String, ParamType> = [
552        ].iter().cloned().collect();
553
554        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
555
556        match response {
557            Ok(r) => {
558                let status_code = r.status();
559                if status_code == reqwest::StatusCode::NO_CONTENT {
560                    Ok(json!(true))
561                } else {
562                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
563                }
564            }
565            Err(e) => {
566                Err(e)
567            }
568        }
569    }
570
571    pub fn delete_attribute(&self, collection_id: &str, key: &str) -> Result<serde_json::value::Value, AppwriteException> {
572        let path = "/database/collections/collectionId/attributes/key".replace("collectionId", &collection_id).replace("key", &key);
573        let  headers: HashMap<String, String> = [
574            ("content-type".to_string(), "application/json".to_string()),
575        ].iter().cloned().collect();
576
577        let  params: HashMap<String, ParamType> = [
578        ].iter().cloned().collect();
579
580        let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
581
582        match response {
583            Ok(r) => {
584                let status_code = r.status();
585                if status_code == reqwest::StatusCode::NO_CONTENT {
586                    Ok(json!(true))
587                } else {
588                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
589                }
590            }
591            Err(e) => {
592                Err(e)
593            }
594        }
595    }
596
597    /// Get a list of all the user documents. You can use the query params to
598    /// filter your results. On admin mode, this endpoint will return a list of all
599    /// of the project's documents. [Learn more about different API
600    /// modes](/docs/admin).
601    pub fn list_documents(&self, collection_id: &str, queries: Option<&[&str]>, limit: Option<i64>, offset: Option<i64>, cursor: Option<&str>, cursor_direction: Option<&str>, order_attributes: Option<&[&str]>, order_types: Option<&[&str]>) -> Result<models::DocumentList, AppwriteException> {
602        let path = "/database/collections/collectionId/documents".replace("collectionId", &collection_id);
603        let  headers: HashMap<String, String> = [
604            ("content-type".to_string(), "application/json".to_string()),
605        ].iter().cloned().collect();
606
607        let queries:&[&str] = match queries {
608            Some(data) => data,
609            None => &[]
610        };
611
612        let cursor:&str = match cursor {
613            Some(data) => data,
614            None => ""
615        };
616
617        let cursor_direction:&str = match cursor_direction {
618            Some(data) => data,
619            None => ""
620        };
621
622        let order_attributes:&[&str] = match order_attributes {
623            Some(data) => data,
624            None => &[]
625        };
626
627        let order_types:&[&str] = match order_types {
628            Some(data) => data,
629            None => &[]
630        };
631
632        let  params: HashMap<String, ParamType> = [
633            ("queries".to_string(), ParamType::Array(queries.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
634            ("limit".to_string(),  ParamType::OptionalNumber(limit)),
635            ("offset".to_string(),  ParamType::OptionalNumber(offset)),
636            ("cursor".to_string(), ParamType::String(cursor.to_string())),
637            ("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
638            ("orderAttributes".to_string(), ParamType::Array(order_attributes.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
639            ("orderTypes".to_string(), ParamType::Array(order_types.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
640        ].iter().cloned().collect();
641
642        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
643
644        let processedResponse:models::DocumentList = match response {
645            Ok(r) => {
646                match r.json() {
647                    Ok(json) => json,
648                    Err(e) => {
649                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
650                    }
651                }
652            }
653            Err(e) => {
654                return Err(e);
655            }
656        };
657
658        Ok(processedResponse)
659    }
660
661    /// Create a new Document. Before using this route, you should create a new
662    /// collection resource using either a [server
663    /// integration](/docs/server/database#databaseCreateCollection) API or
664    /// directly from your database console.
665    pub fn create_document(&self, collection_id: &str, document_id: &str, data: Option<HashMap<String, crate::client::ParamType>>, read: Option<&[&str]>, write: Option<&[&str]>) -> Result<models::Document, AppwriteException> {
666        let path = "/database/collections/collectionId/documents".replace("collectionId", &collection_id);
667        let  headers: HashMap<String, String> = [
668            ("content-type".to_string(), "application/json".to_string()),
669        ].iter().cloned().collect();
670
671        let read:&[&str] = match read {
672            Some(data) => data,
673            None => &[]
674        };
675
676        let write:&[&str] = match write {
677            Some(data) => data,
678            None => &[]
679        };
680
681        let  params: HashMap<String, ParamType> = [
682            ("documentId".to_string(), ParamType::String(document_id.to_string())),
683            ("data".to_string(), ParamType::Object(data.unwrap())),
684            ("read".to_string(), ParamType::Array(read.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
685            ("write".to_string(), ParamType::Array(write.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
686        ].iter().cloned().collect();
687
688        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
689
690        let processedResponse:models::Document = match response {
691            Ok(r) => {
692                match r.json() {
693                    Ok(json) => json,
694                    Err(e) => {
695                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
696                    }
697                }
698            }
699            Err(e) => {
700                return Err(e);
701            }
702        };
703
704        Ok(processedResponse)
705    }
706
707    /// Get a document by its unique ID. This endpoint response returns a JSON
708    /// object with the document data.
709    pub fn get_document(&self, collection_id: &str, document_id: &str) -> Result<models::Document, AppwriteException> {
710        let path = "/database/collections/collectionId/documents/documentId".replace("collectionId", &collection_id).replace("documentId", &document_id);
711        let  headers: HashMap<String, String> = [
712            ("content-type".to_string(), "application/json".to_string()),
713        ].iter().cloned().collect();
714
715        let  params: HashMap<String, ParamType> = [
716        ].iter().cloned().collect();
717
718        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
719
720        let processedResponse:models::Document = match response {
721            Ok(r) => {
722                match r.json() {
723                    Ok(json) => json,
724                    Err(e) => {
725                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
726                    }
727                }
728            }
729            Err(e) => {
730                return Err(e);
731            }
732        };
733
734        Ok(processedResponse)
735    }
736
737    /// Update a document by its unique ID. Using the patch method you can pass
738    /// only specific fields that will get updated.
739    pub fn update_document(&self, collection_id: &str, document_id: &str, data: Option<HashMap<String, crate::client::ParamType>>, read: Option<&[&str]>, write: Option<&[&str]>) -> Result<models::Document, AppwriteException> {
740        let path = "/database/collections/collectionId/documents/documentId".replace("collectionId", &collection_id).replace("documentId", &document_id);
741        let  headers: HashMap<String, String> = [
742            ("content-type".to_string(), "application/json".to_string()),
743        ].iter().cloned().collect();
744
745        let read:&[&str] = match read {
746            Some(data) => data,
747            None => &[]
748        };
749
750        let write:&[&str] = match write {
751            Some(data) => data,
752            None => &[]
753        };
754
755        let  params: HashMap<String, ParamType> = [
756            ("data".to_string(), ParamType::Object(data.unwrap())),
757            ("read".to_string(), ParamType::Array(read.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
758            ("write".to_string(), ParamType::Array(write.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
759        ].iter().cloned().collect();
760
761        let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
762
763        let processedResponse:models::Document = match response {
764            Ok(r) => {
765                match r.json() {
766                    Ok(json) => json,
767                    Err(e) => {
768                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
769                    }
770                }
771            }
772            Err(e) => {
773                return Err(e);
774            }
775        };
776
777        Ok(processedResponse)
778    }
779
780    /// Delete a document by its unique ID. This endpoint deletes only the parent
781    /// documents, its attributes and relations to other documents. Child documents
782    /// **will not** be deleted.
783    pub fn delete_document(&self, collection_id: &str, document_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
784        let path = "/database/collections/collectionId/documents/documentId".replace("collectionId", &collection_id).replace("documentId", &document_id);
785        let  headers: HashMap<String, String> = [
786            ("content-type".to_string(), "application/json".to_string()),
787        ].iter().cloned().collect();
788
789        let  params: HashMap<String, ParamType> = [
790        ].iter().cloned().collect();
791
792        let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
793
794        match response {
795            Ok(r) => {
796                let status_code = r.status();
797                if status_code == reqwest::StatusCode::NO_CONTENT {
798                    Ok(json!(true))
799                } else {
800                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
801                }
802            }
803            Err(e) => {
804                Err(e)
805            }
806        }
807    }
808
809    /// Get the document activity logs list by its unique ID.
810    pub fn list_document_logs(&self, collection_id: &str, document_id: &str, limit: Option<i64>, offset: Option<i64>) -> Result<models::LogList, AppwriteException> {
811        let path = "/database/collections/collectionId/documents/documentId/logs".replace("collectionId", &collection_id).replace("documentId", &document_id);
812        let  headers: HashMap<String, String> = [
813            ("content-type".to_string(), "application/json".to_string()),
814        ].iter().cloned().collect();
815
816        let  params: HashMap<String, ParamType> = [
817            ("limit".to_string(),  ParamType::OptionalNumber(limit)),
818            ("offset".to_string(),  ParamType::OptionalNumber(offset)),
819        ].iter().cloned().collect();
820
821        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
822
823        let processedResponse:models::LogList = match response {
824            Ok(r) => {
825                match r.json() {
826                    Ok(json) => json,
827                    Err(e) => {
828                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
829                    }
830                }
831            }
832            Err(e) => {
833                return Err(e);
834            }
835        };
836
837        Ok(processedResponse)
838    }
839
840    pub fn list_indexes(&self, collection_id: &str) -> Result<models::IndexList, AppwriteException> {
841        let path = "/database/collections/collectionId/indexes".replace("collectionId", &collection_id);
842        let  headers: HashMap<String, String> = [
843            ("content-type".to_string(), "application/json".to_string()),
844        ].iter().cloned().collect();
845
846        let  params: HashMap<String, ParamType> = [
847        ].iter().cloned().collect();
848
849        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
850
851        let processedResponse:models::IndexList = match response {
852            Ok(r) => {
853                match r.json() {
854                    Ok(json) => json,
855                    Err(e) => {
856                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
857                    }
858                }
859            }
860            Err(e) => {
861                return Err(e);
862            }
863        };
864
865        Ok(processedResponse)
866    }
867
868    pub fn create_index(&self, collection_id: &str, key: &str, xtype: &str, attributes: &[&str], orders: Option<&[&str]>) -> Result<models::Index, AppwriteException> {
869        let path = "/database/collections/collectionId/indexes".replace("collectionId", &collection_id);
870        let  headers: HashMap<String, String> = [
871            ("content-type".to_string(), "application/json".to_string()),
872        ].iter().cloned().collect();
873
874        let orders:&[&str] = match orders {
875            Some(data) => data,
876            None => &[]
877        };
878
879        let  params: HashMap<String, ParamType> = [
880            ("key".to_string(), ParamType::String(key.to_string())),
881            ("type".to_string(), ParamType::String(xtype.to_string())),
882            ("attributes".to_string(), ParamType::Array(attributes.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
883            ("orders".to_string(), ParamType::Array(orders.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
884        ].iter().cloned().collect();
885
886        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
887
888        let processedResponse:models::Index = match response {
889            Ok(r) => {
890                match r.json() {
891                    Ok(json) => json,
892                    Err(e) => {
893                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
894                    }
895                }
896            }
897            Err(e) => {
898                return Err(e);
899            }
900        };
901
902        Ok(processedResponse)
903    }
904
905    pub fn get_index(&self, collection_id: &str, key: &str) -> Result<models::Index, AppwriteException> {
906        let path = "/database/collections/collectionId/indexes/key".replace("collectionId", &collection_id).replace("key", &key);
907        let  headers: HashMap<String, String> = [
908            ("content-type".to_string(), "application/json".to_string()),
909        ].iter().cloned().collect();
910
911        let  params: HashMap<String, ParamType> = [
912        ].iter().cloned().collect();
913
914        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
915
916        let processedResponse:models::Index = match response {
917            Ok(r) => {
918                match r.json() {
919                    Ok(json) => json,
920                    Err(e) => {
921                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
922                    }
923                }
924            }
925            Err(e) => {
926                return Err(e);
927            }
928        };
929
930        Ok(processedResponse)
931    }
932
933    pub fn delete_index(&self, collection_id: &str, key: &str) -> Result<serde_json::value::Value, AppwriteException> {
934        let path = "/database/collections/collectionId/indexes/key".replace("collectionId", &collection_id).replace("key", &key);
935        let  headers: HashMap<String, String> = [
936            ("content-type".to_string(), "application/json".to_string()),
937        ].iter().cloned().collect();
938
939        let  params: HashMap<String, ParamType> = [
940        ].iter().cloned().collect();
941
942        let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
943
944        match response {
945            Ok(r) => {
946                let status_code = r.status();
947                if status_code == reqwest::StatusCode::NO_CONTENT {
948                    Ok(json!(true))
949                } else {
950                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
951                }
952            }
953            Err(e) => {
954                Err(e)
955            }
956        }
957    }
958
959    /// Get the collection activity logs list by its unique ID.
960    pub fn list_collection_logs(&self, collection_id: &str, limit: Option<i64>, offset: Option<i64>) -> Result<models::LogList, AppwriteException> {
961        let path = "/database/collections/collectionId/logs".replace("collectionId", &collection_id);
962        let  headers: HashMap<String, String> = [
963            ("content-type".to_string(), "application/json".to_string()),
964        ].iter().cloned().collect();
965
966        let  params: HashMap<String, ParamType> = [
967            ("limit".to_string(),  ParamType::OptionalNumber(limit)),
968            ("offset".to_string(),  ParamType::OptionalNumber(offset)),
969        ].iter().cloned().collect();
970
971        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
972
973        let processedResponse:models::LogList = match response {
974            Ok(r) => {
975                match r.json() {
976                    Ok(json) => json,
977                    Err(e) => {
978                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
979                    }
980                }
981            }
982            Err(e) => {
983                return Err(e);
984            }
985        };
986
987        Ok(processedResponse)
988    }
989
990    pub fn get_usage(&self, range: Option<&str>) -> Result<models::UsageDatabase, AppwriteException> {
991        let path = "/database/usage";
992        let  headers: HashMap<String, String> = [
993            ("content-type".to_string(), "application/json".to_string()),
994        ].iter().cloned().collect();
995
996        let range:&str = match range {
997            Some(data) => data,
998            None => ""
999        };
1000
1001        let  params: HashMap<String, ParamType> = [
1002            ("range".to_string(), ParamType::String(range.to_string())),
1003        ].iter().cloned().collect();
1004
1005        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
1006
1007        let processedResponse:models::UsageDatabase = match response {
1008            Ok(r) => {
1009                match r.json() {
1010                    Ok(json) => json,
1011                    Err(e) => {
1012                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
1013                    }
1014                }
1015            }
1016            Err(e) => {
1017                return Err(e);
1018            }
1019        };
1020
1021        Ok(processedResponse)
1022    }
1023
1024    pub fn get_collection_usage(&self, collection_id: &str, range: Option<&str>) -> Result<models::UsageCollection, AppwriteException> {
1025        let path = "/database/collectionId/usage".replace("collectionId", &collection_id);
1026        let  headers: HashMap<String, String> = [
1027            ("content-type".to_string(), "application/json".to_string()),
1028        ].iter().cloned().collect();
1029
1030        let range:&str = match range {
1031            Some(data) => data,
1032            None => ""
1033        };
1034
1035        let  params: HashMap<String, ParamType> = [
1036            ("range".to_string(), ParamType::String(range.to_string())),
1037        ].iter().cloned().collect();
1038
1039        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
1040
1041        let processedResponse:models::UsageCollection = match response {
1042            Ok(r) => {
1043                match r.json() {
1044                    Ok(json) => json,
1045                    Err(e) => {
1046                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
1047                    }
1048                }
1049            }
1050            Err(e) => {
1051                return Err(e);
1052            }
1053        };
1054
1055        Ok(processedResponse)
1056    }
1057}