use crate::client::Client;
use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;
#[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
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/png".to_string());
let path = "/avatars/browsers/{code}".to_string().replace("{code}", &code.to_string());
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/png".to_string());
let path = "/avatars/credit-cards/{code}".to_string().replace("{code}", &code.to_string());
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/*".to_string());
let path = "/avatars/favicon".to_string();
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/png".to_string());
let path = "/avatars/flags/{code}".to_string().replace("{code}", &code.to_string());
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/*".to_string());
let path = "/avatars/image".to_string();
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/png".to_string());
let path = "/avatars/initials".to_string();
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/png".to_string());
let path = "/avatars/qr".to_string();
self.client.call_bytes(Method::GET, &path, Some(api_headers), Some(params)).await
}
#[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::BrowserTheme>,
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 mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "image/png".to_string());
let path = "/avatars/screenshots".to_string();
self.client.call_bytes(Method::GET, &path, Some(api_headers), 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"));
}
}