caretly crate has an async client for tokio and a blocking client. Both share the same options, result and error types.
- Package:
caretlyon crates.io - API reference: docs.rs/caretly
- Source:
clients/rust
Install
main macro:
Feature flags
To use only the blocking client, without tokio in your dependency tree:
Cargo.toml
rustls-tls or native-tls on to reach https:// URLs. Without either, only http:// works, for example a self-hosted server on localhost.
Quickstart
Create a test key in the console and set it in your environment:render and render_pdf accept a &str, String, &[u8] or Vec<u8>.
Async and blocking
caretly::Client is async and runs on tokio. With the blocking feature, caretly::blocking::Client has the same methods without .await:
Render a page as PNG
A PNG holds one page. Choose it withpage, starting at 1:
Render several files
render_files sends the files as multipart/form-data. Each file starts from a fresh printer state, and pages follow the order of the files:
(name, content) pairs. File contents can be &str, String, &[u8] or Vec<u8>.
Render options
Build options withRenderOptions::new(), or pass Default::default() for the server defaults:
Unset options aren’t sent, so the server defaults apply.
Dpmm::try_from(n) converts a number and fails on anything other than 6, 8, 12 or 24.
page(0), an empty or too long idempotency key, and an empty file list fail with Error::InvalidArgument before anything is sent.
Read the result
RenderResult has public fields:
Handle errors
Every call returnsResult<_, caretly::Error>:
The API variants hold an
ApiError with status, message (the response body) and retry_after. Use Error::status(), Error::retry_after() and Error::api_error() to read them without matching.
Retries and idempotency
The client retries a429, a 503 or a connection error up to max_retries times, 2 by default. It waits Retry-After seconds when the server sends it. Otherwise it backs off 0.5 s, 1 s, 2 s, 4 s, then 8 s. Timeouts and other statuses aren’t retried.
When retries are on and you don’t set an idempotency key, the client generates a UUID v4 per call and sends it on every attempt, so a retried request is never billed twice.
Set your own key, such as an order number, to make retries across calls or processes safe too:
Configuration
ClientBuilder has the same methods. An empty API key sends no Authorization header, for self-hosted servers without keys.
The client builds its own reqwest client, so you can’t inject one. Choose the TLS backend with the rustls-tls and native-tls features.