busbar-sf-client 0.0.3

Core HTTP client infrastructure for Salesforce APIs with retry, compression, and rate limiting
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
//! High-level Salesforce client with typed HTTP methods.
//!
//! This module provides `SalesforceClient`, which combines credentials with
//! an HTTP client and provides typed JSON methods for API interactions.
//!
//! ## Security
//!
//! - Access tokens are redacted in Debug output
//! - Sensitive parameters are skipped in tracing spans

use serde::{de::DeserializeOwned, Serialize};
use tracing::instrument;

use crate::client::SfHttpClient;
use crate::config::ClientConfig;
use crate::error::{Error, ErrorKind, Result};
use crate::request::RequestBuilder;
use crate::DEFAULT_API_VERSION;

/// High-level Salesforce API client.
///
/// This client combines credentials with HTTP infrastructure and provides
/// typed methods for making API requests. It's designed to be used by
/// higher-level API-specific crates (sf-rest, sf-bulk, etc.).
///
/// ## Security
///
/// The access token is redacted in Debug output to prevent accidental
/// exposure in logs.
///
/// # Example
///
/// ```rust,ignore
/// use sf_client::SalesforceClient;
/// use sf_auth::SalesforceCredentials;
///
/// let creds = SalesforceCredentials::from_env()?;
/// let client = SalesforceClient::new(creds)?;
///
/// // GET with typed response
/// let user: UserInfo = client.get_json("/services/oauth2/userinfo").await?;
///
/// // POST with body and typed response
/// let result: CreateResult = client
///     .post_json("/services/data/v62.0/sobjects/Account", &account)
///     .await?;
/// ```
#[derive(Clone)]
pub struct SalesforceClient {
    http: SfHttpClient,
    instance_url: String,
    access_token: String,
    api_version: String,
}

impl std::fmt::Debug for SalesforceClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SalesforceClient")
            .field("instance_url", &self.instance_url)
            .field("access_token", &"[REDACTED]")
            .field("api_version", &self.api_version)
            .finish_non_exhaustive()
    }
}

impl SalesforceClient {
    /// Create a new Salesforce client with the given instance URL and access token.
    pub fn new(instance_url: impl Into<String>, access_token: impl Into<String>) -> Result<Self> {
        Self::with_config(instance_url, access_token, ClientConfig::default())
    }

    /// Create a new Salesforce client with custom configuration.
    pub fn with_config(
        instance_url: impl Into<String>,
        access_token: impl Into<String>,
        config: ClientConfig,
    ) -> Result<Self> {
        let http = SfHttpClient::new(config)?;
        Ok(Self {
            http,
            instance_url: instance_url.into().trim_end_matches('/').to_string(),
            access_token: access_token.into(),
            api_version: DEFAULT_API_VERSION.to_string(),
        })
    }

    /// Set the API version (e.g., "62.0").
    pub fn with_api_version(mut self, version: impl Into<String>) -> Self {
        self.api_version = version.into();
        self
    }

    /// Get the instance URL.
    pub fn instance_url(&self) -> &str {
        &self.instance_url
    }

    /// Get the access token.
    pub fn access_token(&self) -> &str {
        &self.access_token
    }

    /// Get the API version.
    pub fn api_version(&self) -> &str {
        &self.api_version
    }

    /// Build the full URL for a path.
    ///
    /// If the path starts with `/`, it's appended to the instance URL.
    /// Otherwise, it's assumed to be a full URL.
    pub fn url(&self, path: &str) -> String {
        if path.starts_with("http://") || path.starts_with("https://") {
            path.to_string()
        } else if path.starts_with('/') {
            format!("{}{}", self.instance_url, path)
        } else {
            format!("{}/{}", self.instance_url, path)
        }
    }

    /// Build the REST API URL for a path.
    ///
    /// Example: `rest_url("sobjects/Account")` -> `/services/data/v62.0/sobjects/Account`
    pub fn rest_url(&self, path: &str) -> String {
        let path = path.trim_start_matches('/');
        format!(
            "{}/services/data/v{}/{}",
            self.instance_url, self.api_version, path
        )
    }

    /// Build the Tooling API URL for a path.
    ///
    /// Example: `tooling_url("sobjects/ApexClass")` -> `/services/data/v62.0/tooling/sobjects/ApexClass`
    pub fn tooling_url(&self, path: &str) -> String {
        let path = path.trim_start_matches('/');
        format!(
            "{}/services/data/v{}/tooling/{}",
            self.instance_url, self.api_version, path
        )
    }

    /// Build the Metadata API URL (SOAP endpoint).
    pub fn metadata_url(&self) -> String {
        format!("{}/services/Soap/m/{}", self.instance_url, self.api_version)
    }

    /// Build the Bulk API 2.0 URL for a path.
    pub fn bulk_url(&self, path: &str) -> String {
        let path = path.trim_start_matches('/');
        format!(
            "{}/services/data/v{}/jobs/{}",
            self.instance_url, self.api_version, path
        )
    }

