Add tag api

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 8b5af04075
commit f90580ea06

@ -1,29 +1,45 @@
pub mod error;
pub mod file;
pub mod tag;
use std::fmt::Debug;
use rmp_ipc::ipc::context::Context;
use rmp_ipc::IPCBuilder;
use crate::client_api::error::ApiResult;
use crate::client_api::file::FileApi;
use crate::client_api::tag::TagApi;
use crate::types::misc::InfoResponse;
use async_trait::async_trait;
use rmp_ipc::ipc::context::Context;
use rmp_ipc::ipc::stream_emitter::EmitMetadata;
use rmp_ipc::payload::{EventReceivePayload, EventSendPayload};
use rmp_ipc::IPCBuilder;
use std::fmt::Debug;
#[async_trait]
pub trait IPCApi {
fn namespace() -> &'static str;
fn ctx(&self) -> &Context;
async fn emit<T: EventSendPayload + Debug + Send>(&self, event_name: &str, data: T) -> ApiResult<EmitMetadata> {
async fn emit<T: EventSendPayload + Debug + Send>(
&self,
event_name: &str,
data: T,
) -> ApiResult<EmitMetadata> {
let ctx = self.ctx();
let meta = ctx.emitter.emit_to(Self::namespace(), event_name, data).await?;
let meta = ctx
.emitter
.emit_to(Self::namespace(), event_name, data)
.await?;
Ok(meta)
}
async fn emit_and_get<T: EventSendPayload + Debug + Send, R: EventReceivePayload + Debug + Send>(&self, event_name: &str, data: T) -> ApiResult<R> {
async fn emit_and_get<
T: EventSendPayload + Debug + Send,
R: EventReceivePayload + Debug + Send,
>(
&self,
event_name: &str,
data: T,
) -> ApiResult<R> {
let meta = self.emit(event_name, data).await?;
let response = meta.await_reply(self.ctx()).await?;
@ -35,6 +51,7 @@ pub trait IPCApi {
pub struct ApiClient {
ctx: Context,
pub file: FileApi,
pub tag: TagApi,
}
impl ApiClient {
@ -42,7 +59,8 @@ impl ApiClient {
pub fn new(ctx: Context) -> Self {
Self {
file: FileApi::new(ctx.clone()),
ctx
tag: TagApi::new(ctx.clone()),
ctx,
}
}
@ -54,9 +72,15 @@ impl ApiClient {
}
/// Returns information about the connected ipc server
#[tracing::instrument(level="debug", skip(self))]
#[tracing::instrument(level = "debug", skip(self))]
pub async fn info(&self) -> ApiResult<InfoResponse> {
let res = self.ctx.emitter.emit("info", ()).await?.await_reply(&self.ctx).await?;
let res = self
.ctx
.emitter
.emit("info", ())
.await?
.await_reply(&self.ctx)
.await?;
Ok(res.data::<InfoResponse>()?)
}
}
}

@ -0,0 +1,41 @@
use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi;
use crate::types::files::GetFileTagsRequest;
use crate::types::identifier::FileIdentifier;
use crate::types::tags::TagResponse;
use async_trait::async_trait;
use rmp_ipc::ipc::context::Context;
#[derive(Clone)]
pub struct TagApi {
ctx: Context,
}
#[async_trait]
impl IPCApi for TagApi {
fn namespace() -> &'static str {
"tags"
}
fn ctx(&self) -> &Context {
&self.ctx
}
}
impl TagApi {
pub fn new(ctx: Context) -> Self {
Self { ctx }
}
/// Returns a list of all tags for a file
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_tags_for_file(&self, hash: String) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get(
"tags_for_file",
GetFileTagsRequest {
id: FileIdentifier::Hash(hash),
},
)
.await
}
}
Loading…
Cancel
Save