Open sergeysova opened 4 years ago
pub mod parameters { use actix_web::{FromRequest, HttpRequest}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] struct ParseHeaderError { error: String, message: String, } fn extract_header(req: &HttpRequest, name: String) -> Result<String, ParseHeaderError> { let header_error = ParseHeaderError { error: "header_required".to_string(), message: format!("header '{}' is required", name), }; let header = req.headers().get(name).ok_or(header_error.clone())?; let value = header.to_str().map_err(|_| header_error)?.to_string(); Ok(value) } pub struct AccessToken(pub String); impl FromRequest for AccessToken { type Config = (); type Error = actix_web::Error; type Future = futures::future::Ready<Result<Self, Self::Error>>; #[inline] fn from_request(req: &HttpRequest, _: &mut actix_web::dev::Payload) -> Self::Future { match extract_header(&req, "X-Access-Token".to_string()) { Ok(value) => futures::future::ok(AccessToken(value)), Err(reason) => match serde_json::to_string(&reason) { Ok(json) => futures::future::err(actix_web::error::ErrorBadRequest(json)), Err(error) => { futures::future::err(actix_web::error::ErrorInternalServerError(error)) } }, } } } }