Rust for Exporting Data from Rossum API

This Rust code shows how to download JSON data captured in a given Rossum queue for the previous calendar day.

This Rust code shows how to download JSON data captured in a given queue for the previous calendar day – as part of the regular export routine. Update the parameters in the main() function based on your scenario.

The export API is more powerful - you can filter by different criteria or download the data e.g. as a XML instead - see the full docs.

[package]
name = "rossum_api_examples"
version = "0.1.0"
authors = ["Bohumir Zamecnik <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = "^0.9"
serde = "^1"
serde_derive = "^1"
serde_json = "^1"
chrono = "^0.4"
// Rossum API Export in Rust

extern crate chrono;
extern crate reqwest;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use std::collections::HashMap;
use chrono::{DateTime, Duration, Utc};

#[derive(Debug, Deserialize)]
struct LoginResponse {
    key: String,
}

fn authorize(username: &str, password: &str) -> Result<String, Box<std::error::Error>> {
    let login_url = "https://api.elis.rossum.ai/v1/auth/login";
    let mut credentials = HashMap::new();
    credentials.insert("username", username);
    credentials.insert("password", password);
    
    let client = reqwest::Client::new();
    let response = client.post(login_url)
        .json(&credentials)
        .send();
    let login_response = response?.json::<LoginResponse>()?;
    let auth_token = login_response.key;
    
    Ok(auth_token)
}

fn export(queue_id: &str, date_start: &str, date_end: &str, auth_token: &str) -> Result<String, Box<std::error::Error>> {    
    let client = reqwest::Client::new();
    let url = format!("https://api.elis.rossum.ai/v1/queues/{}/export?format=json&exported_at_after={}&exported_at_before={}&page_size=100&page=1", queue_id, date_start, date_end);
    let response = client.get(&url)
        .header("Authorization", format!("token {}", auth_token))
        .send();
    let content = response?.text()?;
    Ok(content)
}

fn export_today(queue_id: &str, auth_token: &str) -> Result<String, Box<std::error::Error>> {
    let today: DateTime<Utc> = Utc::now();
    let yesterday = today - Duration::days(1);
    let date_start = yesterday.format("%Y-%m-%d").to_string();
    let date_end = today.format("%Y-%m-%d").to_string();
    let exported = export(&queue_id, &date_start, &date_end, &auth_token)?;
    Ok(exported)
}

fn main() -> Result<(), Box<std::error::Error>> {
    let username = "[email protected]";
    let password = "secret_password";
    let queue_id = "12345";
    
    let auth_token = authorize(username, password)?;
    let exported = export_today(&queue_id, &auth_token)?;
    
    println!("{}", exported);
    
    Ok(())
}

This example will export at most 100 documents. To export more documents, you need to modify the code download multiple pages one by one, until it reaches the page count (or it can simply stop when an empty set of results is finally downloaded).