    // =========================================================================
    // Base HTTP Methods (with authentication)
    // =========================================================================

    /// Create a GET request builder with authentication.
    pub fn get(&self, url: &str) -> RequestBuilder {
        self.http.get(url).bearer_auth(&self.access_token)
    }

    /// Create a POST request builder with authentication.
    pub fn post(&self, url: &str) -> RequestBuilder {
        self.http.post(url).bearer_auth(&self.access_token)
    }

    /// Create a PATCH request builder with authentication.
    pub fn patch(&self, url: &str) -> RequestBuilder {
        self.http.patch(url).bearer_auth(&self.access_token)
    }

    /// Create a PUT request builder with authentication.
    pub fn put(&self, url: &str) -> RequestBuilder {
        self.http.put(url).bearer_auth(&self.access_token)
    }

    /// Create a DELETE request builder with authentication.
    pub fn delete(&self, url: &str) -> RequestBuilder {
        self.http.delete(url).bearer_auth(&self.access_token)
    }

    /// Execute a request and return the raw response.
    pub async fn execute(&self, request: RequestBuilder) -> Result<crate::Response> {
        self.http.execute(request).await
    }

    // =========================================================================
    // Typed JSON Methods
    // =========================================================================

    /// GET request with JSON response deserialization.
    #[instrument(skip(self), fields(url = %url))]
    pub async fn get_json<T: DeserializeOwned>(&self, url: &str) -> Result<T> {
        let full_url = self.url(url);
        let request = self.get(&full_url).header("Accept", "application/json");
        let response = self.http.execute(request).await?;
        response.json().await
    }

    /// GET request to REST API with JSON response.
    pub async fn rest_get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        self.get_json(&self.rest_url(path)).await
    }

    /// GET request to Tooling API with JSON response.
    pub async fn tooling_get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        self.get_json(&self.tooling_url(path)).await
    }

    /// POST request with JSON body and response.
    #[instrument(skip(self, body), fields(url = %url))]
    pub async fn post_json<T: DeserializeOwned, B: Serialize>(
        &self,
        url: &str,
        body: &B,
    ) -> Result<T> {
        let full_url = self.url(url);
        let request = self
            .post(&full_url)
            .header("Accept", "application/json")
            .json(body)?;
        let response = self.http.execute(request).await?;
        response.json().await
    }

    /// POST request to REST API with JSON body and response.
    pub async fn rest_post<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        self.post_json(&self.rest_url(path), body).await
    }

    /// POST request to Tooling API with JSON body and response.
    pub async fn tooling_post<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        self.post_json(&self.tooling_url(path), body).await
    }

    /// PATCH request with JSON body and optional response.
    #[instrument(skip(self, body), fields(url = %url))]
    pub async fn patch_json<B: Serialize>(&self, url: &str, body: &B) -> Result<()> {
        let full_url = self.url(url);
        let request = self.patch(&full_url).json(body)?;
        let response = self.http.execute(request).await?;

        // PATCH typically returns 204 No Content on success
        if response.status() == 204 || response.is_success() {
            Ok(())
        } else {
            Err(Error::new(ErrorKind::Http {
                status: response.status(),
                message: "PATCH request failed".to_string(),
            }))
        }
    }

    /// PATCH request to REST API with JSON body.
    pub async fn rest_patch<B: Serialize>(&self, path: &str, body: &B) -> Result<()> {
        self.patch_json(&self.rest_url(path), body).await
    }

    /// DELETE request.
    #[instrument(skip(self), fields(url = %url))]
    pub async fn delete_request(&self, url: &str) -> Result<()> {
        let full_url = self.url(url);
        let request = self.delete(&full_url);
        let response = self.http.execute(request).await?;

        // DELETE typically returns 204 No Content on success
        if response.status() == 204 || response.is_success() {
            Ok(())
        } else {
            Err(Error::new(ErrorKind::Http {
                status: response.status(),
                message: "DELETE request failed".to_string(),
            }))
        }
    }

    /// DELETE request to REST API.
    pub async fn rest_delete(&self, path: &str) -> Result<()> {
        self.delete_request(&self.rest_url(path)).await
    }

    // =========================================================================
    // Conditional Request Methods (ETags, If-Modified-Since)
    // =========================================================================

    /// GET request with If-None-Match header (ETag caching).
    /// Returns None if the resource hasn't changed (304 response).
    pub async fn get_json_if_changed<T: DeserializeOwned>(
        &self,
        url: &str,
        etag: &str,
    ) -> Result<Option<(T, Option<String>)>> {
        let full_url = self.url(url);
        let request = self.get(&full_url).if_none_match(etag);
        let response = self.http.execute(request).await?;

        if response.is_not_modified() {
            return Ok(None);
        }

        let new_etag = response.etag().map(|s| s.to_string());
        let data: T = response.json().await?;
        Ok(Some((data, new_etag)))
    }

    /// GET request with If-Modified-Since header.
    /// Returns None if the resource hasn't changed (304 response).
    pub async fn get_json_if_modified<T: DeserializeOwned>(
        &self,
        url: &str,
        since: &str,
    ) -> Result<Option<(T, Option<String>)>> {
        let full_url = self.url(url);
        let request = self.get(&full_url).if_modified_since(since);
        let response = self.http.execute(request).await?;

        if response.is_not_modified() {
            return Ok(None);
        }

        let last_modified = response.last_modified().map(|s| s.to_string());
        let data: T = response.json().await?;
        Ok(Some((data, last_modified)))
    }

    // =========================================================================
    // Query Helpers
    // =========================================================================

    /// Execute a SOQL query via REST API.
    pub async fn query<T: DeserializeOwned>(&self, soql: &str) -> Result<QueryResult<T>> {
        let encoded = urlencoding::encode(soql);
        let url = format!(
            "{}/services/data/v{}/query?q={}",
            self.instance_url, self.api_version, encoded
        );
        self.get_json(&url).await
    }

    /// Execute a SOQL query via Tooling API.
    pub async fn tooling_query<T: DeserializeOwned>(&self, soql: &str) -> Result<QueryResult<T>> {
        let encoded = urlencoding::encode(soql);
        let url = format!(
            "{}/services/data/v{}/tooling/query?q={}",
            self.instance_url, self.api_version, encoded
        );
        self.get_json(&url).await
    }

    /// Execute a SOQL query and automatically fetch all pages.
    pub async fn query_all<T: DeserializeOwned + Clone>(&self, soql: &str) -> Result<Vec<T>> {
        let mut all_records = Vec::new();
        let mut result: QueryResult<T> = self.query(soql).await?;

        all_records.extend(result.records);

        while let Some(ref next_url) = result.next_records_url {
            result = self.get_json(next_url).await?;
            all_records.extend(result.records);
        }

        Ok(all_records)
    }

    /// Execute a Tooling API query and automatically fetch all pages.
    pub async fn tooling_query_all<T: DeserializeOwned + Clone>(
        &self,
        soql: &str,
    ) -> Result<Vec<T>> {
        let mut all_records = Vec::new();
        let mut result: QueryResult<T> = self.tooling_query(soql).await?;

        all_records.extend(result.records);

        while let Some(ref next_url) = result.next_records_url {
            result = self.get_json(next_url).await?;
            all_records.extend(result.records);
        }

        Ok(all_records)
    }
}

