Skip to content

Commit

Permalink
added clap cargo 0.0.3
Browse files Browse the repository at this point in the history
rebuild the hole package in with clap and reqwest instead of doing everything  manually
  • Loading branch information
Mooninghnk committed Oct 22, 2023
1 parent 6355c27 commit cdcaa09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

clap = { version = "4.4.6", features = ["derive"] }
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }

67 changes: 29 additions & 38 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
use reqwest;
use serde::{Deserialize, Serialize};
use std::{env, str::FromStr};
use clap::Parser;
use reqwest::{self, Response};
// figure out how the option args syntax works
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// url address of the api
#[arg(short)]
link: Option<String>,
/// request type
#[arg(short, default_value_t = String::new())]
get: String,
/// count to run
#[arg(short, default_value_t = 1)]
count: u8,
}

// add extra function for handleing optional input POST/GET
fn main() {
println!("enter a url to fetch");
let mut args: Vec<String> = env::args().collect();
args.remove(0);
let slit: Vec<&str> = args[2].split(":").collect();
let jsn = format!(r#"{{"{}": "{}"}}"#, slit[0], slit[1]);

println!("{:?}", jsn);
let args = Args::parse();

//checks for get keyword in os args if matched run the corisponding function
if args[0] == "get" {
let res = fetch_get(args[1].as_str());
match res {
Ok(x) => println!("{:?}", x),
Err(err) => println!("{:?}", err),
}
//checks for post keyword in os args if matched runs the coresponding fucntioin
} else if args[0] == "post" {
let res = fetch_post(args[1].as_str(), String::new());
match res {
Ok(x) => println!("{:?}", x),
Err(err) => println!("{:?}", err),
for _ in 0..args.count {
let cmp = args.link.as_deref().unwrap();
println!("{:?}", cmp);
if args.get.contains("get") || args.get.contains("Get") {
let resp = get_req(cmp);
println!("{:?}", resp);
}
}
}

// add support for error handleing and fix the return type
#[tokio::main]
async fn fetch_get(url: &str) -> Result<String, reqwest::Error> {
async fn get_req(url: &str) -> Result<String, reqwest::Error> {
let body = reqwest::get(url).await?.text().await?;
Ok(body)
}

//seperated post function to handle post requests
#[tokio::main]
async fn fetch_post(url: &str, bd: String) -> Result<reqwest::Response, reqwest::Error> {
let client = reqwest::Client::new();
let res = client.post(url).body(bd).send().await?;
Ok(res)
}

//* wrtie a parser to parse the 3rd part of the args witch is going to be be parsed streight into json
//so we can actaully make a real post request with sending out data
//automaticly parse the data and call the json on body
//add post support with json
//transfer 25 into a fn
//update the instruction on usage
//lines 8 11 14

0 comments on commit cdcaa09

Please sign in to comment.