aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2020-12-02 16:16:58 -0500
committerGitHub <noreply@github.com>2020-12-02 16:16:58 -0500
commit233ea76204ea5bc9a7d8f12816a9525b7a732bc5 (patch)
treede0ef2b37a6b8ee04f51eee3169ab29a472fa031 /lib
parentd35f62351f800115e7f6aef7fd0791b9ac608229 (diff)
downloadxesite-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')
-rw-r--r--lib/mi/Cargo.toml22
-rw-r--r--lib/mi/src/lib.rs63
2 files changed, 85 insertions, 0 deletions
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 <me@christine.website>"]
+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<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);
+ }
+}