rust_api_kit/http/
axum_integration.rs

1use axum::{Json, response::IntoResponse};
2use reqwest::StatusCode;
3use serde::Serialize;
4
5use super::client::Response;
6
7#[macro_export]
8macro_rules! generate_routes {
9    ( $( $req_ty:ty => $handler:path ),* $(,)? ) => {{
10        use axum::{Router, routing::{get, post, put, delete}};
11
12        let mut router = Router::new();
13        $(
14            router = router.route(
15                <$req_ty as $crate::http::client::HttpRequest<_, _, _>>::ENDPOINT,
16                match <$req_ty as $crate::http::client::HttpRequest<_, _, _>>::METHOD {
17                    $crate::http::client::RequestMethod::GET => get($handler),
18                    $crate::http::client::RequestMethod::POST => post($handler),
19                    $crate::http::client::RequestMethod::PUT => put($handler),
20                    $crate::http::client::RequestMethod::DELETE => delete($handler)
21                },
22            );
23        )*
24        router
25    }};
26}
27
28impl<T, E, U> IntoResponse for Response<T, E, U>
29where
30    T: Serialize,
31    E: Serialize,
32    U: Serialize + Clone,
33{
34    fn into_response(self) -> axum::response::Response {
35        let body = Json(self);
36
37        (StatusCode::OK, body).into_response()
38    }
39}