-
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.
- Loading branch information
1 parent
5654906
commit 37545e9
Showing
4 changed files
with
96 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
[package] | ||
name = "rust-api-test" | ||
description = "CLI for testing REST APIs" | ||
description = "CLI tool for testing REST APIs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT" | ||
authors = ["Prasad Jivane <prasadjivane@gmail.com>"] | ||
repository = "https://github.com/prasadjivane/rust-api-test" | ||
|
||
[dependencies] | ||
reqwest = "0.11" | ||
reqwest = { version = "0.11", features = ["blocking", "json"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
|
||
[dependencies.tokio] | ||
version = "1.0" | ||
features = ["full"] | ||
tokio = { version = "1.0", features = ["full"] } |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use reqwest::{Client, Result}; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
|
||
pub async fn get(url: &str) -> Result<Value> { | ||
let response = Client::new().get(url).send().await?; | ||
let json = response.json().await?; | ||
Ok(json) | ||
} | ||
|
||
pub async fn post(url: &str, body: HashMap<&str, &str>) -> Result<Value> { | ||
let response = Client::new().post(url).json(&body).send().await?; | ||
let json = response.json().await?; | ||
Ok(json) | ||
} | ||
|
||
pub async fn put(url: &str, body: HashMap<&str, &str>) -> Result<Value> { | ||
let response = Client::new().put(url).json(&body).send().await?; | ||
let json = response.json().await?; | ||
Ok(json) | ||
} | ||
|
||
pub async fn delete(url: &str) -> Result<Value> { | ||
let response = Client::new().delete(url).send().await?; | ||
let json = response.json().await?; | ||
Ok(json) | ||
} |
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,124 +1,41 @@ | ||
use std::io; | ||
use reqwest::{Client, Response}; | ||
use serde::{Deserialize, Serialize}; | ||
mod api; | ||
use std::collections::HashMap; | ||
use serde_json::json; | ||
use std::env; | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() < 3 { | ||
println!("Usage: {} <method> <url> [body]", args[0]); | ||
return; | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct ApiResponse { | ||
// Define the structure of the API response data | ||
// This can vary depending on your API's response format | ||
// For simplicity, let's assume it's JSON | ||
} | ||
|
||
async fn get(url: &str) -> Result<Response, reqwest::Error> { | ||
// Implement GET method | ||
Client::new().get(url).send().await | ||
} | ||
|
||
async fn post(url: &str, body: HashMap<&str, &str>) -> Result<Response, reqwest::Error> { | ||
// Serialize the body to JSON | ||
let body_json = json!(body); | ||
|
||
// Send POST request with JSON body | ||
Client::new() | ||
.post(url) | ||
.header("Content-Type", "application/json") | ||
.body(body_json.to_string()) | ||
.send() | ||
.await | ||
} | ||
|
||
async fn put(url: &str, body: HashMap<&str, &str>) -> Result<Response, reqwest::Error> { | ||
// Serialize the body to JSON | ||
let body_json = json!(body); | ||
|
||
// Send PUT request with JSON body | ||
Client::new() | ||
.put(url) | ||
.header("Content-Type", "application/json") | ||
.body(body_json.to_string()) | ||
.send() | ||
.await | ||
} | ||
|
||
async fn delete(url: &str) -> Result<Response, reqwest::Error> { | ||
// Implement DELETE method | ||
Client::new().delete(url).send().await | ||
} | ||
|
||
async fn handle_response(response: Response) -> Result<(), reqwest::Error> { | ||
// Process and display the response | ||
let status = response.status(); | ||
let body = response.text().await?; | ||
|
||
println!("Status: {}", status); | ||
println!("Body: {}", body); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
println!("REST CLI Tester"); | ||
|
||
loop { | ||
println!("Enter your choice:"); | ||
println!("1. GET"); | ||
println!("2. POST"); | ||
println!("3. PUT"); | ||
println!("4. DELETE"); | ||
println!("5. Exit"); | ||
|
||
let mut choice = String::new(); | ||
io::stdin().read_line(&mut choice)?; | ||
|
||
match choice.trim().parse() { | ||
Ok(1) => { | ||
println!("Enter URL:"); | ||
let mut url = String::new(); | ||
io::stdin().read_line(&mut url)?; | ||
let response = get(&url.trim()).await?; | ||
handle_response(response).await?; | ||
} | ||
Ok(2) => { | ||
println!("Enter URL:"); | ||
let mut url = String::new(); | ||
io::stdin().read_line(&mut url)?; | ||
|
||
println!("Enter JSON Body:"); | ||
let mut body_str = String::new(); | ||
io::stdin().read_line(&mut body_str)?; | ||
|
||
let body: HashMap<&str, &str> = serde_json::from_str(&body_str)?; | ||
let response = post(&url.trim(), body).await?; | ||
handle_response(response).await?; | ||
} | ||
Ok(3) => { | ||
println!("Enter URL:"); | ||
let mut url = String::new(); | ||
io::stdin().read_line(&mut url)?; | ||
|
||
println!("Enter JSON Body:"); | ||
let mut body_str = String::new(); | ||
io::stdin().read_line(&mut body_str)?; | ||
|
||
let body: HashMap<&str, &str> = serde_json::from_str(&body_str)?; | ||
let response = put(&url.trim(), body).await?; | ||
handle_response(response).await?; | ||
} | ||
Ok(4) => { | ||
println!("Enter URL:"); | ||
let mut url = String::new(); | ||
io::stdin().read_line(&mut url)?; | ||
let response = delete(&url.trim()).await?; | ||
handle_response(response).await?; | ||
} | ||
Ok(5) => break, | ||
_ => println!("Invalid choice!"), | ||
let method = &args[1]; | ||
let url = &args[2]; | ||
|
||
let body: HashMap<&str, &str> = if args.len() > 3 { | ||
args[3..].iter() | ||
.map(|s| { | ||
let mut parts = s.split('='); | ||
(parts.next().unwrap(), parts.next().unwrap_or("")) | ||
}) | ||
.collect() | ||
} else { | ||
HashMap::new() | ||
}; | ||
|
||
let result = match method.as_str() { | ||
"GET" => tokio::runtime::Runtime::new().unwrap().block_on(api::get(url)), | ||
"POST" => tokio::runtime::Runtime::new().unwrap().block_on(api::post(url, body)), | ||
"PUT" => tokio::runtime::Runtime::new().unwrap().block_on(api::put(url, body)), | ||
"DELETE" => tokio::runtime::Runtime::new().unwrap().block_on(api::delete(url)), | ||
_ => { | ||
println!("Invalid method!"); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
Ok(()) | ||
} | ||
match result { | ||
Ok(response) => println!("{:#?}", response), | ||
Err(e) => println!("Error: {}", e), | ||
} | ||
} |