Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use reqwest::header::CONTENT_TYPE;
use serde::{Serialize, Deserialize};
use std::fmt;
use clap::{Parser};
use std::thread::{spawn, JoinHandle};

/// Defines the Ambi Mock Client command line interface as a struct
#[derive(Parser, Debug)]
Expand All @@ -27,6 +28,10 @@ pub struct Cli {
/// Turns verbose console debug output on
#[clap(short, long)]
pub debug: bool,

// Make int number of concurrent requests
#[clap(short, long, default_value_t = 1)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an upper limit to how many concurrent requests we want to allow for? Perhaps adding CLI interface data check on that would be in order. For example, do we really want to allow for 65k simultaneous threads, or at least without having tested it well? :)

pub int: u16
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -110,39 +115,34 @@ fn random_gen_dust_concentration() -> String {
rng.gen_range(0..=1000).to_string()
}

pub fn run(cli: &Cli) {
println!("\r\ncli: {:?}\r\n", cli);

fn send_request(url: &str, client: Client, debug: bool) {
let dust_concentration = random_gen_dust_concentration();
let air_purity = AirPurity::from_value(dust_concentration.parse::<u16>().unwrap()).to_string();
let reading = Reading::new(
random_gen_temperature(),
random_gen_humidity(),
random_gen_pressure(),
dust_concentration,
air_purity
air_purity,
);

let json = serde_json::to_string(&reading).unwrap();
const URL: &str = "http://localhost:4000/api/readings/add";

println!("Sending POST request to {} as JSON: {}", URL, json);

let client = Client::new();
println!("Sending POST request to {} as JSON: {}", url, json);
let res = client
.post(URL)
.post(url)
.header(CONTENT_TYPE, "application/json")
.body(json)
.send();
match res {
Ok(response) => {
match cli.debug {
true => println!("Response from Ambi backend: {:#?}", response),
false => println!("Response from Ambi backend: {:?}", response.status().as_str())
}
}
Ok(response) => match debug {
true => println!("Response from Ambi backend: {:#?}", response),
false => println!(
"Response from Ambi backend: {:?}",
response.status().as_str()
),
},
Err(e) => {
match cli.debug {
match debug {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, this can be an if statement instead of a pattern matching statement

// Print out the entire reqwest::Error for verbose debugging
true => eprintln!("Response error from Ambi backend: {:?}", e),
// Keep the error reports more succinct
Expand All @@ -160,6 +160,23 @@ pub fn run(cli: &Cli) {
}
}

pub fn run(cli: &Cli) {
println!("\r\ncli: {:?}\r\n", cli);

const URL: &str = "http://localhost:4000/api/readings/add";
let mut handlers: Vec<JoinHandle<()>> = vec![];
let debug: bool = cli.debug;
for _ in 0..cli.int {
let handler = spawn(move || send_request(URL, Client::new(), debug));

handlers.push(handler);
}

for handler in handlers {
handler.join().unwrap();
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down