1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
pub mod indieauth;
pub mod link_rel;
pub mod webmention;

#[cfg(test)]
mod test;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("No {rel:?} endpoints were found at {url:?}")]
    NoEndpointsFound { rel: String, url: String },

    #[error(
        "The Webmention endpoint at {url:?} returned a unexpected status code of {status_code:?})"
    )]
    WebmentionUnsupportedStatusCode {
        status_code: reqwest::StatusCode,
        url: String,
    },

    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        std::mem::discriminant(self) == std::mem::discriminant(other)
    }
}

static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "-rs/", env!("CARGO_PKG_VERSION"),);

/// Represents the base client interface for IndieWeb activity.
pub trait IConfiguration {
    /// Define the base request.
    fn request(&self, reason: Option<String>) -> reqwest::Client {
        let agent = if APP_USER_AGENT == self.user_agent() {
            APP_USER_AGENT.to_owned()
        } else {
            format!("{} {}", self.user_agent(), APP_USER_AGENT)
        };

        reqwest::Client::builder()
            .user_agent(format!(
                "{} ({})",
                agent,
                reason.unwrap_or_else(|| "A generic request".to_owned())
            ))
            .timeout(std::time::Duration::new(30, 0))
            .connect_timeout(std::time::Duration::new(30, 0))
            .connection_verbose(true)
            .build()
            .expect("Failed to construct HTTP client for IndieWeb interactivity")
    }

    /// The agent string to use when making out-going requests.
    fn user_agent(&self) -> &str {
        APP_USER_AGENT
    }
}

pub type Configuration = dyn IConfiguration + Send + Sync;