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 Teams {
client: Client
}
impl Teams {
pub fn new(client: &Client) -> Self {
Self {
client: client.clone()
}
}
pub fn list(&self, search: Option<&str>, limit: Option<i64>, offset: Option<i64>, cursor: Option<&str>, cursor_direction: Option<&str>, order_type: Option<&str>) -> Result<models::TeamList, AppwriteException> {
let path = "/teams";
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let search:&str = match search {
Some(data) => data,
None => ""
};
let cursor:&str = match cursor {
Some(data) => data,
None => ""
};
let cursor_direction:&str = match cursor_direction {
Some(data) => data,
None => ""
};
let order_type:&str = match order_type {
Some(data) => data,
None => ""
};
let params: HashMap<String, ParamType> = [
("search".to_string(), ParamType::String(search.to_string())),
("limit".to_string(), ParamType::OptionalNumber(limit)),
("offset".to_string(), ParamType::OptionalNumber(offset)),
("cursor".to_string(), ParamType::String(cursor.to_string())),
("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
("orderType".to_string(), ParamType::String(order_type.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:models::TeamList = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn create(&self, team_id: &str, name: &str, roles: Option<&[&str]>) -> Result<models::Team, AppwriteException> {
let path = "/teams";
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let roles:&[&str] = match roles {
Some(data) => data,
None => &[]
};
let params: HashMap<String, ParamType> = [
("teamId".to_string(), ParamType::String(team_id.to_string())),
("name".to_string(), ParamType::String(name.to_string())),
("roles".to_string(), ParamType::Array(roles.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
].iter().cloned().collect();
let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
let processedResponse:models::Team = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get(&self, team_id: &str) -> Result<models::Team, AppwriteException> {
let path = "/teams/teamId".replace("teamId", &team_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:models::Team = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn update(&self, team_id: &str, name: &str) -> Result<models::Team, AppwriteException> {
let path = "/teams/teamId".replace("teamId", &team_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("name".to_string(), ParamType::String(name.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("PUT", &path, Some(headers), Some(params) );
let processedResponse:models::Team = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn delete(&self, team_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
let path = "/teams/teamId".replace("teamId", &team_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
].iter().cloned().collect();
let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
match response {
Ok(r) => {
let status_code = r.status();
if status_code == reqwest::StatusCode::NO_CONTENT {
Ok(json!(true))
} else {
Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
}
}
Err(e) => {
Err(e)
}
}
}
pub fn get_memberships(&self, team_id: &str, search: Option<&str>, limit: Option<i64>, offset: Option<i64>, cursor: Option<&str>, cursor_direction: Option<&str>, order_type: Option<&str>) -> Result<models::MembershipList, AppwriteException> {
let path = "/teams/teamId/memberships".replace("teamId", &team_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let search:&str = match search {
Some(data) => data,
None => ""
};
let cursor:&str = match cursor {
Some(data) => data,
None => ""
};
let cursor_direction:&str = match cursor_direction {
Some(data) => data,
None => ""
};
let order_type:&str = match order_type {
Some(data) => data,
None => ""
};
let params: HashMap<String, ParamType> = [
("search".to_string(), ParamType::String(search.to_string())),
("limit".to_string(), ParamType::OptionalNumber(limit)),
("offset".to_string(), ParamType::OptionalNumber(offset)),
("cursor".to_string(), ParamType::String(cursor.to_string())),
("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
("orderType".to_string(), ParamType::String(order_type.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:models::MembershipList = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn create_membership(&self, team_id: &str, email: &str, roles: &[&str], url: &str, name: Option<&str>) -> Result<models::Membership, AppwriteException> {
let path = "/teams/teamId/memberships".replace("teamId", &team_id);
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 params: HashMap<String, ParamType> = [
("email".to_string(), ParamType::String(email.to_string())),
("roles".to_string(), ParamType::Array(roles.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
("url".to_string(), ParamType::String(url.to_string())),
("name".to_string(), ParamType::String(name.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
let processedResponse:models::Membership = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn get_membership(&self, team_id: &str, membership_id: &str) -> Result<models::MembershipList, AppwriteException> {
let path = "/teams/teamId/memberships/membershipId".replace("teamId", &team_id).replace("membershipId", &membership_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
].iter().cloned().collect();
let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
let processedResponse:models::MembershipList = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn update_membership_roles(&self, team_id: &str, membership_id: &str, roles: &[&str]) -> Result<models::Membership, AppwriteException> {
let path = "/teams/teamId/memberships/membershipId".replace("teamId", &team_id).replace("membershipId", &membership_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("roles".to_string(), ParamType::Array(roles.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
].iter().cloned().collect();
let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
let processedResponse:models::Membership = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
pub fn delete_membership(&self, team_id: &str, membership_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
let path = "/teams/teamId/memberships/membershipId".replace("teamId", &team_id).replace("membershipId", &membership_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
].iter().cloned().collect();
let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
match response {
Ok(r) => {
let status_code = r.status();
if status_code == reqwest::StatusCode::NO_CONTENT {
Ok(json!(true))
} else {
Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
}
}
Err(e) => {
Err(e)
}
}
}
pub fn update_membership_status(&self, team_id: &str, membership_id: &str, user_id: &str, secret: &str) -> Result<models::Membership, AppwriteException> {
let path = "/teams/teamId/memberships/membershipId/status".replace("teamId", &team_id).replace("membershipId", &membership_id);
let headers: HashMap<String, String> = [
("content-type".to_string(), "application/json".to_string()),
].iter().cloned().collect();
let params: HashMap<String, ParamType> = [
("userId".to_string(), ParamType::String(user_id.to_string())),
("secret".to_string(), ParamType::String(secret.to_string())),
].iter().cloned().collect();
let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
let processedResponse:models::Membership = match response {
Ok(r) => {
match r.json() {
Ok(json) => json,
Err(e) => {
return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
}
}
}
Err(e) => {
return Err(e);
}
};
Ok(processedResponse)
}
}