use crate::{Vote, VoteHandler};
use std::sync::Arc;
use warp::{body, header, http::StatusCode, path, Filter, Rejection, Reply};
#[cfg_attr(docsrs, doc(cfg(feature = "warp")))]
pub fn webhook<T>(
endpoint: &'static str,
password: String,
state: Arc<T>,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone
where
T: VoteHandler,
{
let password = Arc::new(password);
warp::post()
.and(path(endpoint))
.and(header("Authorization"))
.and(body::json())
.then(move |auth: String, vote: Vote| {
let current_state = Arc::clone(&state);
let current_password = Arc::clone(&password);
async move {
if auth == *current_password {
current_state.voted(vote).await;
StatusCode::OK
} else {
StatusCode::UNAUTHORIZED
}
}
})
}