From 233ea76204ea5bc9a7d8f12816a9525b7a732bc5 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 2 Dec 2020 16:16:58 -0500 Subject: add webmention support (#274) * add webmention support Signed-off-by: Christine Dodrill * add webmention integration post Signed-off-by: Christine Dodrill --- lib/mi/Cargo.toml | 22 +++++++++++++++++++ lib/mi/src/lib.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 lib/mi/Cargo.toml create mode 100644 lib/mi/src/lib.rs (limited to 'lib/mi') diff --git a/lib/mi/Cargo.toml b/lib/mi/Cargo.toml new file mode 100644 index 0000000..1d0e716 --- /dev/null +++ b/lib/mi/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "mi" +version = "0.1.0" +authors = ["Christine Dodrill "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = { version = "0.4", features = ["serde"] } +color-eyre = "0.5" +reqwest = { version = "0.10", features = ["json"] } +serde_json = "1.0" +serde = { version = "1", features = ["derive"] } +thiserror = "1" +tracing = "0.1" +tracing-futures = "0.2" + +[dev-dependencies] +tokio = { version = "0.2", features = ["macros"] } +envy = "0.4" +pretty_env_logger = "0" diff --git a/lib/mi/src/lib.rs b/lib/mi/src/lib.rs new file mode 100644 index 0000000..ec9d459 --- /dev/null +++ b/lib/mi/src/lib.rs @@ -0,0 +1,63 @@ +use color_eyre::eyre::Result; +use reqwest::header; +use serde::Deserialize; +use tracing::instrument; + +const USER_AGENT_BASE: &str = concat!( + "library/", + env!("CARGO_PKG_NAME"), + "/", + env!("CARGO_PKG_VERSION") +); + +pub struct Client { + cli: reqwest::Client, + base_url: String, +} + +impl Client { + pub fn new(token: String, user_agent: String) -> Result { + let mut headers = header::HeaderMap::new(); + headers.insert( + header::AUTHORIZATION, + header::HeaderValue::from_str(&token.clone())?, + ); + + let cli = reqwest::Client::builder() + .user_agent(&format!("{} {}", user_agent, USER_AGENT_BASE)) + .default_headers(headers) + .build()?; + + Ok(Self { + cli: cli, + base_url: "https://mi.within.website".to_string(), + }) + } + + #[instrument(skip(self))] + pub async fn mentioners(&self, url: String) -> Result> { + Ok(self + .cli + .get(&format!("{}/api/webmention/for", self.base_url)) + .query(&[("target", &url)]) + .send() + .await? + .error_for_status()? + .json() + .await?) + } +} + +#[derive(Debug, Deserialize, Eq, PartialEq, Clone)] +pub struct WebMention { + pub source: String, + pub title: Option, +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} -- cgit v1.2.3