Skip to main content

powerplatform_dataverse_client/dataverse/
requestparameters.rs

1use reqwest::RequestBuilder;
2
3/// Optional Dataverse request parameters for create and update operations.
4#[derive(Debug, Clone, Default)]
5pub struct RequestParameters {
6    /// Send `MSCRM.BypassBusinessLogicExecution=CustomSync`.
7    pub bypass_business_logic_execution_custom_sync: bool,
8    /// Send `MSCRM.BypassBusinessLogicExecution=CustomAsync`.
9    pub bypass_business_logic_execution_custom_async: bool,
10    /// Send `MSCRM.BypassCustomPluginExecution=true`.
11    pub bypass_custom_plugin_execution: bool,
12    /// Send `MSCRM.SuppressCallbackRegistrationExpanderJob=true`.
13    pub suppress_callback_registration_expander_job: bool,
14    // Step-specific bypass ids are intentionally omitted for now because they need a more stable
15    // public shape than a raw string list. The current API only exposes the simple boolean-style
16    // switches that map cleanly to well-known headers.
17    // pub bypass_business_logic_execution_step_ids: Option<Vec<String>>,
18}
19
20impl RequestParameters {
21    /// Return the Dataverse request headers represented by these parameters.
22    pub fn headers(&self) -> Vec<(&'static str, &'static str)> {
23        let mut headers = Vec::new();
24
25        if let Some(value) = self.bypass_business_logic_execution_value() {
26            headers.push(("MSCRM.BypassBusinessLogicExecution", value));
27        }
28
29        if self.bypass_custom_plugin_execution {
30            headers.push(("MSCRM.BypassCustomPluginExecution", "true"));
31        }
32
33        if self.suppress_callback_registration_expander_job {
34            headers.push(("MSCRM.SuppressCallbackRegistrationExpanderJob", "true"));
35        }
36
37        headers
38    }
39
40    /// Apply the configured Dataverse request parameters to an outgoing request.
41    pub fn apply(&self, mut request: RequestBuilder) -> RequestBuilder {
42        for (header, value) in self.headers() {
43            request = request.header(header, value);
44        }
45
46        // Step-id bypass headers are not emitted yet for the same reason documented on the struct:
47        // the crate does not currently expose a stable typed API for managing those ids.
48        // if let Some(step_ids) = &self.bypass_business_logic_execution_step_ids {
49        //     request = request.header(
50        //         "MSCRM.BypassBusinessLogicExecutionStepIds",
51        //         step_ids.join(","),
52        //     );
53        // }
54
55        request
56    }
57
58    /// Compose the `MSCRM.BypassBusinessLogicExecution` header value.
59    fn bypass_business_logic_execution_value(&self) -> Option<&'static str> {
60        match (
61            self.bypass_business_logic_execution_custom_sync,
62            self.bypass_business_logic_execution_custom_async,
63        ) {
64            (true, true) => Some("CustomSync,CustomAsync"),
65            (true, false) => Some("CustomSync"),
66            (false, true) => Some("CustomAsync"),
67            (false, false) => None,
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::RequestParameters;
75
76    #[test]
77    fn headers_include_requested_bypass_flags() {
78        let parameters = RequestParameters {
79            bypass_business_logic_execution_custom_sync: true,
80            bypass_business_logic_execution_custom_async: true,
81            bypass_custom_plugin_execution: true,
82            suppress_callback_registration_expander_job: true,
83        };
84
85        let headers = parameters.headers();
86
87        assert!(headers.contains(&(
88            "MSCRM.BypassBusinessLogicExecution",
89            "CustomSync,CustomAsync"
90        )));
91        assert!(headers.contains(&("MSCRM.BypassCustomPluginExecution", "true")));
92        assert!(headers.contains(&(
93            "MSCRM.SuppressCallbackRegistrationExpanderJob",
94            "true"
95        )));
96    }
97
98    #[test]
99    fn headers_omit_business_logic_value_when_no_flags_are_set() {
100        let headers = RequestParameters::default().headers();
101
102        assert!(!headers
103            .iter()
104            .any(|(name, _)| *name == "MSCRM.BypassBusinessLogicExecution"));
105    }
106}