Crates.io | rust_sync_force |
lib.rs | rust_sync_force |
version | 0.3.2 |
source | src |
created_at | 2023-02-02 17:13:57.439964 |
updated_at | 2023-08-08 15:51:47.070527 |
description | Salesforce Sync REST API Client |
homepage | https://github.com/0cv/rust_sync_force |
repository | https://github.com/0cv/rust_sync_force |
max_upload_size | |
id | 774918 |
size | 141,094 |
Salesforce Client for Rust Sync. Support most Salesforce REST methods. Also supports Change Data Capture.
Rust async version is here https://github.com/tzmfreedom/rustforce
use rust_sync_force::{Client, Error};
use rust_sync_force::response::{QueryResponse, ErrorResponse};
use serde::Deserialize;
use std::env;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Account {
#[serde(rename = "attributes")]
attributes: Attribute,
id: String,
name: String,
}
#[derive(Deserialize, Debug)]
struct Attribute {
url: String,
#[serde(rename = "type")]
sobject_type: String,
}
fn main() -> Result<(), Error> {
let client_id = env::var("SFDC_CLIENT_ID").unwrap();
let client_secret = env::var("SFDC_CLIENT_SECRET").unwrap();
let username = env::var("SFDC_USERNAME").unwrap();
let password = env::var("SFDC_PASSWORD").unwrap();
let mut client = Client::new(client_id, client_secret);
client.login_with_credential(username, password)?;
let res: QueryResponse<Account> = client.query("SELECT Id, Name FROM Account WHERE id = '0012K00001drfGYQAY'".to_string())?;
println!("{:?}", res);
Ok(())
}
Username Password Flow
let mut client = Client::new(client_id, client_secret);
client.login_with_credential(username, password)?;
[WIP]Authorization Code Grant
let r = client.refresh("xxxx")?;
let r: Result<QueryResponse<Account>, Error> = client.query("SELECT Id, Name FROM Account")?;
let r: Result<QueryResponse<Account>, Error> = client.query_all("SELECT Id, Name FROM Account")?;
let r: Result<Account, Error> = client.find_by_id("Account", "{sf_id}")?;
let mut params = HashMap::new();
params.insert("Name", "hello rust");
let r = client.insert("Account", params)?;
println!("{:?}", r);
let account1 = Account {
name: "account name1".into(),
attributes: Attribute { sobject_type: "Account".into() },
};
let account2 = Account {
name: "account name2".into(),
attributes: Attribute { sobject_type: "Account".into() },
};
let r = client.inserts(true, vec![account1, account2])?;
let r = client.update("Account", "{sobject_id}", params)?;
let account = Account {
id: "account_id".into(),
name: "new_name".into(),
attributes: Attribute { sobject_type: "Account".into() },
};
let r = client.updates(true, vec![account])?;
let r = client.upsert("Account", "external_key_name", "external_key_value", params)?;
let account = Account {
exkey: "external_key_id".into(),
name: "new_name".into(),
attributes: Attribute { sobject_type: "Account".into() },
};
let r = client.upserts(true, "Account", "ExKey__c", vec![account])?;
let r = client.delete("Account", "{sobject_id}")?;
let r = client.deletes(true, vec!["account_id".into()])?;
let r = client.describe_global()?;
let r = client.describe("Account")?;
let versions = client.versions()?;
let r = client.search("FIND {Rust}")?;
let mut client = Client::new(client_id, client_secret);
client.login_with_credential(username, password)?;
let mut stream_client = rust_sync_force::stream::CometdClient::new(
client,
// listen to Account Change Event
vec!["/data/AccountChangeEvent".to_string()],
);
stream_client.init().expect("Could not init cometd client");
println!("Cometd client successfully initialized");
for response in stream_client.connect()? {
if let StreamResponse::Delivery(delivery) = response {
match serde_json::from_value::<SFMetadata>(delivery.data.clone()) {
Ok(data) => {
println!("Data: {:#?}", data);
// Here you should have your patterns matching your own objects
}
Err(err) => {
println!(
"SF delivery data could not be parsed: {:?}\nData:{:?}",
err, delivery
)
}
}
}
}
#[derive(Debug, Deserialize)]
pub struct SFChangeEventHeader {
pub commitNumber: usize,
pub commitUser: String,
pub sequenceNumber: usize,
pub entityName: String,
pub changeType: String,
pub commitTimestamp: usize,
pub recordIds: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct SFPayload {
pub LastModifiedDate: String,
pub ChangeEventHeader: SFChangeEventHeader,
}
#[derive(Debug, Deserialize)]
pub struct SFMetadata {
pub schema: String,
pub payload: SFPayload,
}