use crate::client::Client;
use crate::errors::Error;
use reqwest::Response;
use serde::Serialize;
use std::collections::HashMap;
use std::fmt::Debug;
pub struct BulkApi {
pub(crate) client: Client,
}
impl BulkApi {
pub fn new(client: Client) -> Self {
BulkApi { client }
}
fn base_path(&self) -> String {
let instance_url = self.client.instance_url();
format!("{}/services/async/{}", instance_url, crate::client::DEFAULT_API_VERSION)
}
pub async fn create_job<T: Serialize + Debug>(&mut self, params: T) -> Result<Response, Error> {
let resource_url = format!("{}/job", self.base_path());
let headers = self.get_auth_headers().await?;
self.client.post_json(&resource_url, ¶ms, &headers).await
}
pub async fn add_batch_job(&mut self, job_id: &str, csv: Vec<u8>) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}/batch", self.base_path(), job_id);
let mut headers = self.get_auth_headers().await?;
headers.insert("Content-Type".to_string(), "text/csv".to_string());
self.client.post(&resource_url, csv, &headers).await
}
pub async fn get_batch(&mut self, job_id: &str, batch_id: &str) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}/batch/{}/", self.base_path(), job_id, batch_id);
let headers = self.get_auth_headers().await?;
self.client.get_raw(&resource_url, &headers).await
}
pub async fn close_job(&mut self, job_id: &str) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}", self.base_path(), job_id);
let headers = self.get_auth_headers().await?;
let mut params = HashMap::new();
params.insert("state", "Closed");
self.client.post_json(&resource_url, ¶ms, &headers).await
}
pub async fn get_job_details(&mut self, job_id: &str) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}", self.base_path(), job_id);
let headers = self.get_auth_headers().await?;
self.client.get_raw(&resource_url, &headers).await
}
pub async fn get_batches(&mut self, job_id: &str, content_type: &str) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}/batch", self.base_path(), job_id);
let mut headers = self.get_auth_headers().await?;
headers.insert("Content-Type".to_string(), content_type.to_string());
self.client.get_raw(&resource_url, &headers).await
}
pub async fn get_result_list(
&mut self,
job_id: &str,
batch_id: &str,
content_type: &str,
) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}/batch/{}/result", self.base_path(), job_id, batch_id);
let mut headers = self.get_auth_headers().await?;
headers.insert("Content-Type".to_string(), content_type.to_string());
self.client.get_raw(&resource_url, &headers).await
}
pub async fn get_result(&mut self, job_id: &str, batch_id: &str, result_id: &str) -> Result<Response, Error> {
let resource_url = format!(
"{}/job/{}/batch/{}/result/{}",
self.base_path(),
job_id,
batch_id,
result_id
);
let mut headers = self.get_auth_headers().await?;
headers.insert("Content-Type".to_string(), "application/xml".to_string());
self.client.get_raw(&resource_url, &headers).await
}
pub async fn abort_job(&mut self, job_id: &str) -> Result<Response, Error> {
let resource_url = format!("{}/job/{}", self.base_path(), job_id);
let mut headers = self.get_auth_headers().await?;
headers.insert("Content-Type".to_string(), "application/json".to_string());
let mut params = HashMap::new();
params.insert("state", "Aborted");
self.client.post_json(&resource_url, ¶ms, &headers).await
}
async fn get_auth_headers(&self) -> Result<HashMap<String, String>, Error> {
let token = self.client.access_token().await?;
let mut headers = HashMap::new();
headers.insert("X-SFDC-Session".to_string(), token);
Ok(headers)
}
}