API SDKs

Rust SDK

Generate report with Rust

Rust SDK to generate reports through the Carbone Cloud API in few lines of codes.

The source code is available on Github: https://github.com/carboneio/carbone-sdk-rust

Install

[dependencies]
carbone-sdk-rust = "1.0.0"

Quickstart

Try the following code to render a report in 10 seconds. Just insert your API key, the template ID you want to render, and the JSON data-set as string. Get your API key on your Carbone account: https://account.carbone.io/.

use std::env;

use carbone_sdk_rust::config::Config;
use carbone_sdk_rust::carbone::Carbone;
use carbone_sdk_rust::types::{ApiJsonToken, JsonData};
use carbone_sdk_rust::template::TemplateId;

use carbone_sdk_rust::errors::CarboneError;

use std::fs::File;
use std::io::Write

#[tokio::main]
async fn main() -> Result<(), CarboneError> {

    let token = "Token";

    let config: Config = Default::default();

    let api_token = ApiJsonToken::new(token.to_string())?;

    let json_data_value = String::from(r#"
        {
            "data" : {
                "firstname" : "John",
                "lastname" : "Wick"
            },
            "convertTo" : "odt"
        }
    "#);

    let json_data = JsonData::new(json_data_value)?;

    let template_id = TemplateId::new("YourTemplateId".to_string())?;

    let carbone = Carbone::new(&config, Some(&api_token))?;

    let report_content = match carbone.generate_report_with_template_id(template_id, json_data).await {
        Ok(v) => v,
        Err(e) => panic!("{}", e.to_string())
    };

    let mut output_file = File::create("report.pdf").expect("Failed to create file");

    if let Err(e) = output_file.write_all(&report_content) {
        eprintln!("Failed to write to file: {:?}", e);
    }

    Ok(())
}

SDK Constructor

Example of a new SDK instance for Carbone Cloud: Get your API key on your Carbone account: https://account.carbone.io/.

// For Carbone Cloud, provide your API Access Token as first argument:
let token = "Token";
let config: Config = Default::default();
let api_token = ApiJsonToken::new(token.to_string())?;
let carbone = Carbone::new(&config, Some(&api_token))?;

Example of a new SDK instance for Carbone On-premise or Carbone On-AWS:

// Define the URL of your Carbone On-premise Server or AWS EC2 URL:
let config: Config = Config::new("ON_PREMISE_URL".to_string(), "api_time_out_in_sec_in_u64", ApiVersion::new("4".to_string()).expect("REASON")).expect("REASON");
let carbone = Carbone::new(&config, None)?;

Generate and Download Document

Generate a document from a local template file:

pub async fn generate_report( &self, template_name: String, template_data: Vec<u8>, json_data: JsonData, payload: Option<&str>, salt: Option<&str>);

Arguments details:

Or, Generate a document from a template ID:

pub async fn pub async fn generate_report_with_template_id( &self, template_id: TemplateId, json_data: JsonData);

Argument details:

Add Template

pub async fn upload_template(&self,file_name: &str,file_content: Vec<u8>,salt: Option<&str>);

Add a template as file-content Vec and the function return the template ID as String.

Delete Template

pub async fn delete_template(&self, template_id: TemplateId);

Delete a template by providing a template ID as template_id, and it returns whether the request succeeded as a Boolean.