From 8aba71a72b14d10dc9193257d256873b7ef7a40b Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 4 Jun 2023 14:09:25 +0800 Subject: [PATCH] feat: update fetch-rs --- __network/fetch-rs/README.md | 42 +++++++++++++++++++++---------- __network/fetch-rs/src/main.rs | 45 +++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/__network/fetch-rs/README.md b/__network/fetch-rs/README.md index 48914a7..d1216cc 100644 --- a/__network/fetch-rs/README.md +++ b/__network/fetch-rs/README.md @@ -1,18 +1,34 @@ -```js -const response = fetch("http://example.com/movies.json") -const responseJson = response.json(); +```rust +let response = fetch( + "https://hatter.ink/util/print_request.action", + &FetchOptions::default())?; ``` -```js -const response = fetch(url, { - method: "POST", // *GET, POST, PUT, DELETE, etc. - headers: { - "Content-Type": "application/json", - // 'Content-Type': 'application/x-www-form-urlencoded', - }, - redirect: "follow", // manual, *follow, error - body: JSON.stringify(data), // body data type must match "Content-Type" header -}); +```rust +let response = fetch( + "https://hatter.ink/util/print_request.action", + &FetchOptions { + method: FetchMethod::Put, + body: Some("{}".as_bytes().to_vec()), + ..Default::default() + })?; ``` + + +```rust +let response = fetch( + "https://hatter.ink/util/print_request.action", + &FetchOptions { + method: FetchMethod::Post, + headers: Some(vec![ + FetchHeader { + key: "content-type".to_string(), + value: "application/json".to_string(), + } + ]), + body: Some("{}".as_bytes().to_vec()), + ..Default::default() + })?; +``` \ No newline at end of file diff --git a/__network/fetch-rs/src/main.rs b/__network/fetch-rs/src/main.rs index 327bc6e..9658850 100644 --- a/__network/fetch-rs/src/main.rs +++ b/__network/fetch-rs/src/main.rs @@ -1,12 +1,29 @@ use std::str::FromStr; use std::time::Duration; -use reqwest::{Method, Url}; +use reqwest::{Method, Proxy, Url}; use reqwest::blocking::{Body, Client, Request}; use reqwest::header::{HeaderName, HeaderValue}; use rust_util::{simple_error, XResult}; fn main() -> XResult<()> { + println!("TEST1: GET"); + let response = fetch( + "https://hatter.ink/util/print_request.action", + &FetchOptions::default())?; + print_response(&response)?; + + println!("TEST2: PUT"); + let response = fetch( + "https://hatter.ink/util/print_request.action", + &FetchOptions { + method: FetchMethod::Put, + body: Some("{}".as_bytes().to_vec()), + ..Default::default() + })?; + print_response(&response)?; + + println!("TEST3: POST"); let response = fetch( "https://hatter.ink/util/print_request.action", &FetchOptions { @@ -20,9 +37,15 @@ fn main() -> XResult<()> { body: Some("{}".as_bytes().to_vec()), ..Default::default() })?; + print_response(&response)?; + + Ok(()) +} + +fn print_response(response: &FetchResponse) -> XResult<()> { println!("Status: {}", response.status); - response.headers.iter().for_each(|h| { - println!("- {}: {}", h.key, h.value); + response.headers.iter().for_each(|_h| { + // println!("- {}: {}", h.key, h.value); }); println!("----- BEGIN RESPONSE BODY -----\n{}\n----- END RESPONSE BODY -----", String::from_utf8(response.body().clone().unwrap().to_vec())?); @@ -62,10 +85,11 @@ pub struct FetchOptions { pub method: FetchMethod, pub headers: Option>, pub body: Option>, - // connect timeout or read timeout? + // FIXME connect timeout or read timeout? pub timeout: Option, - // proxy supports - // hsts supports + pub proxy: Option, + // TODO hsts supports + // TODO follow redirects? } impl FetchOptions {} @@ -96,9 +120,12 @@ impl FetchResponse { } pub fn fetch(url: &str, option: &FetchOptions) -> XResult { - let client = Client::builder() - // .proxy() - .build()?; + let mut client_builder = Client::builder(); + if let Some(proxy) = &option.proxy { + client_builder = client_builder.proxy(Proxy::all(proxy.as_str())?); + } + // TODO client_builder.add_root_certificate() + let client = client_builder.build()?; let method = match option.method { FetchMethod::Get => Method::GET, FetchMethod::Post => Method::POST,