Last active
February 9, 2024 01:34
-
-
Save malik672/fa29a74a527a87620ad1042a2579168d to your computer and use it in GitHub Desktop.
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
// Import the crates | |
use clap::{App, Arg, SubCommand}; | |
use reqwest::Client; | |
#[tokio::main] | |
async fn main() { | |
// Create a CLI app with clap | |
let app = App::new("Rick and Morty CLI") | |
.version("0.1.0") | |
.author("Your Name <[email protected]>") | |
.about("A CLI application to consume the Rick and Morty API") | |
.subcommand( | |
SubCommand::with_name("character") | |
.about("Get information about a character by ID") | |
.arg( | |
Arg::with_name("id") | |
.help("The ID of the character") | |
.required(true) | |
.index(1), | |
), | |
) | |
.subcommand( | |
SubCommand::with_name("episode") | |
.about("Get information about an episode by ID") | |
.arg( | |
Arg::with_name("id") | |
.help("The ID of the episode") | |
.required(true) | |
.index(1), | |
), | |
) | |
.subcommand( | |
SubCommand::with_name("location") | |
.about("Get information about a location by ID") | |
.arg( | |
Arg::with_name("id") | |
.help("The ID of the location") | |
.required(true) | |
.index(1), | |
), | |
) | |
.subcommand( | |
SubCommand::with_name("proxy") | |
.about("Spin up a proxy server to cache the API results") | |
.arg( | |
Arg::with_name("port") | |
.help("The port to listen on") | |
.default_value("8000") | |
.index(1), | |
), | |
); | |
// Parse the command-line arguments | |
let matches = app.get_matches(); | |
// Create a HTTP client with reqwest | |
let client = Client::new(); | |
match matches.subcommand() { | |
Some(("character", sub_matches)) => { | |
// Get the character ID from the argument | |
let id = sub_matches.value_of("id").unwrap(); | |
// Build the URL for the API request | |
let url = format!("https://rickandmortyapi.com/api/character/{}", id); | |
// Make a GET request to the API and get the response | |
let response = client.get(&url).send().await.unwrap(); | |
// Check if the response is successful | |
if response.status().is_success() { | |
// Parse the response body as a JSON value | |
let json = response.text().await.unwrap(); | |
// Print the JSON value | |
println!("{}", json); | |
} else { | |
// Print the status code and the error message | |
println!( | |
"Error: {} {}", | |
response.status(), | |
response.text().await.unwrap() | |
); | |
} | |
} | |
Some(("episode", sub_matches)) => { | |
// Get the episode ID from the argument | |
let id = sub_matches.value_of("id").unwrap(); | |
// Build the URL for the API request | |
let url = format!("https://rickandmortyapi.com/api/episode/{}", id); | |
// Make a GET request to the API and get the response | |
let response = client.get(&url).send().await.unwrap(); | |
// Check if the response is successful | |
if response.status().is_success() { | |
// Parse the response body as a JSON value | |
let json = response.text().await.unwrap(); | |
// Print the JSON value | |
println!("{}", json); | |
} else { | |
// Print the status code and the error message | |
println!( | |
"Error: {} {}", | |
response.status(), | |
response.text().await.unwrap() | |
); | |
} | |
} | |
Some(("location", sub_matches)) => { | |
// Get the episode ID from the argument | |
let id = sub_matches.value_of("id").unwrap(); | |
// Build the URL for the API request | |
let url = format!("https://rickandmortyapi.com/api/location/{}", id); | |
// Make a GET request to the API and get the response | |
let response = client.get(&url).send().await.unwrap(); | |
// Check if the response is successful | |
if response.status().is_success() { | |
// Parse the response body as a JSON value | |
let json = response.text().await.unwrap(); | |
// Print the JSON value | |
println!("{}", json); | |
} else { | |
// Print the status code and the error message | |
println!( | |
"Error: {} {}", | |
response.status(), | |
response.text().await.unwrap() | |
); | |
} | |
} | |
_ => { | |
// Handle the case where no subcommand was provided or an unknown subcommand was used | |
eprintln!("No subcommand provided or an unknown subcommand was used."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment