Onspring API Rust SDK
The Rust SDK for the Onspring API is meant to simplify development in Rust for Onspring customers who want to build integrations with their Onspring instance.
Note: This is an unofficial SDK for the Onspring API. It was not built in consultation with Onspring Technologies LLC or a member of their development team.
This SDK was developed independently using Onspring's existing C# SDK, the Onspring API's swagger page, and API documentation as the starting point with the intention of making development of integrations done in Rust with an Onspring instance quicker and more convenient.
Dependencies
Rust
Requires Rust edition 2021 or later (Rust 1.56+).
Key Crates
| Crate | Purpose |
|---|---|
| reqwest | HTTP client for all API requests |
| serde / serde_json | JSON serialization and deserialization |
| tokio | Async runtime |
| chrono | Date and time types |
| uuid | UUID types for list item identifiers |
| thiserror | Error type derivation |
Installation
Add the SDK to your project using Cargo:
Or add it directly to your Cargo.toml:
[]
= "<latest>"
= { = "1", = ["full"] }
API Key
In order to successfully interact with the Onspring API you will need an API key. API keys are obtained by an Onspring user with permissions to at least Read API Keys for your instance via the following steps:
- Login to the Onspring instance.
- Navigate to Administration > Security > API Keys.
- On the list page, add a new API Key - this will require Create permissions - or click an existing API key to view its details.
- Click on the Developer Information tab.
- Copy the X-ApiKey Header value from this tab.
Important:
- An API Key must have a status of
Enabledin order to make authorized requests. - Each API Key must have an assigned Role. This role controls the permissions for requests made. If the API Key used does not have sufficient permissions the requests made won't be successful.
Permission Considerations
You can think of any API Key as another user in your Onspring instance and therefore it is subject to all the same permission considerations as any other user when it comes to its ability to access data in your instance. The API Key you use needs to have all the correct permissions within your instance to access the data requested. Things to think about in this context are role security, content security, and field security.
Start Coding
OnspringClient
The most common way to use the SDK is to create an OnspringClient instance and call its methods. The builder requires an API key and defaults to https://api.onspring.com as the base URL.
It is best practice to read these values in from environment variables or a configuration file for both flexibility and security purposes.
Example .env file:
API_KEY=000000ffffff000000ffffff/00000000-ffff-0000-ffff-000000000000
BASE_URL=https://api.onspring.com
Example constructing OnspringClient:
use OnspringClient;
use env;
let client = builder
.base_url
.build;
Client Configuration
By default when you construct an instance of the OnspringClient a new reqwest::Client will also be created with a 120-second timeout. You can customize the timeout or provide your own reqwest::Client:
use OnspringClient;
use Duration;
// Custom timeout
let client = builder
.timeout
.build;
// Custom reqwest client
let http_client = builder
.timeout
.build
.unwrap;
let client = builder
.http_client
.build;
Error Handling
All client methods return onspring::Result<T>, which is an alias for Result<T, OnspringError>. The OnspringError enum has the following variants:
Http- An HTTP transport error occurred (network issues, timeouts, etc.).Api { status_code, message }- The API returned a non-success status code.Serialization- A JSON serialization or deserialization error occurred.InvalidArgument- An invalid argument was provided to an SDK method.
match client.get_app.await
Full API Documentation
You may wish to refer to the full Onspring API documentation when determining which values to pass as parameters to some of the OnspringClient methods. There is also a swagger page that you can use for making exploratory requests.
Runnable Examples
The examples/ directory contains runnable examples that demonstrate each endpoint group against a real Onspring instance. They load configuration from a .env file in the project root.
Create a .env file with your test instance details:
API_BASE_URL=https://your-instance.onspring.com
SANDBOX_API_KEY=your-api-key
TEST_APP_ID=123
TEST_SURVEY_ID=456
TEST_TEXT_FIELD=789
TEST_RECORD=1
TEST_ATTACHMENT_FIELD=101
TEST_ATTACHMENT=202
TEST_IMAGE_FIELD=303
TEST_IMAGE=404
TEST_LIST_ID=505
Then run any example:
Code Examples
Note the following code snippets assume you've already instantiated an OnspringClient as shown in the OnspringClient section and that the code is running within an async context.
Connectivity
Verify connectivity
client.ping.await?;
println!;
Apps
Get Apps
Returns a paged collection of apps and/or surveys that can be paged through. By default the page size is 50 and page number is 1.
let res = client.list_apps.await?;
let apps = res.items.unwrap_or_default;
for app in &apps
You can set your own page size and page number (max is 1,000) as well.
use PagingRequest;
let paging = PagingRequest ;
let res = client.list_apps.await?;
let apps = res.items.unwrap_or_default;
for app in &apps
Get App By Id
Returns an Onspring app or survey according to provided id.
let app = client.get_app.await?;
println!;
Get Apps By Ids
Returns a collection of Onspring apps and/or surveys according to provided ids.
let res = client.batch_get_apps.await?;
let apps = res.items.unwrap_or_default;
for app in &apps
Fields
Get Field By Id
Returns an Onspring field according to provided id.
let field = client.get_field.await?;
println!;
Get Fields By Ids
Returns a collection of Onspring fields according to provided ids.
let res = client.batch_get_fields.await?;
let fields = res.items.unwrap_or_default;
for field in &fields
Get Fields By App Id
Returns a paged collection of fields that can be paged through. By default the page size is 50 and page number is 1.
let res = client.list_fields.await?;
let fields = res.items.unwrap_or_default;
for field in &fields
You can set your own page size and page number (max is 1,000) as well.
use PagingRequest;
let paging = PagingRequest ;
let res = client.list_fields.await?;
let fields = res.items.unwrap_or_default;
for field in &fields
Files
Get File Info By Id
Returns the Onspring file's metadata.
let file_info = client.get_file_info.await?;
println!;
Get File By Id
Returns the file itself.
use fs;
let file = client.get_file.await?;
println!;
println!;
if let Some = &file.file_name
Save File
use SaveFileRequest;
let request = SaveFileRequest ;
let res = client.upload_file.await?;
println!;
Delete File By Id
client.delete_file.await?;
println!;
Lists
Add Or Update List Value
To add a list value don't provide an id value.
use SaveListItemRequest;
let request = SaveListItemRequest ;
let res = client.save_list_item.await?;
println!;
To update a list value provide an id value.
use SaveListItemRequest;
use Uuid;
let item_id: Uuid = "35c79a46-04b8-4069-bbc1-161a175f962c".parse.unwrap;
let request = SaveListItemRequest ;
let res = client.save_list_item.await?;
println!;
Delete List Value
use Uuid;
let item_id: Uuid = "35c79a46-04b8-4069-bbc1-161a175f962c".parse.unwrap;
client.delete_list_item.await?;
println!;
Records
Get Records By App Id
Returns a paged collection of records that can be paged through. By default the page size is 50 and page number is 1.
let res = client.list_records.await?;
let records = res.items.unwrap_or_default;
for record in &records
You can set your own page size and page number (max is 1,000) as well. In addition to specifying what field values to return and in what format (Raw vs. Formatted) to return them.
use ;
let paging = PagingRequest ;
let res = client.list_records.await?;
let records = res.items.unwrap_or_default;
for record in &records
Get Record By Id
Returns an Onspring record based on the provided app and record ids.
let record = client.get_record.await?;
println!;
You can also specify what field values to return and in what format (Raw vs. Formatted) to return them.
use DataFormat;
let record = client.get_record.await?;
println!;
Get Records By Ids
Returns a collection of Onspring records based on the provided app id and record ids.
use BatchGetRecordsRequest;
let request = BatchGetRecordsRequest ;
let res = client.batch_get_records.await?;
let records = res.items.unwrap_or_default;
for record in &records
You can also specify what field values to return and in what format (Raw vs. Formatted) to return them.
use ;
let request = BatchGetRecordsRequest ;
let res = client.batch_get_records.await?;
let records = res.items.unwrap_or_default;
for record in &records
Query Records
Returns a paged collection of records based on a criteria that can be paged through. By default the page size is 50 and page number is 1.
use QueryRecordsRequest;
let request = QueryRecordsRequest ;
let res = client.query_records.await?;
let records = res.items.unwrap_or_default;
for record in &records
You can set your own page size and page number (max is 1,000) as well. In addition to specifying what field values to return and in what format (Raw vs. Formatted) to return them.
use ;
let request = QueryRecordsRequest ;
let paging = PagingRequest ;
let res = client.query_records.await?;
let records = res.items.unwrap_or_default;
for record in &records
For further details on constructing the filter parameter please refer to the documentation for the Onspring API.
Add or Update A Record
You can add a record by not providing a record id value. If successful will return the id of the added record.
use HashMap;
use SaveRecordRequest;
let mut fields = new;
fields.insert;
let request = SaveRecordRequest ;
let res = client.save_record.await?;
println!;
You can update a record by providing its id. If successful will return the id of record updated.
use HashMap;
use SaveRecordRequest;
let mut fields = new;
fields.insert;
let request = SaveRecordRequest ;
let res = client.save_record.await?;
println!;
Delete Record By Id
Delete an individual record based upon its id.
client.delete_record.await?;
println!;
Delete Records By Ids
Delete a batch of records based upon their ids.
use BatchDeleteRecordsRequest;
let request = BatchDeleteRecordsRequest ;
client.batch_delete_records.await?;
println!;
Reports
Get Report By Id
Returns the report for the provided id.
let report = client.get_report.await?;
println!;
You can also specify the format of the data in the report as well as whether you are requesting the report's data or its chart data.
use ;
let report = client.get_report.await?;
println!;
Get Reports By App Id
Returns a paged collection of reports that can be paged through. By default the page size is 50 and page number is 1.
let res = client.list_reports.await?;
let reports = res.items.unwrap_or_default;
for report in &reports
You can set your own page size and page number (max is 1,000) as well.
use PagingRequest;
let paging = PagingRequest ;
let res = client.list_reports.await?;
let reports = res.items.unwrap_or_default;
for report in &reports