use hyper::server;
pub use hyper::{header, Method, StatusCode};
#[derive(Debug)]
pub struct Response {
pub code: StatusCode,
pub content_type: header::ContentType,
pub content: String,
}
impl Response {
pub fn empty() -> Self {
Self::ok(String::new())
}
pub fn ok<T: Into<String>>(response: T) -> Self {
Response {
code: StatusCode::Ok,
content_type: header::ContentType::json(),
content: response.into(),
}
}
pub fn internal_error() -> Self {
Response {
code: StatusCode::Forbidden,
content_type: header::ContentType::plaintext(),
content: "Provided Host header is not whitelisted.\n".to_owned(),
}
}
pub fn host_not_allowed() -> Self {
Response {
code: StatusCode::Forbidden,
content_type: header::ContentType::plaintext(),
content: "Provided Host header is not whitelisted.\n".to_owned(),
}
}
pub fn unsupported_content_type() -> Self {
Response {
code: StatusCode::UnsupportedMediaType,
content_type: header::ContentType::plaintext(),
content: "Supplied content type is not allowed. Content-Type: application/json is required\n".to_owned(),
}
}
pub fn method_not_allowed() -> Self {
Response {
code: StatusCode::MethodNotAllowed,
content_type: header::ContentType::plaintext(),
content: "Used HTTP Method is not allowed. POST or OPTIONS is required\n".to_owned(),
}
}
pub fn invalid_cors() -> Self {
Response {
code: StatusCode::Forbidden,
content_type: header::ContentType::plaintext(),
content: "Origin of the request is not whitelisted. CORS headers would not be sent and any side-effects were cancelled as well.\n".to_owned(),
}
}
}
impl Into<server::Response> for Response {
fn into(self) -> server::Response {
server::Response::new()
.with_status(self.code)
.with_header(self.content_type)
.with_body(self.content)
}
}