client: move request commands to own file
This commit is contained in:
parent
33596612fd
commit
1aba24bd5c
3 changed files with 61 additions and 49 deletions
|
|
@ -1,4 +1,7 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use reqwest::blocking::Client;
|
||||
|
||||
use crate::command;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
|
@ -33,6 +36,28 @@ impl Cli {
|
|||
pub fn new() -> Self {
|
||||
Self::parse()
|
||||
}
|
||||
pub fn handle_cli_args(&self, client: Client) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(config_path) = self.config.as_deref() {
|
||||
println!("Value for config: {}", config_path);
|
||||
}
|
||||
match self.debug {
|
||||
0 => println!("Debug mode is off"),
|
||||
1 => println!("Debug mode is kind of on"),
|
||||
2 => println!("Debug mode is on"),
|
||||
_ => println!("Don't be crazy"),
|
||||
}
|
||||
|
||||
match &self.command {
|
||||
Some(Commands::Send { file }) => {
|
||||
command::send_info(client, file.as_deref().unwrap_or("test.txt"))?;
|
||||
}
|
||||
None => {
|
||||
let filename = self.name.as_deref().unwrap_or("None");
|
||||
command::download_info(client, filename)?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Cli {
|
||||
|
|
|
|||
33
caesar_cli/src/command.rs
Normal file
33
caesar_cli/src/command.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use reqwest::{blocking::Client, StatusCode};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn send_info(client: Client, file: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("keyword", "test");
|
||||
map.insert("files", file);
|
||||
|
||||
let res = client
|
||||
.post("http://192.168.178.43:1323/upload")
|
||||
.json(&map)
|
||||
.send()?;
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
let json: HashMap<String, String> = res.json()?;
|
||||
println!("JSON Response: {:?}", json);
|
||||
} else {
|
||||
println!("Error: Failed to send request");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn download_info(client: Client, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let res = client
|
||||
.get(format!("http://192.168.178.43:1323/download/{}", filename))
|
||||
.send()?;
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
let json: HashMap<String, String> = res.json()?;
|
||||
println!("JSON Response: {:?}", json);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,60 +1,14 @@
|
|||
mod cli;
|
||||
mod command;
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::StatusCode;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub use crate::cli::*;
|
||||
|
||||
fn main() {
|
||||
let client = Client::new();
|
||||
let cli = cli::Cli::new();
|
||||
if let Some(config_path) = cli.config.as_deref() {
|
||||
println!("Value for config: {}", config_path);
|
||||
}
|
||||
|
||||
match cli.debug {
|
||||
0 => println!("Debug mode is off"),
|
||||
1 => println!("Debug mode is kind of on"),
|
||||
2 => println!("Debug mode is on"),
|
||||
_ => println!("Don't be crazy"),
|
||||
}
|
||||
|
||||
match &cli.command {
|
||||
Some(Commands::Send { file }) => {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("keyword", "test");
|
||||
let files = match file {
|
||||
Some(name) => name.trim(),
|
||||
None => "test.txt",
|
||||
};
|
||||
map.insert("files", files);
|
||||
let res = client
|
||||
.post("http://192.168.178.43:1323/upload")
|
||||
.json(&map)
|
||||
.send()
|
||||
.expect("Error sending request");
|
||||
if res.status() == StatusCode::OK {
|
||||
let json: HashMap<String, String> =
|
||||
res.json().expect("Error parsing JSON response");
|
||||
println!("JSON Response: {:?}", json);
|
||||
} else {
|
||||
println!("Error: Failed to send request");
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let filename = match cli.name {
|
||||
Some(name) => name,
|
||||
None => "None".to_string(),
|
||||
};
|
||||
let res = client
|
||||
.get(format!("http://192.168.178.43:1323/download/{}", filename))
|
||||
.send()
|
||||
.expect("Error sending request");
|
||||
if res.status() == StatusCode::OK {
|
||||
let json: HashMap<String, String> =
|
||||
res.json().expect("Error parsing JSON response");
|
||||
println!("Json Response: {:?}", json);
|
||||
}
|
||||
}
|
||||
if let Err(e) = cli.handle_cli_args(client) {
|
||||
eprintln!("Error: {e}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue