redfish_axum/
task_service.rs

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