force 0.4.0

Production-ready Salesforce Platform API client with REST and Bulk API 2.0 support
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Salesforce Files API.
//!
//! Provides capabilities to upload and download ContentVersions, and link them to records
//! via ContentDocumentLinks without loading the entire binary content into memory.

use crate::auth::Authenticator;
use crate::error::{ForceError, HttpError, Result};
use crate::session::Session;
use reqwest::multipart::{Form, Part};
use serde_json::json;
use std::sync::Arc;

/// Maximum body size (100 MiB) accepted for a `ContentVersion` download/upload.
const MAX_CONTENT_BODY: usize = 100 * 1024 * 1024;

/// Maximum body size (10 MiB) accepted for a `ContentDocumentLink` JSON response.
const MAX_LINK_BODY: usize = 10 * 1024 * 1024;

/// A handler for interacting with Salesforce Files.
#[derive(Debug, Clone)]
pub struct FilesHandler<A: Authenticator> {
    session: Arc<Session<A>>,
}

impl<A: Authenticator> FilesHandler<A> {
    /// Creates a new Files API handler.
    #[must_use]
    pub fn new(session: Arc<Session<A>>) -> Self {
        Self { session }
    }

    /// Uploads a file, creating a new `ContentVersion`.
    ///
    /// The request is routed through the shared HTTP executor, so it benefits
    /// from automatic 401 token refresh, 429/`Retry-After` handling, and 503
    /// backoff. Because the multipart body cannot be cloned for a retry, the
    /// request is rebuilt from the retained title/path/bytes on each attempt.
    ///
    /// # Arguments
    /// * `title` - The title of the document.
    /// * `path_on_client` - The original filename.
    /// * `file_bytes` - The contents of the file.
    ///
    /// # Returns
    /// The ID of the inserted `ContentVersion`.
    pub async fn upload(
        &self,
        title: &str,
        path_on_client: &str,
        file_bytes: Vec<u8>,
    ) -> Result<String> {
        let url = self.session.resolve_url("sobjects/ContentVersion").await?;

        let session = Arc::clone(&self.session);
        let title = title.to_string();
        let path_on_client = path_on_client.to_string();

        // Rebuild the multipart form (and its body) on every attempt: streaming
        // multipart bodies cannot be cloned, so the executor calls this factory
        // fresh for each retry / post-401 retry.
        let make_request = move || -> Result<reqwest::Request> {
            let entity_content = json!({
                "Title": title,
                "PathOnClient": path_on_client,
            });

            let entity_part = Part::text(entity_content.to_string())
                .mime_str("application/json")
                .map_err(|e| ForceError::InvalidInput(e.to_string()))?;

            let version_data_part = Part::bytes(file_bytes.clone())
                .file_name(path_on_client.clone())
                .mime_str("application/octet-stream")
                .map_err(|e| ForceError::InvalidInput(e.to_string()))?;

            let form = Form::new()
                .part("entity_content", entity_part)
                .part("VersionData", version_data_part);

            session
                .post(&url)
                .multipart(form)
                .build()
                .map_err(|e| ForceError::Http(HttpError::RequestFailed(e)))
        };

        let response = self.session.execute_request_factory(make_request).await?;
        let status = response.status();
        let bytes = crate::http::error::read_capped_body_bytes(response, MAX_CONTENT_BODY).await?;

        if !status.is_success() {
            return Err(ForceError::InvalidInput(
                "Failed to upload ContentVersion".into(),
            ));
        }

        Self::extract_created_id(&bytes, "Failed to upload ContentVersion")
    }

    /// Downloads the binary data of a `ContentVersion`.
    ///
    /// Routed through the shared HTTP executor (401 refresh + retry / rate-limit
    /// / backoff middleware).
    ///
    /// # Arguments
    /// * `content_version_id` - The ID of the ContentVersion.
    ///
    /// # Returns
    /// The binary content as a `Vec<u8>`.
    pub async fn download(&self, content_version_id: &str) -> Result<Vec<u8>> {
        let path = format!("sobjects/ContentVersion/{content_version_id}/VersionData");
        let url = self.session.resolve_url(&path).await?;

        let request = self
            .session
            .get(&url)
            .build()
            .map_err(|e| ForceError::Http(HttpError::RequestFailed(e)))?;

        let response = self.session.execute_request(request).await?;
        let status = response.status();
        let bytes = crate::http::error::read_capped_body_bytes(response, MAX_CONTENT_BODY).await?;

        if !status.is_success() {
            return Err(ForceError::InvalidInput(
                "Failed to download ContentVersion".into(),
            ));
        }

        Ok(bytes)
    }

