use crate::client::Client;
use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Teams {
client: Client,
}
impl Teams {
pub fn new(client: &Client) -> Self {
Self { client: client.clone() }
}
pub fn client(&self) -> &Client {
&self.client
}
pub async fn list(
&self,
queries: Option<Vec<String>>,
search: Option<&str>,
total: Option<bool>,
) -> crate::error::Result<crate::models::TeamList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert("queries".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = search {
params.insert("search".to_string(), json!(value));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let path = "/teams".to_string();
self.client.call(Method::GET, &path, None, Some(params)).await
}
pub async fn create(
&self,
team_id: impl Into<String>,
name: impl Into<String>,
roles: Option<Vec<String>>,
) -> crate::error::Result<crate::models::Team> {
let mut params = HashMap::new();
params.insert("teamId".to_string(), json!(team_id.into()));
params.insert("name".to_string(), json!(name.into()));
if let Some(value) = roles {
params.insert("roles".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn get(
&self,
team_id: impl Into<String>,
) -> crate::error::Result<crate::models::Team> {
let params = HashMap::new();
let path = "/teams/{teamId}".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::GET, &path, None, Some(params)).await
}
pub async fn update_name(
&self,
team_id: impl Into<String>,
name: impl Into<String>,
) -> crate::error::Result<crate::models::Team> {
let mut params = HashMap::new();
params.insert("name".to_string(), json!(name.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn delete(
&self,
team_id: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::DELETE, &path, Some(api_headers), Some(params)).await
}
pub async fn list_memberships(
&self,
team_id: impl Into<String>,
queries: Option<Vec<String>>,
search: Option<&str>,
total: Option<bool>,
) -> crate::error::Result<crate::models::MembershipList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert("queries".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = search {
params.insert("search".to_string(), json!(value));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let path = "/teams/{teamId}/memberships".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::GET, &path, None, Some(params)).await
}
#[allow(clippy::too_many_arguments)]
pub async fn create_membership(
&self,
team_id: impl Into<String>,
roles: impl IntoIterator<Item = impl Into<String>>,
email: Option<&str>,
user_id: Option<&str>,
phone: Option<&str>,
url: Option<&str>,
name: Option<&str>,
) -> crate::error::Result<crate::models::Membership> {
let mut params = HashMap::new();
if let Some(value) = email {
params.insert("email".to_string(), json!(value));
}
if let Some(value) = user_id {
params.insert("userId".to_string(), json!(value));
}
if let Some(value) = phone {
params.insert("phone".to_string(), json!(value));
}
params.insert("roles".to_string(), json!(roles.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
if let Some(value) = url {
params.insert("url".to_string(), json!(value));
}
if let Some(value) = name {
params.insert("name".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}/memberships".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn get_membership(
&self,
team_id: impl Into<String>,
membership_id: impl Into<String>,
) -> crate::error::Result<crate::models::Membership> {
let params = HashMap::new();
let path = "/teams/{teamId}/memberships/{membershipId}".to_string().replace("{teamId}", &team_id.into().to_string()).replace("{membershipId}", &membership_id.into().to_string());
self.client.call(Method::GET, &path, None, Some(params)).await
}
pub async fn update_membership(
&self,
team_id: impl Into<String>,
membership_id: impl Into<String>,
roles: impl IntoIterator<Item = impl Into<String>>,
) -> crate::error::Result<crate::models::Membership> {
let mut params = HashMap::new();
params.insert("roles".to_string(), json!(roles.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}/memberships/{membershipId}".to_string().replace("{teamId}", &team_id.into().to_string()).replace("{membershipId}", &membership_id.into().to_string());
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn delete_membership(
&self,
team_id: impl Into<String>,
membership_id: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}/memberships/{membershipId}".to_string().replace("{teamId}", &team_id.into().to_string()).replace("{membershipId}", &membership_id.into().to_string());
self.client.call(Method::DELETE, &path, Some(api_headers), Some(params)).await
}
pub async fn update_membership_status(
&self,
team_id: impl Into<String>,
membership_id: impl Into<String>,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Membership> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}/memberships/{membershipId}/status".to_string().replace("{teamId}", &team_id.into().to_string()).replace("{membershipId}", &membership_id.into().to_string());
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn get_prefs(
&self,
team_id: impl Into<String>,
) -> crate::error::Result<crate::models::Preferences> {
let params = HashMap::new();
let path = "/teams/{teamId}/prefs".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::GET, &path, None, Some(params)).await
}
pub async fn update_prefs(
&self,
team_id: impl Into<String>,
prefs: serde_json::Value,
) -> crate::error::Result<crate::models::Preferences> {
let mut params = HashMap::new();
params.insert("prefs".to_string(), json!(prefs));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/teams/{teamId}/prefs".to_string().replace("{teamId}", &team_id.into().to_string());
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
}
impl crate::services::Service for Teams {
fn client(&self) -> &Client {
&self.client
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_teams_creation() {
let client = Client::new();
let service = Teams::new(&client);
assert!(service.client().endpoint().contains("cloud.appwrite.io/v1"));
}
}