force 0.2.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Generic Apex REST API handler.
//!
//! The Apex REST handler provides generic HTTP access to any custom Apex REST
//! endpoint at `/services/apexrest/{path}`. This is the foundation for both
//! custom Apex REST classes and the Salesforce CPQ API.
//!
//! # Feature Flag
//!
//! This module requires the `apex_rest` feature flag:
//! ```toml
//! [dependencies]
//! force = { version = "...", features = ["apex_rest"] }
//! ```
//!
//! # Usage
//!
//! ```ignore
//! let client = builder().authenticate(auth).build().await?;
//! let apex = client.apex_rest();
//!
//! // Call a custom Apex REST endpoint
//! let result: MyResponse = apex.post("MyNamespace/MyEndpoint", &request_body).await?;
//!
//! // GET with query parameters
//! let data: Vec<Record> = apex.get_with_params("MyService/records", &[("limit", "10")]).await?;
//! ```

use crate::error::{HttpError, Result};
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::sync::Arc;

/// Generic Apex REST API handler.
///
/// Provides typed HTTP access to any `/services/apexrest/{path}` endpoint.
/// This handler is the building block for calling custom Apex REST classes
/// deployed in a Salesforce org.
///
/// Obtain a handler from [`ForceClient::apex_rest`](crate::client::ForceClient::apex_rest).
#[derive(Debug)]
pub struct ApexRestHandler<A: crate::auth::Authenticator> {
    inner: Arc<crate::session::Session<A>>,
}

impl<A: crate::auth::Authenticator> Clone for ApexRestHandler<A> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<A: crate::auth::Authenticator> ApexRestHandler<A> {
    /// Creates a new Apex REST handler wrapping the given session.
    #[must_use]
    pub(crate) fn new(inner: Arc<crate::session::Session<A>>) -> Self {
        Self { inner }
    }

