Rust for Uploading Documents in Rossum API

This Rust code shows how to upload a document automatically using Rossum API to a particular Rossum queue.

This Rust code demonstrates how to upload a document using Rossum API to a particular Rossum queue. Set the options at the beginning of the script – they store your credentials, and also the identifier of the queue to upload in. You can find out the queue id by looking at your main screen URL, it is the number 12345 in https://example.rossum.app/annotations/12345.

See the reference API documentation for more details about how to use the upload API.

[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"
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};
use reqwest::multipart;

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

fn authorize(username: &str, password: &str) -> Result<String, Box<std::error::Error>> {
    let login_url = "https://example.rossum.app/api/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 upload(queue_id: &str, file_path: &str, auth_token: &str) -> Result<(), Box<std::error::Error>> {
    let client = reqwest::Client::new();
    let url = format!("https://example.rossum.app/api/v1/queues/{}/upload", queue_id);
    let attachment = multipart::Form::new().file("content", file_path)?;    
    let response = client.post(&url)
        .header("Authorization", format!("token {}", auth_token))
        .multipart(attachment)
        .send();

    println!("{}", response?.text()?);
    Ok(())
}

fn main() -> Result<(), Box<std::error::Error>> {
    let username = "[email protected]";
    let password = "secret_password";
    let queue_id = "12345";
    let file_path = "invoice.pdf";
    
    let auth_token = authorize(username, password)?;
    
    upload(&queue_id, &file_path, &auth_token)?;
    
    Ok(())
}