Crate espocrm_rs

source ·
Expand description

espocrm-rs

The espocrm-rs crate provides an API Client for EspoCRM. This client is based on the official PHP API client provided by the EspoCRM team. You can find this client here.

Getting started

To get started you’ll have to provide the URL where EspoCRM is located at. You will also have to set the way you want to authenticate with EspoCRM. This can be done in one of the following ways:

  • Username+Password
  • API Key
  • HMAC (Recommended)

The following example creates an EspoApiClient with HMAC authorization

use espocrm_rs::EspoApiClient;

let client = EspoApiClient::new("https://espocrm.example.com")
    .set_api_key("Your API Key here")
    .set_secret_key("Your API Secret")
    .build();

The following example creates an EspoApiClient with API Key authorization

use espocrm_rs::EspoApiClient;
let client = EspoApiClient::new("https://espocrm.example.com")
    .set_api_key("Your API Key here")
    .build();

The following example creates an EspoApiClient with Username+Password authorization. This is highly discouraged!

use espocrm_rs::EspoApiClient;
let client = EspoApiClient::new("https://espocrm.example.com")
    .set_username("Your Username here")
    .set_password("Your Password here")
    .build();

Making a GET request

To make a request, you need to know a couple things:

  • The request method to use
  • On what to perform the request
  • Optionally, any data needed for the request

Most of these things are laid out pretty well in the EspoCRM API documentation here

use espocrm_rs::{EspoApiClient, Params, Where, FilterType, Value, NoGeneric, Method};

let params = Params::default()
    .set_offset(0)
    .set_where(vec![
        Where {
            r#type: FilterType::IsTrue,
            attribute: "exampleField".to_string(),
            value: None
        },
        Where {
            r#type: FilterType::ArrayAnyOf,
            attribute: "exampleField2".to_string(),
            value: Some(Value::array(vec![
                Value::str("a"),
                Value::str("b"),
                Value::str("c")
            ]))
        }
    ])
    .build();

let client = EspoApiClient::new("https://espocrm.example.com")
    .set_secret_key("Your Secret Key")
    .set_api_key("Your api key")
    .build();

let result = client.request::<NoGeneric, &str>(Method::Get, "Contact", Some(params), None);

Making a POST, PUT or DELETE request

These are all similar in working. They’ll serialize your data into json using Serde’s serialize trait

use espocrm_rs::{EspoApiClient, Method};
use serde::Serialize;

#[derive(Serialize, Clone)]
struct MyData {
    some_value:         String,
    some_other_value:   i64
}

let client = EspoApiClient::new("https://espocrm.example.com")
    .set_secret_key("Your Secret Key")
    .set_api_key("Your api key")
    .build();

let data = MyData {
    some_value: "value".to_string(),
    some_other_value: 10
};

let result = client.request(Method::Post, "Contact", None, Some(data));

Macros

Macro to conditionally call [tracing::debug] if tracing is enabled
Macro to conditionally call [tracing::trace] if tracing is enabled

Structs

Enums

Type Definitions

Used to indicate the required GenericType is not needed Used when calling request() with the GET method