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/src/lib.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lib/mi/src/lib.rs (limited to 'lib/mi/src') 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