use crate::client::client::Client;
use crate::errors::Error;
use reqwest::Response;
use serde::Serialize;
use std::collections::HashMap;
use std::fmt::Debug;
#[derive(Default)]
pub struct BulkApi {
pub(crate) client: Client,
}
impl BulkApi {
pub fn new(client: Client) -> Self {
BulkApi { client }
}
fn base_path(&self) -> Result<String, Error> {
let instance_url = self
.client
.instance_url
.as_ref()
.ok_or(Error::NotLoggedIn)?;
let version = &self.client.version[1..];
Ok(format!("{}/services/async/{}", instance_url, 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()?;
self.client.post(resource_url, params, 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()?;
headers.push(("Content-Type".to_string(), "text/csv".to_string()));
self.client
.post_raw_buffer(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()?;
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()?;
let mut params = HashMap::new();
params.insert("state", "Closed");
self.client.post(resource_url, params, 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()?;
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()?;
headers.push(("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()?;
headers.push(("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()?;
headers.push(("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()?;
headers.push(("Content-Type".to_string(), "application/json".to_string()));
let mut params = HashMap::new();
params.insert("state", "Aborted");
self.client.post(resource_url, params, headers).await
}
fn get_auth_headers(&self) -> Result<Vec<(String, String)>, Error> {
let token = self
.client
.access_token
.as_ref()
.ok_or(Error::NotLoggedIn)?;
Ok(vec![
(
"X-SFDC-Session".to_string(),
token.value.clone(),
),
])
}
}
#[cfg(test)]
mod test;