/// Result of a SOQL query.
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct QueryResult<T> {
    /// Total number of records matching the query.
    #[serde(rename = "totalSize")]
    pub total_size: u64,

    /// Whether all records are returned (no more pages).
    pub done: bool,

    /// URL to fetch next batch of results.
    #[serde(rename = "nextRecordsUrl")]
    pub next_records_url: Option<String>,

    /// The records.
    pub records: Vec<T>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_url_building() {
        let client = SalesforceClient::new("https://na1.salesforce.com", "token123").unwrap();

        // Absolute paths
        assert_eq!(
            client.url("/services/oauth2/userinfo"),
            "https://na1.salesforce.com/services/oauth2/userinfo"
        );

        // Relative paths
        assert_eq!(
            client.url("services/oauth2/userinfo"),
            "https://na1.salesforce.com/services/oauth2/userinfo"
        );

        // Full URLs
        assert_eq!(
            client.url("https://other.com/path"),
            "https://other.com/path"
        );

        // REST API URL
        assert_eq!(
            client.rest_url("sobjects/Account"),
            "https://na1.salesforce.com/services/data/v62.0/sobjects/Account"
        );

        // Tooling API URL
        assert_eq!(
            client.tooling_url("sobjects/ApexClass"),
            "https://na1.salesforce.com/services/data/v62.0/tooling/sobjects/ApexClass"
        );

        // Bulk API URL
        assert_eq!(
            client.bulk_url("ingest"),
            "https://na1.salesforce.com/services/data/v62.0/jobs/ingest"
        );
    }

    #[test]
    fn test_api_version() {
        let client = SalesforceClient::new("https://na1.salesforce.com", "token")
            .unwrap()
            .with_api_version("60.0");

        assert_eq!(client.api_version(), "60.0");
        assert_eq!(
            client.rest_url("limits"),
            "https://na1.salesforce.com/services/data/v60.0/limits"
        );
    }

    #[test]
    fn test_trailing_slash_handling() {
        let client = SalesforceClient::new(
            "https://na1.salesforce.com/", // Trailing slash
            "token",
        )
        .unwrap();

        assert_eq!(client.instance_url(), "https://na1.salesforce.com");
        assert_eq!(
            client.rest_url("limits"),
            "https://na1.salesforce.com/services/data/v62.0/limits"
        );
    }
}