diff options
| author | Christine Dodrill <me@christine.website> | 2020-12-02 16:16:58 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-02 16:16:58 -0500 |
| commit | 233ea76204ea5bc9a7d8f12816a9525b7a732bc5 (patch) | |
| tree | de0ef2b37a6b8ee04f51eee3169ab29a472fa031 /lib/mi/src/lib.rs | |
| parent | d35f62351f800115e7f6aef7fd0791b9ac608229 (diff) | |
| download | xesite-233ea76204ea5bc9a7d8f12816a9525b7a732bc5.tar.xz xesite-233ea76204ea5bc9a7d8f12816a9525b7a732bc5.zip | |
add webmention support (#274)
* add webmention support
Signed-off-by: Christine Dodrill <me@christine.website>
* add webmention integration post
Signed-off-by: Christine Dodrill <me@christine.website>
Diffstat (limited to 'lib/mi/src/lib.rs')
| -rw-r--r-- | lib/mi/src/lib.rs | 63 |
1 files changed, 63 insertions, 0 deletions
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<Self> { + 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<Vec<WebMention>> { + 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<String>, +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} |
