Skip to content

Commit

Permalink
build(deps): bump project dependencies (#62)
Browse files Browse the repository at this point in the history
- [x] update cargo dependencies;
  • Loading branch information
rfprod authored Mar 15, 2024
1 parent b341715 commit 1288be8
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 32 deletions.
179 changes: 159 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
[package]
name = "rust-workspace"
version = "0.8.1"
version = "0.8.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
colored = "2.1.0"
hyper = { version = "0.14", features = ["full"] }
hyper = { version = "1.2.0", features = ["full"] }
hyper-util = { version = "0.1.3", features = ["full"] }
http-body-util = "0.1.1"
tokio = { version = "1.36.0", features = ["full"] }
meval = "0.2"
rand = "0.8.5"
sysinfo = "0.30.6"
sysinfo = "0.30.7"
# ML Packages for Model Training
linfa = "0.7.0"
linfa-logistic = { version = "0.7.0", features = ["serde"] }
Expand Down
46 changes: 37 additions & 9 deletions src/open_weather/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Open weather module.
use colored::Colorize;
use hyper::{body::Buf, Client, Uri};
use http_body_util::{BodyExt, Empty};
use hyper::{body::Bytes, Request, Uri};
use hyper_util::rt::TokioIo;
use std::{
env::args,
io::{self, Write},
};
use tokio::net::TcpStream;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

Expand Down Expand Up @@ -139,9 +142,7 @@ impl OpenWeather {

/// Weather data request logic.
async fn weather_request(&mut self, city: &str, api_key: &str) -> Result<()> {
let client = Client::new();

let mut uri_with_params = String::from("http:///api.openweathermap.org/data/2.5/weather");
let mut uri_with_params = String::from("http://api.openweathermap.org/data/2.5/weather");
uri_with_params.push_str("?q=");
uri_with_params.push_str(city);
uri_with_params.push_str("&appid=");
Expand All @@ -151,13 +152,40 @@ impl OpenWeather {

let uri = uri_with_params.as_str().parse::<Uri>()?;

let res = client.get(uri).await?;
let host = uri.host().expect("uri has no host");
let port = uri.port_u16().unwrap_or(80);
let addr = format!("{}:{}", host, port);
let stream = TcpStream::connect(addr).await?;
let io = TokioIo::new(stream);

let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
tokio::task::spawn(async move {
if let Err(err) = conn.await {
println!("Connection failed: {:?}", err);
}
});

let authority = uri.authority().unwrap().clone();

println!("{}: {}", "Response".green(), res.status());
println!("{}: {:#?}\n", "Headers".green(), res.headers());
let path = uri.path();
let req = Request::builder()
.uri(path)
.header(hyper::header::HOST, authority.as_str())
.body(Empty::<Bytes>::new())?;

let body = hyper::body::aggregate(res).await?;
io::stdout().write_all(body.chunk())?;
let mut res = sender.send_request(req).await?;

println!("Response: {}", res.status());
println!("Headers: {:#?}\n", res.headers());

// Stream the body, writing each chunk to stdout as we get it
// (instead of buffering and printing at the end).
while let Some(next) = res.frame().await {
let frame = next?;
if let Some(chunk) = frame.data_ref() {
io::stdout().write_all(chunk)?;
}
}

println!("\n\n{}", "Done!".green().bold());

Expand Down

0 comments on commit 1288be8

Please sign in to comment.