rework client a little to make more low level methods available
This commit is contained in:
parent
96402420c1
commit
c39315b2b7
204
src/client.rs
204
src/client.rs
@ -206,12 +206,15 @@ impl Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl InnerClient {
|
impl InnerClient {
|
||||||
|
#[inline]
|
||||||
pub(crate) fn object_url(&self, domain_type: DomainType, id: impl Display) -> String {
|
pub(crate) fn object_url(&self, domain_type: DomainType, id: impl Display) -> String {
|
||||||
format!("{}/objects/{}/{}", self.url, domain_type, id)
|
format!("{}/objects/{}/{}", self.url, domain_type, id)
|
||||||
}
|
}
|
||||||
|
#[inline]
|
||||||
pub(crate) fn collection_url(&self, domain_type: DomainType) -> String {
|
pub(crate) fn collection_url(&self, domain_type: DomainType) -> String {
|
||||||
format!("{}/domain-types/{}/collections/all", self.url, domain_type)
|
format!("{}/domain-types/{}/collections/all", self.url, domain_type)
|
||||||
}
|
}
|
||||||
|
#[inline]
|
||||||
pub(crate) fn bulk_action_url(&self, domain_type: DomainType, action: BulkAction) -> String {
|
pub(crate) fn bulk_action_url(&self, domain_type: DomainType, action: BulkAction) -> String {
|
||||||
format!(
|
format!(
|
||||||
"{}/domain-types/{}/actions/bulk-{}/invoke",
|
"{}/domain-types/{}/actions/bulk-{}/invoke",
|
||||||
@ -282,114 +285,231 @@ impl InnerClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn create_domain_object<E: DomainExtension>(
|
pub(crate) async fn create<O, Q>(
|
||||||
&self,
|
&self,
|
||||||
body: &E::CreationRequest,
|
domain_type: DomainType,
|
||||||
query: &E::CreationQuery,
|
object: &O,
|
||||||
) -> Result<()> {
|
query: &Q
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
O: Serialize,
|
||||||
|
Q: Serialize
|
||||||
|
{
|
||||||
|
let url = self.collection_url(domain_type);
|
||||||
let request = self
|
let request = self
|
||||||
.http
|
.http
|
||||||
.post(self.collection_url(E::DOMAIN_TYPE))
|
.post(url)
|
||||||
.json(body)
|
.json(object)
|
||||||
.query(query)
|
.query(query)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.invoke_api(request).await
|
self.invoke_api(request).await
|
||||||
}
|
}
|
||||||
pub(crate) async fn read_domain_object<E: DomainExtension>(
|
pub(crate) async fn create_domain_object<E: DomainExtension>(
|
||||||
&self,
|
&self,
|
||||||
|
object: &E::CreationRequest,
|
||||||
|
query: &E::CreationQuery,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.create(E::DOMAIN_TYPE, object, query).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn read<O, Q>(
|
||||||
|
&self,
|
||||||
|
domain_type: DomainType,
|
||||||
id: impl Display,
|
id: impl Display,
|
||||||
query: &E::ReadQuery,
|
query: &Q,
|
||||||
) -> Result<DomainObject<E>> {
|
) -> Result<O>
|
||||||
|
where
|
||||||
|
O: DeserializeOwned,
|
||||||
|
Q: Serialize
|
||||||
|
{
|
||||||
|
let url = self.object_url(domain_type, id);
|
||||||
let request = self
|
let request = self
|
||||||
.http
|
.http
|
||||||
.get(self.object_url(E::DOMAIN_TYPE, id))
|
.get(url)
|
||||||
.query(query)
|
.query(query)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.query_api(request).await
|
self.query_api(request).await
|
||||||
}
|
}
|
||||||
pub(crate) async fn update_domain_object<E: DomainExtension>(
|
pub(crate) async fn read_domain_object<E: DomainExtension>(
|
||||||
&self,
|
&self,
|
||||||
id: impl Display,
|
id: impl Display,
|
||||||
request: &E::UpdateRequest,
|
query: &E::ReadQuery,
|
||||||
) -> Result<()> {
|
) -> Result<DomainObject<E>> {
|
||||||
let url = self.object_url(E::DOMAIN_TYPE, id);
|
self.read(E::DOMAIN_TYPE, id, query).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn update<O>(
|
||||||
|
&self,
|
||||||
|
domain_type: DomainType,
|
||||||
|
id: impl Display,
|
||||||
|
object: &O,
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
O: Serialize,
|
||||||
|
{
|
||||||
|
let url = self.object_url(domain_type, id);
|
||||||
let etag = self.get_etag(&url).await?;
|
let etag = self.get_etag(&url).await?;
|
||||||
|
|
||||||
let request = self
|
let request = self
|
||||||
.http
|
.http
|
||||||
.put(url)
|
.put(url)
|
||||||
.json(&request)
|
.json(object)
|
||||||
.header(reqwest::header::IF_MATCH, etag)
|
.header(reqwest::header::IF_MATCH, etag)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.invoke_api(request).await
|
self.invoke_api(request).await
|
||||||
}
|
}
|
||||||
|
pub(crate) async fn update_domain_object<E: DomainExtension>(
|
||||||
|
&self,
|
||||||
|
id: impl Display,
|
||||||
|
object: &E::UpdateRequest,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.update(E::DOMAIN_TYPE, id, object).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn delete<Q>(
|
||||||
|
&self,
|
||||||
|
domain_type: DomainType,
|
||||||
|
id: impl Display,
|
||||||
|
query: &Q,
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
Q: Serialize,
|
||||||
|
{
|
||||||
|
let url = self.object_url(domain_type, id);
|
||||||
|
let request = self
|
||||||
|
.http
|
||||||
|
.delete(url)
|
||||||
|
.query(&query)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
self.invoke_api(request).await
|
||||||
|
}
|
||||||
pub(crate) async fn delete_domain_object<E: DomainExtension>(
|
pub(crate) async fn delete_domain_object<E: DomainExtension>(
|
||||||
&self,
|
&self,
|
||||||
id: impl Display,
|
id: impl Display,
|
||||||
query: &E::DeleteQuery,
|
query: &E::DeleteQuery,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
self.delete(E::DOMAIN_TYPE, id, query).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn bulk_create<O, Q>(
|
||||||
|
&self,
|
||||||
|
domain_type: DomainType,
|
||||||
|
entries: &[O],
|
||||||
|
query: &Q,
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
O: Serialize,
|
||||||
|
Q: Serialize,
|
||||||
|
{
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Request<'a, E: Serialize> {
|
||||||
|
entries: &'a [E],
|
||||||
|
}
|
||||||
|
let url = self.bulk_action_url(domain_type, BulkAction::Create);
|
||||||
|
let body = Request::<O> { entries };
|
||||||
|
|
||||||
let request = self
|
let request = self
|
||||||
.http
|
.http
|
||||||
.delete(self.object_url(E::DOMAIN_TYPE, id))
|
.post(url)
|
||||||
.query(&query)
|
.json(&body)
|
||||||
|
.query(query)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.invoke_api(request).await
|
self.invoke_api(request).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn bulk_create_domain_objects<E: BulkCreateDomainExtension>(
|
pub(crate) async fn bulk_create_domain_objects<E: BulkCreateDomainExtension>(
|
||||||
&self,
|
&self,
|
||||||
entries: &[E::CreationRequest],
|
entries: &[E::CreationRequest],
|
||||||
query: &E::CreationQuery,
|
query: &E::CreationQuery,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
#[derive(Serialize)]
|
self.bulk_create(E::DOMAIN_TYPE, entries, query).await
|
||||||
struct Request<'a, D: BulkCreateDomainExtension> {
|
}
|
||||||
entries: &'a [D::CreationRequest],
|
|
||||||
}
|
|
||||||
|
|
||||||
let request = self
|
pub(crate) async fn bulk_read<O, Q>(
|
||||||
.http
|
&self,
|
||||||
.post(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Create))
|
domain_type: DomainType,
|
||||||
.json(&Request::<E> { entries })
|
query: &Q
|
||||||
|
) -> Result<Vec<DomainObject<O>>>
|
||||||
|
where
|
||||||
|
O: DeserializeOwned,
|
||||||
|
Q: Serialize
|
||||||
|
{
|
||||||
|
let url = self.collection_url(domain_type);
|
||||||
|
let request = self.http
|
||||||
|
.get(url)
|
||||||
.query(query)
|
.query(query)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.invoke_api(request).await
|
let response: DomainCollection<O> = self.query_api(request).await?;
|
||||||
|
Ok(response.value)
|
||||||
|
|
||||||
}
|
}
|
||||||
pub(crate) async fn bulk_read_domain_objects<E: BulkReadDomainExtension>(
|
pub(crate) async fn bulk_read_domain_objects<E: BulkReadDomainExtension>(
|
||||||
&self,
|
&self,
|
||||||
query: &E::BulkReadQuery,
|
query: &E::BulkReadQuery,
|
||||||
) -> Result<Vec<DomainObject<E>>> {
|
) -> Result<Vec<DomainObject<E>>> {
|
||||||
|
self.bulk_read(E::DOMAIN_TYPE, query).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn bulk_update<O>(
|
||||||
|
&self,
|
||||||
|
domain_type: DomainType,
|
||||||
|
entries: &[O],
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
O: Serialize,
|
||||||
|
{
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Request<'a, E: Serialize> {
|
||||||
|
entries: &'a [E],
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = self.bulk_action_url(domain_type, BulkAction::Update);
|
||||||
|
let body = Request::<O> { entries };
|
||||||
|
|
||||||
let request = self
|
let request = self
|
||||||
.http
|
.http
|
||||||
.get(self.collection_url(E::DOMAIN_TYPE))
|
.put(url)
|
||||||
.query(query)
|
.json(&body)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let response: DomainCollection<E> = self.query_api(request).await?;
|
self.invoke_api(request).await
|
||||||
Ok(response.value)
|
|
||||||
}
|
}
|
||||||
pub(crate) async fn bulk_update_domain_objects<E: BulkUpdateDomainExtension>(
|
pub(crate) async fn bulk_update_domain_objects<E: BulkUpdateDomainExtension>(
|
||||||
&self,
|
&self,
|
||||||
entries: &[E::BulkUpdateRequest],
|
entries: &[E::BulkUpdateRequest],
|
||||||
|
) -> Result<()> {
|
||||||
|
self.bulk_update(E::DOMAIN_TYPE, entries).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn bulk_delete(
|
||||||
|
&self,
|
||||||
|
domain_type: DomainType,
|
||||||
|
entries: &[String]
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Request<'a, D: BulkUpdateDomainExtension> {
|
struct Request<'a> {
|
||||||
entries: &'a [D::BulkUpdateRequest],
|
entries: &'a [String],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let url = self.bulk_action_url(domain_type, BulkAction::Delete);
|
||||||
|
let body = Request { entries };
|
||||||
|
|
||||||
let request = self
|
let request = self
|
||||||
.http
|
.http
|
||||||
.put(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Update))
|
.post(url)
|
||||||
.json(&Request::<E> { entries })
|
.json(&body)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -399,19 +519,7 @@ impl InnerClient {
|
|||||||
&self,
|
&self,
|
||||||
entries: &[String],
|
entries: &[String],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
#[derive(Serialize)]
|
self.bulk_delete(E::DOMAIN_TYPE, entries).await
|
||||||
struct Request<'a> {
|
|
||||||
entries: &'a [String],
|
|
||||||
}
|
|
||||||
|
|
||||||
let request = self
|
|
||||||
.http
|
|
||||||
.post(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Delete))
|
|
||||||
.json(&Request { entries })
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
self.invoke_api(request).await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user