Skip to content

Commit

Permalink
feat: define get receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiaojiu committed Sep 24, 2024
1 parent e75dce1 commit dd040e8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
1 change: 0 additions & 1 deletion kimika/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod config;
mod receive;
mod request;
mod send;
mod utils;

Expand Down
Empty file removed kimika/src/request/data.rs
Empty file.
1 change: 0 additions & 1 deletion kimika/src/request/mod.rs

This file was deleted.

27 changes: 27 additions & 0 deletions kimika/src/send/remote_http.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::SendArgs;
use crate::utils::request::get_receivers;
use crate::{config, utils::handle, utils::select};
use crossterm::style::Stylize;
use std::{fs, path::PathBuf};
use tokio::{sync::mpsc, time};

pub struct Content {
pub message: Option<String>,
Expand Down Expand Up @@ -52,5 +54,30 @@ pub async fn remote_send(
return Ok(());
};

let (tx, mut rx) = mpsc::channel::<Vec<select::SelectItem<String>>>(1);

#[warn(while_true)]
loop {
let res = get_receivers(&address).await.expect("");
let receiver_iter = res.receivers.iter().map(|receiver| select::SelectItem {
id: receiver.id.clone(),
label: receiver.alias.clone(),
});
let result = tx.send(receiver_iter.collect()).await;
if result.is_err() {
break;
}
time::sleep(time::Duration::from_secs(1)).await;
}

let selected_receiver_id = if let Some(id) = select::receiver_select(&mut rx)
.await
.expect("select receiver failed")
{
id
} else {
return Ok(());
};

Ok(())
}
1 change: 1 addition & 0 deletions kimika/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod clap;
pub mod crossterm;
pub mod handle;
pub mod request;
pub mod select;
pub mod udp;
25 changes: 25 additions & 0 deletions kimika/src/utils/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use reqwest::{Client, Url};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[derive(Deserialize)]
pub struct Receiver {
pub id: String,
pub alias: String,
}

fn into_url(address: &SocketAddr) -> Url {
let url_string = format!("http://{}", address);
Url::parse(&url_string).expect("invalid address")
}

#[derive(Deserialize)]
pub struct GetReceiversRes {
pub receivers: Vec<Receiver>,
message: String,
}

pub async fn get_receivers(address: &SocketAddr) -> Result<GetReceiversRes, reqwest::Error> {
let result = Client::new().get(into_url(address)).send().await?;
Ok(result.json().await.unwrap())
}

0 comments on commit dd040e8

Please sign in to comment.