-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rebuild the hole package in with clap and reqwest instead of doing everything manually
- Loading branch information
1 parent
6355c27
commit cdcaa09
Showing
2 changed files
with
30 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |