use crate::client::{Client, ParamType};
use std::collections::HashMap;
use crate::services::AppwriteException;
use crate::models;
use serde_json::json;
use std::io::Read;
#[derive(Clone)]
pub struct Avatars {
client: Client
}
impl Avatars {
pub fn new(client: &Client) -> Self {
Self {
client: client.clone()
}
}
pub fn get_browser(&self, code: &str, width: Option<i64>, height: Option<i64>, quality: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/browsers/code".replace("code", &code);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("width".to_string(), ParamType::OptionalNumber(width)),
("height".to_string(), ParamType::OptionalNumber(height)),
("quality".to_string(), ParamType::OptionalNumber(quality)),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_credit_card(&self, code: &str, width: Option<i64>, height: Option<i64>, quality: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/credit-cards/code".replace("code", &code);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("width".to_string(), ParamType::OptionalNumber(width)),
("height".to_string(), ParamType::OptionalNumber(height)),
("quality".to_string(), ParamType::OptionalNumber(quality)),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_favicon(&self, url: &str) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/favicon";
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("url".to_string(), ParamType::String(url.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_flag(&self, code: &str, width: Option<i64>, height: Option<i64>, quality: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/flags/code".replace("code", &code);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("width".to_string(), ParamType::OptionalNumber(width)),
("height".to_string(), ParamType::OptionalNumber(height)),
("quality".to_string(), ParamType::OptionalNumber(quality)),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_image(&self, url: &str, width: Option<i64>, height: Option<i64>) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/image";
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("url".to_string(), ParamType::String(url.to_string())),
("width".to_string(), ParamType::OptionalNumber(width)),
("height".to_string(), ParamType::OptionalNumber(height)),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_initials(&self, name: Option<&str>, width: Option<i64>, height: Option<i64>, color: Option<&str>, background: Option<&str>) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/initials";
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let name:&str = match name {
Some(data) => data,
None => ""
};
let color:&str = match color {
Some(data) => data,
None => ""
};
let background:&str = match background {
Some(data) => data,
None => ""
};
let params: HashMap<String, ParamType> = [
("name".to_string(), ParamType::String(name.to_string())),
("width".to_string(), ParamType::OptionalNumber(width)),
("height".to_string(), ParamType::OptionalNumber(height)),
("color".to_string(), ParamType::String(color.to_string())),
("background".to_string(), ParamType::String(background.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_qr(&self, text: &str, size: Option<i64>, margin: Option<i64>, download: Option<bool>) -> Result<Vec<u8>, AppwriteException> {
let path = "/avatars/qr";
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("text".to_string(), ParamType::String(text.to_string())),
("size".to_string(), ParamType::OptionalNumber(size)),
("margin".to_string(), ParamType::OptionalNumber(margin)),
("download".to_string(), ParamType::OptionalBool(download)),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:Vec<u8> = match response {
Ok(mut r) => {
let mut buf: Vec<u8> = vec![];
match r.copy_to(&mut buf) {
Ok(_) => (),
Err(e) => {
return Err(AppwriteException::new(format!("Error copying response to buffer: {}", e), 0, "".to_string()));
}
};
buf
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
}