Skip to main content

api2convert/
lib.rs

1//! Official Rust SDK for the [API2Convert](https://www.api2convert.com) file-conversion API.
2//!
3//! One call uploads (or references a URL), starts the job, polls it to
4//! completion and hands you the result:
5//!
6//! ```no_run
7//! use api2convert::{Api2Convert, ConvertOptions};
8//!
9//! # fn main() -> Result<(), api2convert::Api2ConvertError> {
10//! let client = Api2Convert::new("YOUR_API_KEY")?;
11//!
12//! // Convert a local file and save the result into a directory.
13//! let result = client.convert("photo.heic", "jpg")?;
14//! let path = result.save("out/", None)?;
15//! println!("saved {}", path.display());
16//!
17//! // Convert a remote URL with target-specific options.
18//! let result = client.convert_with(
19//!     "https://example.com/input.png",
20//!     "jpg",
21//!     ConvertOptions::new().option("quality", 85),
22//! )?;
23//! let bytes = result.contents(None)?;
24//! # let _ = bytes;
25//! # Ok(())
26//! # }
27//! ```
28//!
29//! This crate is one of the official API2Convert SDKs (PHP, Python, Java,
30//! Node.js, Go, Ruby, Rust) that implement the same language-agnostic contract
31//! (`docs/SDK_CONTRACT.md`) and version together.
32//!
33//! ## Errors
34//!
35//! Every fallible call returns [`Result`], whose error is [`Api2ConvertError`].
36//! Match it to react to specific conditions:
37//!
38//! ```no_run
39//! # use api2convert::{Api2Convert, Api2ConvertError};
40//! # fn f(client: &Api2Convert) {
41//! match client.convert("in.png", "not-a-format") {
42//!     Ok(result) => { let _ = result; }
43//!     Err(Api2ConvertError::Validation(e)) => eprintln!("bad request: {}", e.message),
44//!     Err(Api2ConvertError::RateLimit { retry_after, .. }) => {
45//!         eprintln!("rate limited; retry after {:?}s", retry_after)
46//!     }
47//!     Err(e) => eprintln!("failed: {e}"),
48//! }
49//! # }
50//! ```
51//!
52//! ## Security
53//!
54//! Account key, per-job upload token and download password ride in custom
55//! `X-Api2convert-*` headers. Secret-bearing requests never follow redirects (only the
56//! self-contained, no-secret download path does), uploads use the per-job token
57//! (never the account key), and no secret ever appears in an error message. See
58//! `SECURITY.md`.
59
60#![forbid(unsafe_code)]
61
62mod client;
63mod config;
64mod convert_options;
65mod data;
66mod errors;
67mod models;
68mod redact;
69mod resources;
70mod result;
71mod transport;
72mod upload;
73mod version;
74mod webhook;
75
76pub mod cloud;
77pub mod enums;
78
79pub use client::{Api2Convert, Input};
80pub use cloud::{CloudInput, OutputTarget};
81pub use config::{ClientBuilder, API_KEY_ENV, DEFAULT_BASE_URL};
82pub use convert_options::{AsyncOptions, ConvertOptions};
83pub use errors::{Api2ConvertError, ApiErrorData, Result};
84pub use models::{Conversion, InputFile, Job, JobMessage, OutputFile, Preset, Status};
85pub use resources::{
86    ContractsResource, ConversionsResource, JobsResource, PresetsResource, StatsResource,
87};
88pub use result::{ConversionResult, FileDownload};
89pub use version::VERSION;
90pub use webhook::{WebhookEvent, WebhookVerifier};
91
92// The pluggable transport seam — implement [`HttpSender`] to bring your own
93// client or a test fake; [`Sleeper`] / [`Rng`] make backoff injectable.
94pub use transport::{Headers, HttpRequest, HttpResponse, HttpSender, Rng, Sleeper};