pub struct Client { /* private fields */ }
Expand description
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.
Implementations§
Source§impl Client
impl Client
Sourcepub fn with_creds(
access_token: Option<String>,
api_domain: Option<String>,
client_id: String,
client_secret: String,
refresh_token: String,
) -> Client
pub fn with_creds( 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.
Source§impl Client
impl Client
Sourcepub fn set_sandbox(&mut self, sandbox: bool)
pub fn set_sandbox(&mut self, sandbox: bool)
Have the client use sandbox URLs.
Sourcepub fn set_timeout(&mut self, timeout: u64)
pub fn set_timeout(&mut self, timeout: u64)
Set the timeout for API requests. Default is 30 seconds.
Sourcepub fn access_token(&self) -> Option<String>
pub fn access_token(&self) -> Option<String>
Get the access token.
Sourcepub fn api_domain(&self) -> Option<String>
pub fn api_domain(&self) -> Option<String>
Get the API domain URL.
Sourcepub fn abbreviated_access_token(&self) -> Option<String>
pub fn abbreviated_access_token(&self) -> Option<String>
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());
Source§impl Client
impl Client
Sourcepub fn get_new_token(&mut self) -> Result<TokenRecord, ClientError>
pub fn get_new_token(&mut self) -> Result<TokenRecord, ClientError>
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.
Sourcepub fn get<T: DeserializeOwned>(
&mut self,
module: &str,
id: &str,
) -> Result<ApiGetResponse<T>, ClientError>
pub fn get<T: DeserializeOwned>( &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");
Sourcepub fn get_many<T: DeserializeOwned>(
&mut self,
module: &str,
params: Option<String>,
) -> Result<ApiGetManyResponse<T>, ClientError>
pub fn get_many<T: DeserializeOwned>( &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();
Sourcepub fn insert<T>(
&mut self,
module: &str,
data: Vec<T>,
) -> Result<ApiSuccessResponse, ClientError>where
T: Serialize,
pub fn insert<T>(
&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"),
}
}
Sourcepub fn update_many<T>(
&mut self,
module: &str,
data: Vec<T>,
) -> Result<ApiSuccessResponse, ClientError>where
T: Serialize,
pub fn update_many<T>(
&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 Freeze for Client
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more