diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 10d7311..fc55b41 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -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(&self, event_name: &str, data: T) -> ApiResult { + async fn emit( + &self, + event_name: &str, + data: T, + ) -> ApiResult { 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(&self, event_name: &str, data: T) -> ApiResult { + async fn emit_and_get< + T: EventSendPayload + Debug + Send, + R: EventReceivePayload + Debug + Send, + >( + &self, + event_name: &str, + data: T, + ) -> ApiResult { 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 { - 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::()?) } -} \ No newline at end of file +} diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs new file mode 100644 index 0000000..c01c591 --- /dev/null +++ b/mediarepo-api/src/client_api/tag.rs @@ -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> { + self.emit_and_get( + "tags_for_file", + GetFileTagsRequest { + id: FileIdentifier::Hash(hash), + }, + ) + .await + } +}