Struct Client

Source
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

Source

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

Source

pub fn sandbox(&self) -> bool

Get the sandbox configuration.

Source

pub fn set_sandbox(&mut self, sandbox: bool)

Have the client use sandbox URLs.

Source

pub fn timeout(&self) -> u64

Get the timeout (in seconds) for API requests.

Source

pub fn set_timeout(&mut self, timeout: u64)

Set the timeout for API requests. Default is 30 seconds.

Source

pub fn access_token(&self) -> Option<String>

Get the access token.

Source

pub fn api_domain(&self) -> Option<String>

Get the API domain URL.

Source

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

Source

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.

Source

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");
Source

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();
Source

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"),
    }
}
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Err = <U as TryFrom<T>>::Err

Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>

Source§

impl<T> ErasedDestructor for T
where T: 'static,