use aep_core::{ErrorCode, new_problem_details};
use aep_service::ServiceResponse;
use bytes::Bytes;
use http::{HeaderValue, Response, StatusCode, header};
use http_body_util::Full;
use crate::TowerError;
pub type HttpResponse = Response<Full<Bytes>>;
pub(crate) fn service_response(response: ServiceResponse) -> Result<HttpResponse, TowerError> {
let status = StatusCode::from_u16(response.status).map_err(|_| {
TowerError::InvalidConfiguration("Service returned an invalid HTTP status".to_owned())
})?;
let body = serde_json::to_vec(&response.to_json()?)?;
let mut result = Response::new(Full::new(Bytes::from(body)));
*result.status_mut() = status;
*result.headers_mut() = response.headers;
Ok(result)
}
pub(crate) fn json_response(
status: StatusCode,
content_type: &'static str,
value: &impl serde::Serialize,
) -> Result<HttpResponse, TowerError> {
let mut response = Response::new(Full::new(Bytes::from(serde_json::to_vec(value)?)));
*response.status_mut() = status;
response
.headers_mut()
.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type));
Ok(response)
}
pub(crate) fn problem_response(code: ErrorCode, status: StatusCode) -> HttpResponse {
let mut characters = code.as_str().replace('_', " ").into_bytes();
if let Some(first) = characters.first_mut() {
first.make_ascii_uppercase();
}
let title = String::from_utf8(characters).unwrap_or_else(|_| code.as_str().to_owned());
let problem = new_problem_details(code, title, i64::from(status.as_u16()));
json_response(status, aep_core::PROBLEM_MEDIA_TYPE, &problem)
.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
}
pub(crate) fn method_not_allowed(allow: &'static str) -> HttpResponse {
let mut response = problem_response(ErrorCode::InvalidRequest, StatusCode::METHOD_NOT_ALLOWED);
response
.headers_mut()
.insert(header::ALLOW, HeaderValue::from_static(allow));
response
}
pub(crate) fn head_response(response: HttpResponse) -> HttpResponse {
let (mut parts, body) = response.into_parts();
let length = http_body::Body::size_hint(&body).exact().unwrap_or(0);
parts
.headers
.insert(header::CONTENT_LENGTH, HeaderValue::from(length));
Response::from_parts(parts, Full::new(Bytes::new()))
}
pub(crate) fn empty_response(status: StatusCode) -> HttpResponse {
let mut response = Response::new(Full::new(Bytes::new()));
*response.status_mut() = status;
response
}
pub(crate) fn internal_error() -> HttpResponse {
empty_response(StatusCode::INTERNAL_SERVER_ERROR)
}