Introduce Response type alias as a shorthand for Response<BoxBody> (#590)

* Introduce `Response` type alias as a shorthand

* Don't re-export `Response` at the crate root
This commit is contained in:
Kai Jewson
2021-12-05 19:16:46 +01:00
committed by GitHub
parent cbb34869d8
commit dfb06e721c
41 changed files with 210 additions and 237 deletions
@@ -9,10 +9,9 @@
use axum::{
async_trait,
body::BoxBody,
extract::{Extension, Path},
http::{Response, StatusCode},
response::IntoResponse,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
AddExtensionLayer, Json, Router,
};
@@ -92,7 +91,7 @@ impl From<UserRepoError> for AppError {
}
impl IntoResponse for AppError {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let (status, error_message) = match self {
AppError::UserRepo(UserRepoError::NotFound) => {
(StatusCode::NOT_FOUND, "User not found")
+4 -4
View File
@@ -13,9 +13,9 @@
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs>
use axum::{
body::{self, Body, BoxBody},
http::{Method, Request, Response, StatusCode},
response::IntoResponse,
body::{self, Body},
http::{Method, Request, StatusCode},
response::{IntoResponse, Response},
routing::get,
Router,
};
@@ -55,7 +55,7 @@ async fn main() {
.unwrap();
}
async fn proxy(req: Request<Body>) -> Result<Response<BoxBody>, hyper::Error> {
async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> {
tracing::trace!(?req);
if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) {
+3 -4
View File
@@ -8,10 +8,9 @@
use axum::{
async_trait,
body::BoxBody,
extract::{FromRequest, RequestParts, TypedHeader},
http::{Response, StatusCode},
response::IntoResponse,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
@@ -141,7 +140,7 @@ where
}
impl IntoResponse for AuthError {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let (status, error_message) = match self {
AuthError::WrongCredentials => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
AuthError::MissingCredentials => (StatusCode::BAD_REQUEST, "Missing credentials"),
+3 -4
View File
@@ -9,10 +9,9 @@
use async_session::{MemoryStore, Session, SessionStore};
use axum::{
async_trait,
body::BoxBody,
extract::{Extension, FromRequest, Query, RequestParts, TypedHeader},
http::{header::SET_COOKIE, HeaderMap, Response},
response::{IntoResponse, Redirect},
http::{header::SET_COOKIE, HeaderMap},
response::{IntoResponse, Redirect, Response},
routing::get,
AddExtensionLayer, Router,
};
@@ -199,7 +198,7 @@ async fn login_authorized(
struct AuthRedirect;
impl IntoResponse for AuthRedirect {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Redirect::found("/auth/discord".parse().unwrap()).into_response()
}
}
+4 -3
View File
@@ -5,9 +5,10 @@
//! ```
use axum::{
body::{Body, BoxBody, Bytes},
body::{Body, Bytes},
error_handling::HandleErrorLayer,
http::{Request, Response, StatusCode},
http::{Request, StatusCode},
response::Response,
routing::post,
Router,
};
@@ -54,7 +55,7 @@ async fn map_request(req: Request<Body>) -> Result<Request<Body>, BoxError> {
Ok(req)
}
async fn map_response(res: Response<BoxBody>) -> Result<Response<Body>, BoxError> {
async fn map_response(res: Response) -> Result<Response<Body>, BoxError> {
let (parts, body) = res.into_parts();
let bytes = buffer_and_print("response", body).await?;
let res = Response::from_parts(parts, Body::from(bytes));
+4 -4
View File
@@ -6,10 +6,10 @@
use askama::Template;
use axum::{
body::{self, BoxBody, Full},
body::{self, Full},
extract,
http::{Response, StatusCode},
response::{Html, IntoResponse},
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
Router,
};
@@ -52,7 +52,7 @@ impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self.0.render() {
Ok(html) => Html(html).into_response(),
Err(err) => Response::builder()
+5 -7
View File
@@ -6,8 +6,8 @@
use axum::{
body::Bytes,
http::{HeaderMap, Request, Response},
response::Html,
http::{HeaderMap, Request},
response::{Html, Response},
routing::get,
Router,
};
@@ -42,11 +42,9 @@ async fn main() {
.on_request(|_request: &Request<_>, _span: &Span| {
// ...
})
.on_response(
|_response: &Response<_>, _latency: Duration, _span: &Span| {
// ...
},
)
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
// ...
})
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
// ..
})
+3 -4
View File
@@ -12,10 +12,9 @@
use async_trait::async_trait;
use axum::{
body::BoxBody,
extract::{Form, FromRequest, RequestParts},
http::{Response, StatusCode},
response::{Html, IntoResponse},
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
BoxError, Router,
};
@@ -84,7 +83,7 @@ pub enum ServerError {
}
impl IntoResponse for ServerError {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self {
ServerError::ValidationError(_) => {
let message = format!("Input validation error: [{}]", self).replace("\n", ", ");
+3 -4
View File
@@ -6,10 +6,9 @@
use axum::{
async_trait,
body::BoxBody,
extract::{FromRequest, Path, RequestParts},
http::{Response, StatusCode},
response::IntoResponse,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Router,
};
@@ -51,7 +50,7 @@ impl<B> FromRequest<B> for Version
where
B: Send,
{
type Rejection = Response<BoxBody>;
type Rejection = Response;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = Path::<HashMap<String, String>>::from_request(req)