Skip to content

Commit 0d4342e

Browse files
committed
Add hyper http client example
1 parent 5055441 commit 0d4342e

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ actix-utils = "1.0.3"
3232
awc = "*" # actix_web_client
3333
actix-codec = "*"
3434
futures = "*"
35-
toml = "*"
35+
toml = "*"
36+
hyper = "*"
37+
hyper-tls = "*"

examples/hyper_http_client.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use bytes::buf::BufExt;
2+
3+
#[cfg(debug_assertions)]
4+
const URL: &str = "https://jsonplaceholder.typicode.com/users/1";
5+
6+
#[tokio::test(core_threads = 1)]
7+
async fn simple_http_request() -> Result<(), Box<dyn std::error::Error>> {
8+
let res = hyper::Client::new()
9+
.get(URL.replace("https", "http").parse()?)
10+
.await?;
11+
let res_body = hyper::body::aggregate(res).await?;
12+
let res_json: serde_json::Value = serde_json::from_reader(res_body.reader())?;
13+
dbg!(res_json);
14+
Ok(())
15+
}
16+
17+
#[tokio::test(core_threads = 1)]
18+
async fn hyper_https_request() -> Result<(), Box<dyn std::error::Error>> {
19+
let https_client =
20+
hyper::Client::builder().build::<_, hyper::Body>(hyper_tls::HttpsConnector::new());
21+
let res = https_client.get(URL.parse()?).await?;
22+
let res_json: serde_json::Value =
23+
serde_json::from_reader(hyper::body::aggregate(res).await?.reader())?;
24+
dbg!(res_json);
25+
Ok(())
26+
}
27+
28+
fn main() {}

examples/macro_impl_dbg.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
macro_rules! my_dbg {
2+
($expr:expr) => {
3+
println!(
4+
"[{}:{}] {} = {:#?}",
5+
file!(),
6+
line!(),
7+
stringify!($expr),
8+
$expr
9+
);
10+
};
11+
}
12+
13+
fn main() {
14+
my_dbg!(1 + 1);
15+
dbg!(1 + 1);
16+
my_dbg!((1, 2));
17+
dbg!((1, 2));
18+
dbg!(1, 2, 3);
19+
// TODO my_dbg!(1, 2, 3);
20+
}

examples/read_toml_config_file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ struct MySQLConfig {
6161
#[derive(Deserialize)]
6262
struct DbName {
6363
production: String,
64-
test: Option<String>
64+
test: Option<String>,
6565
}
6666

6767
#[derive(Deserialize)]
6868
struct RedisClusterConfig {
6969
url: String,
70-
password: Option<String>
70+
password: Option<String>,
7171
}
7272

7373
fn main() -> Result<(), Box<dyn std::error::Error>> {

0 commit comments

Comments
 (0)