feat: add metadata-agregator gRPC client integration
- Add tonic/prost for gRPC client generation - Add proto definitions from metadata-agregator service - Add MetadataClient and MetadataService for gRPC communication - Add REST controller exposing metadata endpoints (search, get, sync) - Update config with metadata.endpoint setting - Update flake.nix with protobuf for proto compilation
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::metadata::{MetadataClient, MetadataClientError};
|
||||
|
||||
pub struct MetadataService {
|
||||
client: Option<Arc<Mutex<MetadataClient>>>,
|
||||
endpoint: String,
|
||||
}
|
||||
|
||||
impl MetadataService {
|
||||
pub fn new(endpoint: &str) -> Self {
|
||||
Self {
|
||||
client: None,
|
||||
endpoint: endpoint.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn connect(&mut self) -> Result<(), MetadataClientError> {
|
||||
let client = MetadataClient::connect(&self.endpoint).await?;
|
||||
self.client = Some(Arc::new(Mutex::new(client)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_connected(&self) -> bool {
|
||||
self.client.is_some()
|
||||
}
|
||||
|
||||
fn client(&self) -> Result<Arc<Mutex<MetadataClient>>, MetadataClientError> {
|
||||
self.client
|
||||
.clone()
|
||||
.ok_or_else(|| MetadataClientError::ConnectionFailed("not connected".into()))
|
||||
}
|
||||
|
||||
pub async fn search_artists(
|
||||
&self,
|
||||
query: &str,
|
||||
limit: Option<i32>,
|
||||
offset: Option<i32>,
|
||||
) -> Result<crate::metadata::proto::SearchArtistsResponse, MetadataClientError> {
|
||||
let client = self.client()?;
|
||||
let mut guard = client.lock().await;
|
||||
guard.search_artists(query, limit, offset).await
|
||||
}
|
||||
|
||||
pub async fn get_artist(
|
||||
&self,
|
||||
id: &str,
|
||||
) -> Result<crate::metadata::proto::Artist, MetadataClientError> {
|
||||
let client = self.client()?;
|
||||
let mut guard = client.lock().await;
|
||||
guard.get_artist(id).await
|
||||
}
|
||||
|
||||
pub async fn get_artist_albums(
|
||||
&self,
|
||||
artist_id: &str,
|
||||
limit: Option<i32>,
|
||||
offset: Option<i32>,
|
||||
) -> Result<crate::metadata::proto::GetArtistAlbumsResponse, MetadataClientError> {
|
||||
let client = self.client()?;
|
||||
let mut guard = client.lock().await;
|
||||
guard.get_artist_albums(artist_id, limit, offset).await
|
||||
}
|
||||
|
||||
pub async fn get_album(
|
||||
&self,
|
||||
id: &str,
|
||||
) -> Result<crate::metadata::proto::Album, MetadataClientError> {
|
||||
let client = self.client()?;
|
||||
let mut guard = client.lock().await;
|
||||
guard.get_album(id).await
|
||||
}
|
||||
|
||||
pub async fn get_album_tracks(
|
||||
&self,
|
||||
album_id: &str,
|
||||
) -> Result<crate::metadata::proto::GetAlbumTracksResponse, MetadataClientError> {
|
||||
let client = self.client()?;
|
||||
let mut guard = client.lock().await;
|
||||
guard.get_album_tracks(album_id).await
|
||||
}
|
||||
|
||||
pub async fn sync_artist(
|
||||
&self,
|
||||
name: &str,
|
||||
) -> Result<crate::metadata::proto::SyncArtistResponse, MetadataClientError> {
|
||||
let client = self.client()?;
|
||||
let mut guard = client.lock().await;
|
||||
guard.sync_artist(name).await
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
mod indexer_service;
|
||||
mod metadata_service;
|
||||
mod torrent_service;
|
||||
|
||||
pub use indexer_service::{IndexerInfo, IndexerService};
|
||||
pub use metadata_service::MetadataService;
|
||||
pub use torrent_service::TorrentService;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
Reference in New Issue
Block a user