cleaner job send function
This commit is contained in:
@@ -5,3 +5,6 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
async-nats = { version = "0.46.0" }
|
||||
async-trait = { version = "0.1.89" }
|
||||
postcard = { version = "1.1.3", features = ["use-std"] }
|
||||
types = { path = "../../libs/types" }
|
||||
|
||||
@@ -1 +1,43 @@
|
||||
pub fn()
|
||||
use async_nats::Client;
|
||||
use async_trait::async_trait;
|
||||
use postcard::{from_bytes, to_stdvec};
|
||||
use types::jobs::{JobResponse, JobsMap, JobsResponseMap};
|
||||
|
||||
#[async_trait]
|
||||
pub trait JobClient {
|
||||
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl JobClient for Client {
|
||||
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, String> {
|
||||
let subject = match &job {
|
||||
JobsMap::Download(_job) => "download",
|
||||
JobsMap::Play(_job) => "play",
|
||||
JobsMap::Search(_job) => "search",
|
||||
};
|
||||
|
||||
match self
|
||||
.request(
|
||||
format!("corro-dj.{subject}"),
|
||||
to_stdvec(&job).unwrap().into(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
println!("{:?}", &response.payload);
|
||||
|
||||
let parsed: JobResponse = from_bytes(&response.payload).unwrap();
|
||||
|
||||
if let Some(error) = parsed.error {
|
||||
Err(error)
|
||||
} else if let Some(content) = parsed.content {
|
||||
Ok(content)
|
||||
} else {
|
||||
Err("Unknown content".to_string())
|
||||
}
|
||||
}
|
||||
Err(why) => Err(why.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-18
@@ -6,31 +6,22 @@ use uuid::Uuid;
|
||||
|
||||
use crate::queue::YoutubeSong;
|
||||
|
||||
|
||||
|
||||
|
||||
pub enum JobsBody {
|
||||
|
||||
}
|
||||
|
||||
impl Jobs {
|
||||
fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Search(self) => "Hello",
|
||||
Self::Download => "World",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub enum Jobs {
|
||||
pub enum JobsResponseMap {
|
||||
Search(SearchResponse),
|
||||
Download(DownloadResponse),
|
||||
Play(PlayResponse),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub enum JobsMap {
|
||||
Search(SearchJob),
|
||||
Download(DownloadJob),
|
||||
Play(PlayJob),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct JobResponse<T = Jobs> {
|
||||
pub struct JobResponse<T = JobsResponseMap> {
|
||||
pub content: Option<T>,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
@@ -44,6 +35,7 @@ pub struct DownloadJob {
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DownloadResponse {
|
||||
pub path: PathBuf,
|
||||
pub test: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
||||
Reference in New Issue
Block a user