Last active
February 23, 2023 06:06
-
-
Save yanyaoer/e3a2f11ab224aa12bb0c554e65f0423c to your computer and use it in GitHub Desktop.
httpbin_rs
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
// cargo add serde --features derive | |
// cargo add serde_json | |
// cargo add reqwest --features json | |
// cargo add tokio --features full | |
pub struct Httpbin { | |
client: reqwest::Client, | |
token: String, | |
data: Option<serde_json::Value>, | |
} | |
impl Httpbin { | |
pub fn new(token: &str) -> Self { | |
let client = reqwest::Client::new(); | |
Self { | |
client, | |
token: token.to_string(), | |
data: None, | |
} | |
} | |
pub async fn req(&mut self, method: &str) -> Result<(), reqwest::Error> { | |
let res = self.client.post("http://httpbin.org/post") | |
.header("Authorization", format!("Bearer {}", self.token)) | |
.header("X-client", "reqwest-rs") | |
.header("Content-Type", "application/json") | |
.json(&serde_json::json!({"method": method, "lifecycle": "always"})) | |
.send() | |
.await? | |
.json::<serde_json::Value>() | |
.await?; | |
self.data = Some(res); | |
Ok(()) | |
} | |
pub fn output(&mut self) { | |
println!("body = {:#?}", &self.data); | |
} | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), reqwest::Error> { | |
let mut cli = Httpbin::new("734C30FD-A6CF-4784-8209-023A298CF877"); | |
cli.req("logseq.Editor.getCurrentPage").await?; | |
cli.output(); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment