redfish_axum/
metric_report.rs

1// Generated by redfish-codegen. Do not modify.
2
3/// The default privileges required for accessing MetricReport instances.
4pub struct DefaultPrivileges;
5impl redfish_core::privilege::OperationPrivilegeMapping for DefaultPrivileges {
6    type Get = redfish_core::privilege::Login;
7    type Head = redfish_core::privilege::Login;
8    type Post = redfish_core::privilege::ConfigureManager;
9    type Put = redfish_core::privilege::ConfigureManager;
10    type Patch = redfish_core::privilege::ConfigureManager;
11    type Delete = redfish_core::privilege::ConfigureManager;
12}
13
14/// This endpoint implements the MetricReport component.
15///
16/// It can be mounted on the following components:
17/// * [MetricReportCollection][crate::metric_report_collection::MetricReportCollection]
18pub struct MetricReport<S, P>
19where
20    S: Clone,
21{
22    router: axum::routing::MethodRouter<S>,
23    privilege_marker: std::marker::PhantomData<fn() -> P>,
24    allowed_methods: Vec<axum::http::method::Method>,
25}
26
27impl<S> Default for MetricReport<S, DefaultPrivileges>
28where
29    S: Clone,
30{
31    fn default() -> Self {
32        Self {
33            router: Default::default(),
34            privilege_marker: Default::default(),
35            allowed_methods: Vec::new(),
36        }
37    }
38}
39
40impl<S, P> MetricReport<S, P>
41where
42    S: AsRef<dyn redfish_core::auth::AuthenticateRequest> + Clone + Send + Sync + 'static,
43    P: redfish_core::privilege::OperationPrivilegeMapping + 'static,
44    <P as redfish_core::privilege::OperationPrivilegeMapping>::Get: Send,
45    <P as redfish_core::privilege::OperationPrivilegeMapping>::Delete: Send,
46{
47    pub fn get<H, T>(mut self, handler: H) -> Self
48    where
49        H: axum::handler::Handler<T, S, axum::body::Body>,
50        T: 'static,
51    {
52        let operation = axum::routing::get(
53            |auth: redfish_core::extract::RedfishAuth<P::Get>,
54             axum::extract::State(state): axum::extract::State<S>,
55             mut request: axum::http::Request<axum::body::Body>| async {
56                request.extensions_mut().insert(auth.user);
57                handler.call(request, state).await
58            },
59        );
60        self.router = self.router.get(operation);
61        self.allowed_methods.push(axum::http::method::Method::GET);
62        self
63    }
64
65    pub fn delete<H, T>(mut self, handler: H) -> Self
66    where
67        H: axum::handler::Handler<T, S, axum::body::Body>,
68        T: 'static,
69    {
70        let operation = axum::routing::delete(
71            |auth: redfish_core::extract::RedfishAuth<P::Delete>,
72             axum::extract::State(state): axum::extract::State<S>,
73             mut request: axum::http::Request<axum::body::Body>| async {
74                request.extensions_mut().insert(auth.user);
75                handler.call(request, state).await
76            },
77        );
78        self.router = self.router.delete(operation);
79        self.allowed_methods.push(axum::http::method::Method::DELETE);
80        self
81    }
82
83    pub fn into_router(self) -> axum::Router<S> {
84        let Self {
85            router,
86            mut allowed_methods,
87            ..
88        } = self;
89        let result = axum::Router::default();
90        allowed_methods.dedup();
91        let allow_header = allowed_methods
92            .into_iter()
93            .map(|method| method.to_string())
94            .reduce(|one, two| one + "," + &two)
95            .unwrap();
96        result.route(
97            "/",
98            router.fallback(|| async {
99                (
100                    axum::http::StatusCode::METHOD_NOT_ALLOWED,
101                    axum::Json(redfish_core::error::one_message(redfish_codegen::registries::base::v1_16_0::Base::OperationNotAllowed.into())),
102                )
103            })
104            .route_layer(axum::middleware::from_fn_with_state(
105                allow_header,
106                |axum::extract::State(allow_header): axum::extract::State<String>,
107                 request: axum::http::Request<axum::body::Body>,
108                 next: axum::middleware::Next<axum::body::Body>| async move {
109                    let apply_allow = matches!(*request.method(), axum::http::Method::GET | axum::http::Method::HEAD);
110                    let mut response = next.run(request).await;
111                    if apply_allow && !response.headers().contains_key(axum::http::header::ALLOW) {
112                        response.headers_mut().insert(
113                            axum::http::header::ALLOW,
114                            axum::http::HeaderValue::from_str(&allow_header).unwrap(),
115                        );
116                    }
117                    response
118                },
119            )),
120        )
121    }
122}