use reqwest::Method;
use serde::Serialize;
use super::seg;
use crate::models::{UnifiedApiMetaResponse, UnifiedMetaGetResponse};
use crate::{Client, Pager, Record, Request};
#[derive(Debug, Clone, Copy)]
pub struct UnifiedApi<'a> {
client: &'a Client,
}
impl<'a> UnifiedApi<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
fn path(model: &str, resource: &str) -> String {
format!("unified/{}/{}", seg(model), seg(resource))
}
fn req(&self, method: Method, path: String, integrated_account_id: &str) -> Request<Record> {
let mut req = self.client.request(method, path);
req.query_ref()
.insert("integrated_account_id", integrated_account_id);
req
}
pub fn list(&self, model: &str, resource: &str, integrated_account_id: &str) -> Pager<Record> {
let mut pager = self.client.pager(Self::path(model, resource));
pager
.query_ref()
.insert("integrated_account_id", integrated_account_id);
pager
}
pub fn get(
&self,
model: &str,
resource: &str,
id: &str,
integrated_account_id: &str,
) -> Request<Record> {
let path = format!("{}/{}", Self::path(model, resource), seg(id));
self.req(Method::GET, path, integrated_account_id)
}
pub fn create(
&self,
model: &str,
resource: &str,
integrated_account_id: &str,
body: impl Serialize,
) -> Request<Record> {
self.req(
Method::POST,
Self::path(model, resource),
integrated_account_id,
)
.body(body)
}
pub fn update(
&self,
model: &str,
resource: &str,
id: &str,
integrated_account_id: &str,
body: impl Serialize,
) -> Request<Record> {
let path = format!("{}/{}", Self::path(model, resource), seg(id));
self.req(Method::PATCH, path, integrated_account_id)
.body(body)
}
pub fn delete(
&self,
model: &str,
resource: &str,
id: &str,
integrated_account_id: &str,
) -> Request<Record> {
let path = format!("{}/{}", Self::path(model, resource), seg(id));
self.req(Method::DELETE, path, integrated_account_id)
}
pub fn custom_method(
&self,
model: &str,
resource: &str,
method: &str,
integrated_account_id: &str,
) -> Request<Record> {
let path = format!("{}/{}", Self::path(model, resource), seg(method));
self.req(Method::POST, path, integrated_account_id)
}
pub fn meta(
&self,
model: &str,
resource: &str,
integration_name: &str,
method: &str,
) -> Request<UnifiedApiMetaResponse> {
self.client.request(
Method::GET,
format!(
"{}/{}/meta/{}",
Self::path(model, resource),
seg(integration_name),
seg(method)
),
)
}
pub fn model_meta(
&self,
model: &str,
integration_name: &str,
) -> Request<UnifiedMetaGetResponse> {
self.client.request(
Method::GET,
format!("unified/meta/{}/{}", seg(model), seg(integration_name)),
)
}
}