    /// Links a ContentDocument to an entity (e.g. Account, Contact).
    ///
    /// Routed through the shared HTTP executor (401 refresh + retry / rate-limit
    /// / backoff middleware).
    ///
    /// # Arguments
    /// * `content_document_id` - The ID of the ContentDocument (not the ContentVersion).
    /// * `linked_entity_id` - The ID of the target Salesforce record.
    ///
    /// # Returns
    /// The ID of the inserted `ContentDocumentLink`.
    pub async fn link_to_record(
        &self,
        content_document_id: &str,
        linked_entity_id: &str,
    ) -> Result<String> {
        let url = self
            .session
            .resolve_url("sobjects/ContentDocumentLink")
            .await?;

        let payload = json!({
            "ContentDocumentId": content_document_id,
            "LinkedEntityId": linked_entity_id,
            "ShareType": "V"
        });

        let request = self
            .session
            .post(&url)
            .json(&payload)
            .build()
            .map_err(|e| ForceError::Http(HttpError::RequestFailed(e)))?;

        let response = self.session.execute_request(request).await?;
        let status = response.status();
        let bytes = crate::http::error::read_capped_body_bytes(response, MAX_LINK_BODY).await?;

        if !status.is_success() {
            return Err(ForceError::InvalidInput(
                "Failed to insert ContentDocumentLink".into(),
            ));
        }

        Self::extract_created_id(&bytes, "Failed to insert ContentDocumentLink")
    }

