2020-12-02 21:16:58 +00:00
|
|
|
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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:38:22 +00:00
|
|
|
#[instrument(skip(self), err)]
|
2020-12-02 21:16:58 +00:00
|
|
|
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?)
|
|
|
|
}
|
2021-01-17 02:38:22 +00:00
|
|
|
|
|
|
|
#[instrument(skip(self), err)]
|
|
|
|
pub async fn refresh(&self) -> Result<()> {
|
|
|
|
self.cli
|
|
|
|
.post("https://mi.within.website/api/blog/refresh")
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.error_for_status()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-02 21:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|