redfish_axum/
task_collection.rs

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