    /// Parses a Salesforce sObject-create response body, returning the new record
    /// `id` when `success` is `true`, or `error_message` otherwise.
    fn extract_created_id(body: &[u8], error_message: &'static str) -> Result<String> {
        let result: serde_json::Value = serde_json::from_slice(body)
            .map_err(|e| ForceError::Serialization(crate::error::SerializationError::Json(e)))?;

        if result["success"].as_bool().unwrap_or(false) {
            Ok(result["id"].as_str().unwrap_or_default().to_string())
        } else {
            Err(ForceError::InvalidInput(error_message.into()))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::AccessToken;
    use crate::client::builder;
    use crate::test_utils::mock_auth::MockAuthenticator;
    use crate::test_utils::must::{Must, MustMsg};
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use wiremock::matchers::{header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// Test authenticator whose initial `authenticate()` yields an "expired"
    /// (server-rejected) token and whose `refresh()` yields a distinct fresh
    /// token, counting how many times a refresh occurs. This lets a handler-level
    /// test prove that a 401 triggers a token refresh + retry through the shared
    /// executor middleware.
    #[derive(Debug)]
    struct RefreshingAuthenticator {
        instance_url: String,
        refresh_calls: Arc<AtomicUsize>,
    }

    #[async_trait]
    impl Authenticator for RefreshingAuthenticator {
        async fn authenticate(&self) -> crate::error::Result<AccessToken> {
            // Valid (not soft-expired) so the token manager hands it out as-is;
            // the *server* is what rejects it with a 401.
            Ok(AccessToken::new(
                "expired_token".to_string(),
                self.instance_url.clone(),
                Some(chrono::Utc::now() + chrono::Duration::hours(1)),
            ))
        }

        async fn refresh(&self) -> crate::error::Result<AccessToken> {
            self.refresh_calls.fetch_add(1, Ordering::SeqCst);
            Ok(AccessToken::new(
                "fresh_token".to_string(),
                self.instance_url.clone(),
                Some(chrono::Utc::now() + chrono::Duration::hours(2)),
            ))
        }
    }

    #[tokio::test]
    async fn test_download_refreshes_on_401_and_retries() {
        let mock_server = MockServer::start().await;

        // First attempt with the stale token is rejected with 401.
        Mock::given(method("GET"))
            .and(path(
                "/services/data/v67.0/sobjects/ContentVersion/068000000000001AAA/VersionData",
            ))
            .and(header("Authorization", "Bearer expired_token"))
            .respond_with(ResponseTemplate::new(401).set_body_json(json!([{
                "message": "Session expired or invalid",
                "errorCode": "INVALID_SESSION_ID"
            }])))
            .up_to_n_times(1)
            .mount(&mock_server)
            .await;

        // Second attempt with the refreshed token succeeds with the bytes.
        let expected_bytes = vec![9, 8, 7, 6, 5];
        Mock::given(method("GET"))
            .and(path(
                "/services/data/v67.0/sobjects/ContentVersion/068000000000001AAA/VersionData",
            ))
            .and(header("Authorization", "Bearer fresh_token"))
            .respond_with(ResponseTemplate::new(200).set_body_bytes(expected_bytes.clone()))
            .mount(&mock_server)
            .await;

        let refresh_calls = Arc::new(AtomicUsize::new(0));
        let auth = RefreshingAuthenticator {
            instance_url: mock_server.uri(),
            refresh_calls: Arc::clone(&refresh_calls),
        };
        let client = builder().authenticate(auth).build().await.must();

        let bytes = client
            .files()
            .download("068000000000001AAA")
            .await
            .must_msg("download should refresh on 401 and retry");

        assert_eq!(bytes, expected_bytes);
        assert_eq!(
            refresh_calls.load(Ordering::SeqCst),
            1,
            "expected exactly one token refresh triggered by the 401"
        );
    }

    #[tokio::test]
    async fn test_upload_refreshes_on_401_and_retries() {
        let mock_server = MockServer::start().await;

        // First multipart POST with the stale token is rejected with 401. This
        // exercises the non-clonable factory path: the executor must rebuild the
        // multipart body from scratch (via `make_request`) for the retry.
        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/sobjects/ContentVersion"))
            .and(header("Authorization", "Bearer expired_token"))
            .respond_with(ResponseTemplate::new(401).set_body_json(json!([{
                "message": "Session expired or invalid",
                "errorCode": "INVALID_SESSION_ID"
            }])))
            .up_to_n_times(1)
            .mount(&mock_server)
            .await;

        // Second POST with the refreshed token succeeds, returning the new id.
        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/sobjects/ContentVersion"))
            .and(header("Authorization", "Bearer fresh_token"))
            .respond_with(ResponseTemplate::new(201).set_body_json(json!({
                "id": "068000000000001AAA",
                "success": true
            })))
            .mount(&mock_server)
            .await;

        let refresh_calls = Arc::new(AtomicUsize::new(0));
        let auth = RefreshingAuthenticator {
            instance_url: mock_server.uri(),
            refresh_calls: Arc::clone(&refresh_calls),
        };
        let client = builder().authenticate(auth).build().await.must();

        let id = client
            .files()
            .upload("Test Title", "test.pdf", vec![1, 2, 3])
            .await
            .must_msg("upload should refresh on 401 and retry");

        assert_eq!(id, "068000000000001AAA");
        assert_eq!(
            refresh_calls.load(Ordering::SeqCst),
            1,
            "expected exactly one token refresh triggered by the 401"
        );
    }

    #[tokio::test]
    async fn test_upload_file() {
        let mock_server = MockServer::start().await;
        let auth = MockAuthenticator::new("test_token", &mock_server.uri());
        let client = builder().authenticate(auth).build().await.must();

        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/sobjects/ContentVersion"))
            .respond_with(ResponseTemplate::new(201).set_body_json(json!({
                "id": "068000000000001AAA",
                "success": true,
                "errors": []
            })))
            .mount(&mock_server)
            .await;

        let files = FilesHandler::new(client.session());
        let id = files
            .upload("Test Title", "test.pdf", vec![1, 2, 3])
            .await
            .must_msg("Failed to upload file");

        assert_eq!(id, "068000000000001AAA");
    }

    #[tokio::test]
    async fn test_download_file() {
        let mock_server = MockServer::start().await;
        let auth = MockAuthenticator::new("test_token", &mock_server.uri());
        let client = builder().authenticate(auth).build().await.must();

        let expected_bytes = vec![1, 2, 3, 4, 5];

        Mock::given(method("GET"))
            .and(path(
                "/services/data/v67.0/sobjects/ContentVersion/068000000000001AAA/VersionData",
            ))
            .respond_with(ResponseTemplate::new(200).set_body_bytes(expected_bytes.clone()))
            .mount(&mock_server)
            .await;

        let files = FilesHandler::new(client.session());
        let bytes = files
            .download("068000000000001AAA")
            .await
            .must_msg("Failed to download file");

        assert_eq!(bytes, expected_bytes);
    }

    #[tokio::test]
    async fn test_link_to_record() {
        let mock_server = MockServer::start().await;
        let auth = MockAuthenticator::new("test_token", &mock_server.uri());
        let client = builder().authenticate(auth).build().await.must();

        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/sobjects/ContentDocumentLink"))
            .respond_with(ResponseTemplate::new(201).set_body_json(json!({
                "id": "06A000000000001AAA",
                "success": true,
                "errors": []
            })))
            .mount(&mock_server)
            .await;

        let files = FilesHandler::new(client.session());
        let link_id = files
            .link_to_record("069000000000001AAA", "001000000000001AAA")
            .await
            .must_msg("Failed to link file to record");

        assert_eq!(link_id, "06A000000000001AAA");
    }
}