1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use self::global_describe_response::GlobalDescribeResponse;
use crate::RestApi;
use crate::errors::Error;
use crate::rest_api::HTTPMethod;
use crate::rest_api::builder::sobjects::SObjectsBuilder;
use chrono::Utc;
use serde_json::json;
mod global_describe_response;
impl RestApi {
/// Get a List of Objects Describes
///
/// Sends a request to the Salesforce API to retrieve metadata information about all
/// global objects (SObjects) available in the Salesforce instance.
///
/// This function constructs the appropriate URL using the client's base path, makes
/// an HTTP GET request to fetch the list of global objects, and processes the JSON
/// response to return the parsed data as a `DescribeGlobalResponse`.
///
/// # Errors
///
/// This function returns an `Error` in the following cases:
/// - If constructing the resource URL fails.
/// - If the HTTP GET request fails.
/// - If the response cannot be parsed into the expected `DescribeGlobalResponse` format.
///
/// # Returns
///
/// On success, returns a `Result` wrapping a `DescribeGlobalResponse` which contains
/// information about the global objects available in Salesforce.
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use anyhow::Result;
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// match api.describe_global().await {
/// Ok(response) => {
/// println!("Successfully retrieved global objects: {:?}", response);
/// }
/// Err(e) => {
/// eprintln!("Error retrieving global objects: {:?}", e);
/// }
/// }
/// }
/// ```
///
/// # Note
/// To retrieve the complete metadata for an object, use the [describe_sobject](#method.describe_sobject) method
///
/// # See
/// - <https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_describeGlobal.htm>
pub async fn describe_global(&self) -> Result<GlobalDescribeResponse, Error> {
self.build().sobjects().describe_global().send().await
}
/// Get a List of Objects
///
/// Sends a request to the Salesforce API to retrieve metadata information about
/// global objects (SObjects) available in the Salesforce instance,
/// which are modified since the given date
///
///
/// # Parameters
/// - `since` (String): A string representation of the timestamp
/// (in `EEE, dd MMM yyyy HH:mm:ss z` format, eg. `Mon, 30 Nov 2020 08:34:54 MST`).
///
/// # Returns
/// - `Ok(DescribeGlobalResponse)`: On success, returns a `DescribeGlobalResponse` containing the list of global
/// sObjects and their metadata.
/// - `Err(Error)`: If an error occurs during the request or while processing the response, an `Error` is returned.
///
/// # Errors
/// - Returns an error if:
/// - The client's base version path is invalid.
/// - The HTTP request fails.
/// - The response cannot be parsed into a valid `DescribeGlobalResponse`.
/// - If there is no available object’s, a 304 Not Modified status code is returned // todo - it should return an empty vec..
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use chrono::{Utc, TimeZone};
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// let since = Utc.with_ymd_and_hms(2015, 10, 21, 7, 28, 0).unwrap();
/// match api.describe_global_modified(since).await {
/// Ok(response) => println!("Retrieved modified sObjects: {:?}", response),
/// Err(err) => println!("Failed to retrieve modified sObjects: {:?}", err),
/// }
/// }
/// ```
///
/// # See
/// <https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_describeGlobal.htm>
pub async fn describe_global_modified(
&self,
datetime: chrono::DateTime<Utc>,
) -> Result<GlobalDescribeResponse, Error> {
Ok(self
.build()
.if_modified_since_header(&datetime)
.sobjects()
.describe_global()
.send()
.await?) // fixme Can potentially return 304 if nothing could be found, add something nice here
}
/// Get a List of Objects
///
/// Sends a request to the Salesforce API to retrieve metadata information about
/// global objects (SObjects) available in the Salesforce instance,
/// that haven’t been modified after that date and time.
///
///
/// # Parameters
/// - `since` (String): A string representation of the timestamp
/// (in `EEE, dd MMM yyyy HH:mm:ss z` format, eg. `Mon, 30 Nov 2020 08:34:54 MST`).
///
/// # Returns
/// - `Ok(DescribeGlobalResponse)`: On success, returns a `DescribeGlobalResponse` containing the list of global
/// sObjects and their metadata.
/// - `Err(Error)`: If an error occurs during the request or while processing the response, an `Error` is returned.
///
/// # Errors
/// - Returns an error if:
/// - The client's base version path is invalid.
/// - The HTTP request fails.
/// - The response cannot be parsed into a valid `DescribeGlobalResponse`.
/// - If there is no available object’s, a 304 Not Modified status code is returned // todo - it should return an empty vec..
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use anyhow::Result;
/// use chrono::{Utc, TimeZone};
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// let since = Utc.with_ymd_and_hms(2015, 10, 21, 7, 28, 0).unwrap();
/// match api.describe_global_unmodified(since).await {
/// Ok(response) => println!("Retrieved unmodified sObjects: {:?}", response),
/// Err(err) => println!("Failed to retrieve unmodified sObjects: {:?}", err),
/// }
/// }
/// ```
/// # See
/// <https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_describeGlobal.htm>
pub async fn describe_global_unmodified(
&self,
datetime: chrono::DateTime<Utc>,
) -> Result<GlobalDescribeResponse, Error> {
let since = datetime.format("%a, %d %b %Y %H:%M:%S %:z").to_string();
Ok(self
.build()
.if_unmodified_since_header(&since)
.sobjects()
.describe_global()
.send()
.await?) // fixme Can potentially return 304 if nothing could be found, add something nice here
}
}
impl<'a> SObjectsBuilder<'a> {
/// Get a List of Objects Describes
///
/// Sends a request to the Salesforce API to retrieve metadata information about all
/// global objects (SObjects) available in the Salesforce instance.
///
/// This function constructs the appropriate URL using the client's base path, makes
/// an HTTP GET request to fetch the list of global objects, and processes the JSON
/// response to return the parsed data as a `DescribeGlobalResponse`.
///
/// # Errors
///
/// This function returns an `Error` in the following cases:
/// - If constructing the resource URL fails.
/// - If the HTTP GET request fails.
/// - If the response cannot be parsed into the expected `DescribeGlobalResponse` format.
///
/// # Returns
///
/// On success, returns a `Result` wrapping a `DescribeGlobalResponse` which contains
/// information about the global objects available in Salesforce.
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use chrono::{Utc, TimeZone};
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// let since = Utc.with_ymd_and_hms(2015, 10, 21, 7, 28, 0).unwrap();
///
/// match api.build()
/// .if_modified_since_header(&since)
/// .sobjects()
/// .describe_global()
/// .send()
/// .await {
/// Ok(response) => {
/// println!("Successfully retrieved global objects: {:?}", response);
/// }
/// Err(e) => {
/// eprintln!("Error retrieving global objects: {:?}", e);
/// }
/// }
/// }
/// ```
///
/// # Note
/// To retrieve the complete metadata for an object, use the [describe_sobject](#method.describe_sobject) method.
///
/// Use the `if_modified_since_heaer' or 'if_unmodified_since_header' methods to filter further
///
/// # See
/// - `if_modified_since_heaer`
/// - `if_unmodified_since_header`
/// <https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_describeGlobal.htm>
pub fn describe_global(&'a self) -> GlobalDescribeBuilder<'a> {
GlobalDescribeBuilder::new(self)
}
}
pub struct GlobalDescribeBuilder<'a> {
builder: &'a SObjectsBuilder<'a>,
}
impl<'a> GlobalDescribeBuilder<'a> {
pub(crate) fn new(builder: &'a SObjectsBuilder) -> GlobalDescribeBuilder<'a> {
Self { builder }
}
/// Sends the request to retrieve the Salesforce SObject type sobject_basic_info.
///
/// # Returns
///
/// - On success, returns a `Result` containing `SObjectDescribeResponse`.
/// - On failure, returns an `Err` containing the reason for failure.
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// match api.build().sobjects().describe_global().send().await {
/// Ok(response) => println!("Describe result: {:?}", response),
/// Err(e) => println!("Error retrieving object description: {:?}", e),
/// }
/// }
/// ```
pub async fn send(self) -> Result<GlobalDescribeResponse, Error> {
let body = json!({});
let url = "sobjects".to_string();
let http_response = self
.builder // gets the SObjectBuild
.builder // gets the RestApiBuild
.send(HTTPMethod::GET, url, &body)
.await?;
RestApi::handle_json_response(http_response).await
}
}