parse_rs/config.rs
1use crate::client::UpdateConfigResponse;
2use crate::ParseError;
3use reqwest::Method;
4use serde::Deserialize;
5use serde_json::Value;
6use std::collections::HashMap;
7
8/// Represents the Parse Server configuration parameters.
9/// The server returns parameters nested under a "params" key.
10#[derive(Debug, Deserialize, Clone, PartialEq)]
11pub struct ParseConfig {
12 /// A map of parameter names to their values.
13 pub params: HashMap<String, Value>,
14}
15
16// We might add helper methods to ParseConfig later, e.g., to get a specific typed parameter.
17impl ParseConfig {
18 /// Retrieves a specific parameter by name and attempts to deserialize it into the requested type.
19 ///
20 /// # Arguments
21 /// * `key`: The name of the parameter to retrieve.
22 ///
23 /// # Returns
24 /// An `Option<T>` containing the deserialized value if the key exists and deserialization is successful,
25 /// otherwise `None`.
26 pub fn get<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
27 self.params
28 .get(key)
29 .and_then(|value| serde_json::from_value(value.clone()).ok())
30 }
31}
32
33impl crate::Parse {
34 // Config management methods
35
36 /// Retrieves the Parse Server configuration.
37 ///
38 /// This operation requires the Master Key.
39 ///
40 /// # Returns
41 /// A `Result` containing the `ParseConfig` or a `ParseError`.
42 pub async fn get_config(&self) -> Result<ParseConfig, ParseError> {
43 if self.master_key.is_none() {
44 return Err(ParseError::MasterKeyRequired(
45 "Master key is required to get server configuration.".to_string(),
46 ));
47 }
48
49 let endpoint = "config";
50 self._request(
51 Method::GET,
52 endpoint,
53 None::<&Value>, // No body for GET
54 true, // Use master key
55 None, // No session token needed when using master key
56 )
57 .await
58 }
59
60 /// Updates the Parse Server configuration parameters.
61 ///
62 /// This operation requires the Master Key.
63 /// The `params_to_update` should contain only the parameters you wish to change.
64 ///
65 /// # Arguments
66 /// * `params_to_update`: A `HashMap<String, Value>` of parameters to update.
67 ///
68 /// # Returns
69 /// A `Result` indicating success (typically an empty successful response or a confirmation)
70 /// or a `ParseError`.
71 /// The Parse Server responds with `{"result": true}` on successful update.
72 pub async fn update_config(
73 &self,
74 params_to_update: &HashMap<String, Value>,
75 ) -> Result<UpdateConfigResponse, ParseError> {
76 if self.master_key.is_none() {
77 return Err(ParseError::MasterKeyRequired(
78 "Master key is required to update server configuration.".to_string(),
79 ));
80 }
81 if params_to_update.is_empty() {
82 return Err(ParseError::InvalidInput(
83 "params_to_update cannot be empty for update_config.".to_string(),
84 ));
85 }
86
87 let endpoint = "config";
88 // The body should be wrapped: {"params": params_to_update}
89 let body = serde_json::json!({ "params": params_to_update });
90
91 self._request(
92 Method::PUT,
93 endpoint,
94 Some(&body), // Pass the wrapped body
95 true, // Use master key
96 None, // No session token needed
97 )
98 .await
99 }
100}