    /// GET an Apex REST endpoint and deserialize the JSON response.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or the response cannot be deserialized.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let records: Vec<MyRecord> = client.apex_rest()
    ///     .get("MyNamespace/records")
    ///     .await?;
    /// ```
    pub async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        let url = self.inner.resolve_apex_rest_url(path).await?;
        let request = self.inner.get(&url).build().map_err(HttpError::from)?;
        self.inner
            .send_request_and_decode(request, "Apex REST GET failed")
            .await
    }

    /// GET an Apex REST endpoint with query parameters.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or the response cannot be deserialized.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let records: Vec<MyRecord> = client.apex_rest()
    ///     .get_with_params("MyService/records", &[("limit", "10")])
    ///     .await?;
    /// ```
    pub async fn get_with_params<T: DeserializeOwned>(
        &self,
        path: &str,
        params: &[(&str, &str)],
    ) -> Result<T> {
        let url = self.inner.resolve_apex_rest_url(path).await?;
        let request = self
            .inner
            .get(&url)
            .query(params)
            .build()
            .map_err(HttpError::from)?;
        self.inner
            .send_request_and_decode(request, "Apex REST GET failed")
            .await
    }

    /// POST to an Apex REST endpoint with a JSON body.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or the response cannot be deserialized.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let result: MyResponse = client.apex_rest()
    ///     .post("MyNamespace/MyEndpoint", &request_body)
    ///     .await?;
    /// ```
    pub async fn post<T: DeserializeOwned>(
        &self,
        path: &str,
        body: &(impl Serialize + Sync),
    ) -> Result<T> {
        let url = self.inner.resolve_apex_rest_url(path).await?;
        let request = self
            .inner
            .post(&url)
            .json(body)
            .build()
            .map_err(HttpError::from)?;
        self.inner
            .send_request_and_decode(request, "Apex REST POST failed")
            .await
    }

    /// POST to an Apex REST endpoint, returning raw `serde_json::Value`.
    ///
    /// Useful for endpoints with dynamic or unknown response shapes.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or the response is not valid JSON.
    pub async fn post_raw(
        &self,
        path: &str,
        body: &(impl Serialize + Sync),
    ) -> Result<serde_json::Value> {
        self.post(path, body).await
    }

    /// PATCH an Apex REST endpoint with a JSON body.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or the response cannot be deserialized.
    pub async fn patch<T: DeserializeOwned>(
        &self,
        path: &str,
        body: &(impl Serialize + Sync),
    ) -> Result<T> {
        let url = self.inner.resolve_apex_rest_url(path).await?;
        let request = self
            .inner
            .patch(&url)
            .json(body)
            .build()
            .map_err(HttpError::from)?;
        self.inner
            .send_request_and_decode(request, "Apex REST PATCH failed")
            .await
    }

    /// PUT to an Apex REST endpoint with a JSON body.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or the response cannot be deserialized.
    pub async fn put<T: DeserializeOwned>(
        &self,
        path: &str,
        body: &(impl Serialize + Sync),
    ) -> Result<T> {
        let url = self.inner.resolve_apex_rest_url(path).await?;
        let request = self
            .inner
            .put(&url)
            .json(body)
            .build()
            .map_err(HttpError::from)?;
        self.inner
            .send_request_and_decode(request, "Apex REST PUT failed")
            .await
    }

    /// DELETE an Apex REST endpoint.
    ///
    /// Expects a 2xx response with no body (typically 204 No Content).
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or returns a non-success status.
    pub async fn delete(&self, path: &str) -> Result<()> {
        let url = self.inner.resolve_apex_rest_url(path).await?;
        let request = self.inner.delete(&url).build().map_err(HttpError::from)?;
        self.inner
            .execute_and_check_success(request, "Apex REST DELETE failed")
            .await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::{MockAuthenticator, Must};
    use serde::Deserialize;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    async fn setup() -> (MockServer, crate::client::ForceClient<MockAuthenticator>) {
        let server = MockServer::start().await;
        let auth = MockAuthenticator::new("test_token", &server.uri());
        let client = crate::client::builder()
            .authenticate(auth)
            .build()
            .await
            .must();
        (server, client)
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct TestResponse {
        message: String,
        count: i32,
    }

    // ── URL resolution tests ─────────────────────────────────────────

    #[tokio::test]
    async fn test_resolve_apex_rest_url_simple_path() {
        let (server, client) = setup().await;
        let handler = client.apex_rest();
        let url = handler
            .inner
            .resolve_apex_rest_url("MyService")
            .await
            .must();
        assert_eq!(url, format!("{}/services/apexrest/MyService", server.uri()));
    }

    #[tokio::test]
    async fn test_resolve_apex_rest_url_strips_leading_slash() {
        let (server, client) = setup().await;
        let handler = client.apex_rest();
        let url = handler
            .inner
            .resolve_apex_rest_url("/MyService")
            .await
            .must();
        assert_eq!(url, format!("{}/services/apexrest/MyService", server.uri()));
    }

    #[tokio::test]
    async fn test_resolve_apex_rest_url_empty_path() {
        let (server, client) = setup().await;
        let handler = client.apex_rest();
        let url = handler.inner.resolve_apex_rest_url("").await.must();
        assert_eq!(url, format!("{}/services/apexrest", server.uri()));
    }

    #[tokio::test]
    async fn test_resolve_apex_rest_url_nested_path() {
        let (server, client) = setup().await;
        let handler = client.apex_rest();
        let url = handler
            .inner
            .resolve_apex_rest_url("SBQQ/ServiceRouter")
            .await
            .must();
        assert_eq!(
            url,
            format!("{}/services/apexrest/SBQQ/ServiceRouter", server.uri())
        );
    }

    #[tokio::test]
    async fn test_resolve_apex_rest_url_has_no_api_version() {
        let (_server, client) = setup().await;
        let handler = client.apex_rest();
        let url = handler
            .inner
            .resolve_apex_rest_url("MyService")
            .await
            .must();
        // Apex REST URLs must NOT contain /services/data/vXX.0/
        assert!(!url.contains("/services/data/"));
    }

    // ── Handler construction tests ───────────────────────────────────

    #[tokio::test]
    async fn test_handler_is_cloneable() {
        let (_server, client) = setup().await;
        let handler = client.apex_rest();
        let cloned = handler.clone();
        // Verify both handlers share the same session via Arc
        let debug_original = format!("{handler:?}");
        let debug_cloned = format!("{cloned:?}");
        assert_eq!(debug_original, debug_cloned);
    }

    #[tokio::test]
    async fn test_handler_is_debug() {
        let (_server, client) = setup().await;
        let handler = client.apex_rest();
        let debug = format!("{handler:?}");
        assert!(debug.contains("ApexRestHandler"));
    }

    // ── GET tests ────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_get_success() {
        let (server, client) = setup().await;

        Mock::given(method("GET"))
            .and(path("/services/apexrest/MyService"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "hello", "count": 42})),
            )
            .mount(&server)
            .await;

        let result: TestResponse = client.apex_rest().get("MyService").await.must();
        assert_eq!(result.message, "hello");
        assert_eq!(result.count, 42);
    }

    #[tokio::test]
    async fn test_get_with_params_success() {
        let (server, client) = setup().await;

        Mock::given(method("GET"))
            .and(path("/services/apexrest/MyService/records"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "filtered", "count": 5})),
            )
            .mount(&server)
            .await;

        let result: TestResponse = client
            .apex_rest()
            .get_with_params("MyService/records", &[("limit", "10")])
            .await
            .must();
        assert_eq!(result.message, "filtered");
        assert_eq!(result.count, 5);
    }

    // ── POST tests ───────────────────────────────────────────────────

    #[tokio::test]
    async fn test_post_success() {
        let (server, client) = setup().await;

        Mock::given(method("POST"))
            .and(path("/services/apexrest/MyService/create"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "created", "count": 1})),
            )
            .mount(&server)
            .await;

        let body = serde_json::json!({"name": "Test"});
        let result: TestResponse = client
            .apex_rest()
            .post("MyService/create", &body)
            .await
            .must();
        assert_eq!(result.message, "created");
        assert_eq!(result.count, 1);
    }

    #[tokio::test]
    async fn test_post_raw_success() {
        let (server, client) = setup().await;

        Mock::given(method("POST"))
            .and(path("/services/apexrest/MyService/dynamic"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"arbitrary": "data", "nested": {"key": 1}})),
            )
            .mount(&server)
            .await;

        let body = serde_json::json!({"input": "test"});
        let result = client
            .apex_rest()
            .post_raw("MyService/dynamic", &body)
            .await
            .must();
        assert_eq!(result["arbitrary"], "data");
        assert_eq!(result["nested"]["key"], 1);
    }

    // ── PATCH tests ──────────────────────────────────────────────────

    #[tokio::test]
    async fn test_patch_success() {
        let (server, client) = setup().await;

        Mock::given(method("PATCH"))
            .and(path("/services/apexrest/MyService/update"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "updated", "count": 1})),
            )
            .mount(&server)
            .await;

        let body = serde_json::json!({"name": "Updated"});
        let result: TestResponse = client
            .apex_rest()
            .patch("MyService/update", &body)
            .await
            .must();
        assert_eq!(result.message, "updated");
    }

    // ── PUT tests ────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_put_success() {
        let (server, client) = setup().await;

        Mock::given(method("PUT"))
            .and(path("/services/apexrest/MyService/replace"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "replaced", "count": 1})),
            )
            .mount(&server)
            .await;

        let body = serde_json::json!({"name": "Replaced"});
        let result: TestResponse = client
            .apex_rest()
            .put("MyService/replace", &body)
            .await
            .must();
        assert_eq!(result.message, "replaced");
    }

    // ── DELETE tests ─────────────────────────────────────────────────

    #[tokio::test]
    async fn test_delete_success() {
        let (server, client) = setup().await;

        Mock::given(method("DELETE"))
            .and(path("/services/apexrest/MyService/item/123"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;

        client.apex_rest().delete("MyService/item/123").await.must();
    }

    // ── Error handling tests ─────────────────────────────────────────

    #[tokio::test]
    async fn test_get_400_returns_error() {
        let (server, client) = setup().await;

        Mock::given(method("GET"))
            .and(path("/services/apexrest/MyService/bad"))
            .respond_with(ResponseTemplate::new(400).set_body_json(
                serde_json::json!([{"message": "Bad request", "errorCode": "INVALID_INPUT"}]),
            ))
            .mount(&server)
            .await;

        let result: Result<TestResponse> = client.apex_rest().get("MyService/bad").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_post_500_returns_error() {
        let (server, client) = setup().await;

        Mock::given(method("POST"))
            .and(path("/services/apexrest/MyService/fail"))
            .respond_with(
                ResponseTemplate::new(500)
                    .set_body_json(serde_json::json!({"message": "Internal error"})),
            )
            .mount(&server)
            .await;

        let body = serde_json::json!({"data": "test"});
        let result: Result<TestResponse> = client.apex_rest().post("MyService/fail", &body).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_delete_404_returns_error() {
        let (server, client) = setup().await;

        Mock::given(method("DELETE"))
            .and(path("/services/apexrest/MyService/notfound"))
            .respond_with(ResponseTemplate::new(404))
            .mount(&server)
            .await;

        let result = client.apex_rest().delete("MyService/notfound").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_namespaced_path() {
        let (server, client) = setup().await;

        Mock::given(method("GET"))
            .and(path("/services/apexrest/MyNS/SubService/action"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "namespaced", "count": 1})),
            )
            .mount(&server)
            .await;

        let result: TestResponse = client
            .apex_rest()
            .get("MyNS/SubService/action")
            .await
            .must();
        assert_eq!(result.message, "namespaced");
    }
}