use std::sync::Arc;
use crate::error::Result;
use crate::http::Config;
use crate::types::{
list_query, Contact, ContactAddress, ContactId, ContactListItem, ContactTopicUpdate,
CreateContactOptions, DeleteContactResponse, List, ListOptions, UpdateContactOptions,
UpdateContactTopicsResponse,
};
#[derive(Clone)]
pub struct Contacts {
config: Arc<Config>,
pub topics: ContactTopics,
}
impl Contacts {
pub(crate) fn new(config: Arc<Config>) -> Self {
Contacts {
topics: ContactTopics(config.clone()),
config,
}
}
pub async fn create(&self, contact: &CreateContactOptions) -> Result<ContactId> {
self.config.post(&["contacts"], contact, None).await
}
pub async fn get(&self, address: impl Into<ContactAddress>) -> Result<Contact> {
let address = address.into();
self.config.get(&["contacts", address.key()], &[]).await
}
pub async fn update(
&self,
address: impl Into<ContactAddress>,
changes: &UpdateContactOptions,
) -> Result<ContactId> {
let address = address.into();
self.config
.patch(&["contacts", address.key()], changes)
.await
}
pub async fn delete(
&self,
address: impl Into<ContactAddress>,
) -> Result<DeleteContactResponse> {
let address = address.into();
self.config.delete(&["contacts", address.key()]).await
}
pub async fn list(&self, options: Option<&ListOptions>) -> Result<List<ContactListItem>> {
self.config.get(&["contacts"], &list_query(options)).await
}
}
#[derive(Clone)]
pub struct ContactTopics(pub(crate) Arc<Config>);
impl ContactTopics {
pub async fn update(
&self,
address: impl Into<ContactAddress>,
topics: &[ContactTopicUpdate],
) -> Result<UpdateContactTopicsResponse> {
let address = address.into();
self.0
.patch(&["contacts", address.key(), "topics"], topics)
.await
}
}