[−][src]Struct zoho_crm::Client
Handles making requests to v2 of the Zoho CRM API.
You can either create a client with a preset access token, or fetch a new one later on. This can be useful if you are keeping track of you access tokens in a database, for example. You will need an API client ID, secret, and refresh token.
You can read more information here: https://www.zoho.com/crm/developer/docs/api/oauth-overview.html
Example
You should create a Client
with the with_creds()
method.
use zoho_crm::Client; let client_id = "YOUR_CLIENT_ID"; let client_secret = "YOUR_CLIENT_SECRET"; let refresh_token = "YOUR_REFRESH_TOKEN"; let client = Client::with_creds( None, // access token None, // api domain String::from(client_id), String::from(client_secret), String::from(refresh_token) );
API methods will automatically fetch a new token if one has not been set. This token is then saved internally to be used on all future requests.
Methods
impl Client
[src]
pub fn with_creds(
access_token: Option<String>,
api_domain: Option<String>,
client_id: String,
client_secret: String,
refresh_token: String
) -> Client
[src]
access_token: Option<String>,
api_domain: Option<String>,
client_id: String,
client_secret: String,
refresh_token: String
) -> Client
Create a new client.
You can supply an optional access token and/or api domain. However, you must supply a client ID, secret, and refresh token.
impl Client
[src]
pub fn sandbox(&self) -> bool
[src]
Get the sandbox configuration.
pub fn set_sandbox(&mut self, sandbox: bool)
[src]
Have the client use sandbox URLs.
pub fn timeout(&self) -> u64
[src]
Get the timeout (in seconds) for API requests.
pub fn set_timeout(&mut self, timeout: u64)
[src]
Set the timeout for API requests. Default is 30 seconds.
pub fn access_token(&self) -> Option<String>
[src]
Get the access token.
pub fn api_domain(&self) -> Option<String>
[src]
Get the API domain URL.
pub fn abbreviated_access_token(&self) -> Option<String>
[src]
Get an abbreviated version of the access token. This is a (slightly) safer version of the access token should you need to print it out.
let token = "1000.ad8f97a9sd7f9a7sdf7a89s7df87a9s8.a77fd8a97fa89sd7f89a7sdf97a89df3"; assert_eq!("1000.ad8f..9df3", &client.abbreviated_access_token().unwrap());
impl Client
[src]
pub fn get_new_token(&mut self) -> Result<TokenRecord, ClientError>
[src]
Get a new access token from Zoho. Guarantees an access token when it returns
an Result::Ok
.
The access token is saved to the Client
, so you don't
need to retrieve the token and set it in different steps. But a copy
of it is returned by this method.
pub fn get<T: DeserializeOwned>(
&mut self,
module: &str,
id: &str
) -> Result<ApiGetResponse<T>, ClientError>
[src]
&mut self,
module: &str,
id: &str
) -> Result<ApiGetResponse<T>, ClientError>
Fetches a record from Zoho.
Zoho returns a data array with this method, even though that array will always be of length-1. We return the data array, so you must treat the response accordingly.
If an error occurred, and we are given a response code back, this method will return a
ClientError::ApiError
with the response code
and message. Otherwise, a ClientError::General
error will be returned with the raw response text.
Example
use zoho_crm::Client; #[derive(Deserialize)] struct Account { name: String, } let mut client = Client::with_creds(None, None, client_id, client_secret, refresh_token); let response = client.get::<Account>("Accounts", "ZOHO_ID_HERE").unwrap(); let account = response.data.get(0).unwrap(); assert_eq!(account.name, "Account name");
pub fn get_many<T: DeserializeOwned>(
&mut self,
module: &str,
params: Option<String>
) -> Result<ApiGetManyResponse<T>, ClientError>
[src]
&mut self,
module: &str,
params: Option<String>
) -> Result<ApiGetManyResponse<T>, ClientError>
Fetches a page of records from Zoho.
Zoho API function documentation: https://www.zoho.com/crm/developer/docs/api/get-records.html
Example
use zoho_crm::Client; #[derive(Deserialize)] struct Account { name: String, } let mut client = Client::with_creds(None, None, client_id, client_secret, refresh_token); let accounts = client.get_many::<Account>("Accounts", None).unwrap();
Example with parameters
use zoho_crm::{parse_params, Client}; #[derive(Deserialize)] struct Account { name: String, } let mut params: HashMap<&str, &str> = HashMap::new(); params.insert("cvid", "YOUR_VIEW_ID_HERE"); params.insert("page", "2"); params.insert("per_page", "50"); let params = parse_params(params).unwrap(); let accounts = client.get_many::<Account>("Accounts", Some(params)).unwrap();
pub fn insert<T>(
&mut self,
module: &str,
data: Vec<T>
) -> Result<ApiSuccessResponse, ClientError> where
T: Serialize,
[src]
&mut self,
module: &str,
data: Vec<T>
) -> Result<ApiSuccessResponse, ClientError> where
T: Serialize,
Insert multiple records in Zoho.
Zoho API function documentation: https://www.zoho.com/crm/developer/docs/api/insert-records.html
It is important to note that this method may mask errors with a successful response. That is because record specific errors will be shown alongside the record in the response. We do not want to assume this is an unsuccessful response, and so it is up to you to handle them.
The params
argument accepts any serializable data type.
let mut record: HashMap<&str, &str> = HashMap::new(); record.insert("name", "sample"); let response = zoho_client.insert("Accounts", vec![record]).unwrap(); for record in response.data { match record.code.as_str() { "SUCCESS" => println!("Record was successful"), _ => println!("Record was NOT successful"), } }
pub fn update_many<T>(
&mut self,
module: &str,
data: Vec<T>
) -> Result<ApiSuccessResponse, ClientError> where
T: Serialize,
[src]
&mut self,
module: &str,
data: Vec<T>
) -> Result<ApiSuccessResponse, ClientError> where
T: Serialize,
Updates multiple records in Zoho.
Zoho API function documentation: https://www.zoho.com/crm/developer/docs/api/update-records.html
It is important to note that this method may mask errors with a successful response. That is because record specific errors will be shown alongside the record in the response. We do not want to assume this is an unsuccessful response, and so it is up to you to handle them.
The params
argument accepts any serializable data type.
let mut record: HashMap<&str, &str> = HashMap::new(); record.insert("id", "ZOHO_RECORD_ID_HERE"); record.insert("name", "sample"); let response = zoho_client.update_many("Accounts", vec![record]).unwrap(); for record in response.data { match record.code.as_str() { "SUCCESS" => println!("Record was successful"), _ => println!("Record was NOT successful"), } }
Auto Trait Implementations
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
U: TryFrom<T>,