sender: receiver: basic reqwest implementation

This commit is contained in:
Patryk Hegenberg 2024-04-15 14:02:41 +02:00
parent 6a862648e7
commit f703d9517f
4 changed files with 56 additions and 20 deletions

View file

@ -2,7 +2,6 @@ use crate::http_server;
use crate::receiver;
use crate::sender;
use clap::{Parser, Subcommand};
// use reqwest::blocking::Client;
#[derive(Parser)]
#[command(version, about, long_about = None)]
@ -16,7 +15,7 @@ pub enum Commands {
Send {
/// Address of the relay server. Accepted formats are: 127.0.0.1:8080, [::1]:8080, example.com
#[arg(short, long)]
relay: bool,
relay: Option<String>,
/// Path to file(s)
#[arg(short, long, value_name = "FILE")]
file: Option<String>,
@ -25,7 +24,7 @@ pub enum Commands {
Receive {
/// Address of the relay server. Accepted formats are: 127.0.0.1:8080, [::1]:8080, example.com
#[arg(short, long)]
relay: bool,
relay: Option<String>,
/// Overwrite existing Files
#[arg(short, long)]
@ -38,7 +37,7 @@ pub enum Commands {
Serve {
/// Port to run the relay server on
#[arg(short, long)]
port: bool,
port: i32,
},
Config {
/// Show path to config file

View file

@ -51,4 +51,3 @@ pub async fn send_request(
}
Ok(())
}

View file

@ -1,13 +1,37 @@
use crate::http_client;
use serde::{Deserialize, Serialize};
// use std::collections::HashMap;
// use crate::http_client;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
#[derive(Debug, Serialize, Deserialize)]
struct TransferInfo {
ip: String,
name: String,
body: TransferInfoBody,
}
#[derive(Debug, Serialize, Deserialize)]
struct TransferInfoBody {
keyword: String,
files: String,
}
pub async fn download_info(filename: &str) -> Result<()> {
http_client::send_request(
format!("http://192.168.178.43:1323/download/{}", filename).trim(),
"GET",
None,
)
.await?;
match reqwest::get(format!("http://192.168.178.43:1323/download/{}", filename)).await {
Ok(resp) => {
let json = resp.json::<TransferInfo>().await?;
println!("Json Response: {:#?}", json);
}
Err(err) => {
println!("Error: {err}");
}
}
// http_client::send_request(
// format!("http://192.168.178.43:1323/download/{}", filename).trim(),
// "GET",
// None,
// )
// .await?;
Ok(())
}

View file

@ -1,4 +1,6 @@
use crate::http_client;
// use crate::http_client;
use reqwest::{Client, StatusCode};
use serde_json::Value;
use std::collections::HashMap;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
@ -8,14 +10,26 @@ pub async fn send_info(file: &str) -> Result<()> {
map.insert("keyword", "test");
map.insert("files", file);
let json_data = serde_json::to_string(&map)?;
// let json_data = serde_json::to_string(&map)?;
http_client::send_request(
"http://192.168.178.43:1323/upload".trim(),
"POST",
Some(json_data),
)
.await?;
let client = Client::new();
let res = client
.post("http://192.168.178.43:1323/upload")
.json(&map)
.send()
.await?;
if res.status() == StatusCode::CREATED {
let json: Value = res.json().await?;
println!("Json Response: {:#?}", json);
} else {
println!("Error reading response");
}
// http_client::send_request(
// "http://192.168.178.43:1323/upload".trim(),
// "POST",
// Some(json_data),
// )
// .await?;
Ok(())
}