pub struct SalesforceClient { /* private fields */ }Expand description
Enterprise-grade Salesforce API client
This client provides comprehensive features for production use:
- Automatic token refresh via OAuth
- Intelligent caching to reduce API calls
- Retry logic with exponential backoff
- Rate limiting to respect API quotas
- Automatic pagination for large result sets
- Full CRUD operations
- Type-safe query building
§Design Principles
- Enterprise-ready: Built-in retry, caching, and rate limiting
- Type-safe: Generic methods with compile-time guarantees
- Async-first: Non-blocking I/O throughout
- Observable: Comprehensive tracing for debugging
- Composable: Arc-based sharing for concurrent use
Implementations§
Source§impl SalesforceClient
impl SalesforceClient
Sourcepub fn new(config: ClientConfig) -> Self
pub fn new(config: ClientConfig) -> Self
Creates a new Salesforce API client with the given configuration
§Example
use salesforce_client::{SalesforceClient, ClientConfig};
let config = ClientConfig::new(
"https://yourinstance.salesforce.com",
"00D... your token",
);
let client = SalesforceClient::new(config);Sourcepub async fn with_oauth(credentials: OAuthCredentials) -> SfResult<Self>
pub async fn with_oauth(credentials: OAuthCredentials) -> SfResult<Self>
Create a client with OAuth credentials (automatic token refresh)
§Example
use salesforce_client::{SalesforceClient, OAuthCredentials};
let credentials = OAuthCredentials {
client_id: "your_client_id".to_string(),
client_secret: "your_client_secret".to_string(),
refresh_token: Some("your_refresh_token".to_string()),
username: None,
password: None,
};
// This will be implemented with TokenManager integration
// let client = SalesforceClient::with_oauth(credentials).await?;Sourcepub async fn query<T>(&self, soql: impl AsRef<str>) -> SfResult<Vec<T>>
pub async fn query<T>(&self, soql: impl AsRef<str>) -> SfResult<Vec<T>>
Execute a SOQL query with caching, retry, and rate limiting
This method automatically handles:
- Rate limiting (waits if limit exceeded)
- Caching (returns cached results if available)
- Retry logic (retries transient failures)
- Pagination (fetches only first page by default)
§Example
let accounts: Vec<Account> = client
.query("SELECT Id, Name FROM Account WHERE AnnualRevenue > 1000000")
.await?;Sourcepub async fn query_all<T>(&self, soql: impl AsRef<str>) -> SfResult<Vec<T>>where
T: DeserializeOwned + Serialize,
pub async fn query_all<T>(&self, soql: impl AsRef<str>) -> SfResult<Vec<T>>where
T: DeserializeOwned + Serialize,
Query with automatic pagination - fetches ALL results
Warning: This can consume significant memory for large result sets.
For queries returning >100k records, consider using query_paginated instead.
§Example
// Fetches all accounts, automatically handling pagination
let accounts: Vec<Account> = client
.query_all("SELECT Id, Name FROM Account")
.await?;Sourcepub async fn query_paginated<T>(
&self,
soql: &str,
) -> SfResult<PaginatedQuery<T>>where
T: DeserializeOwned,
pub async fn query_paginated<T>(
&self,
soql: &str,
) -> SfResult<PaginatedQuery<T>>where
T: DeserializeOwned,
Get a paginated query iterator for manual pagination control
This is the most memory-efficient way to handle large result sets.
§Example
let mut pages = client
.query_paginated::<Account>("SELECT Id FROM Account")
.await?;
while let Some(batch) = pages.next().await? {
for account in batch {
println!("{:?}", account);
}
}Sourcepub async fn insert<T: Serialize>(
&self,
sobject: &str,
data: &T,
) -> SfResult<InsertResponse>
pub async fn insert<T: Serialize>( &self, sobject: &str, data: &T, ) -> SfResult<InsertResponse>
Insert a new record
§Example
#[derive(Serialize)]
struct NewAccount {
#[serde(rename = "Name")]
name: String,
#[serde(rename = "Industry")]
industry: String,
}
let account = NewAccount {
name: "Acme Corporation".to_string(),
industry: "Technology".to_string(),
};
let response = client.insert("Account", &account).await?;
println!("Created account with ID: {}", response.id);Sourcepub async fn update<T: Serialize>(
&self,
sobject: &str,
id: &str,
data: &T,
) -> SfResult<()>
pub async fn update<T: Serialize>( &self, sobject: &str, id: &str, data: &T, ) -> SfResult<()>
Update an existing record
§Example
#[derive(Serialize)]
struct AccountUpdate {
#[serde(rename = "Name")]
name: String,
}
let update = AccountUpdate {
name: "Acme Corp (Updated)".to_string(),
};
client.update("Account", "001xx000003DGbX", &update).await?;
println!("Account updated successfully");Sourcepub async fn delete(&self, sobject: &str, id: &str) -> SfResult<()>
pub async fn delete(&self, sobject: &str, id: &str) -> SfResult<()>
Delete a record
§Example
client.delete("Account", "001xx000003DGbX").await?;
println!("Account deleted successfully");Sourcepub async fn upsert<T: Serialize>(
&self,
sobject: &str,
builder: UpsertBuilder,
data: &T,
) -> SfResult<InsertResponse>
pub async fn upsert<T: Serialize>( &self, sobject: &str, builder: UpsertBuilder, data: &T, ) -> SfResult<InsertResponse>
Upsert a record (insert or update based on external ID)
§Example
#[derive(Serialize)]
struct Account {
#[serde(rename = "Name")]
name: String,
}
let account = Account {
name: "Acme Corporation".to_string(),
};
let upsert = UpsertBuilder::new("External_Id__c", "EXT-12345");
let response = client.upsert("Account", upsert, &account).await?;
println!("Upserted account with ID: {}", response.id);Sourcepub async fn clear_cache(&self)
pub async fn clear_cache(&self)
Clear the query cache
Sourcepub fn config(&self) -> &ClientConfig
pub fn config(&self) -> &ClientConfig
Get the current configuration
Sourcepub fn rate_limit_status(&self) -> RateLimitStatus
pub fn rate_limit_status(&self) -> RateLimitStatus
Get rate limiter status
Trait Implementations§
Source§impl Clone for SalesforceClient
impl Clone for SalesforceClient
Source§fn clone(&self) -> SalesforceClient
fn clone(&self) -> SalesforceClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more