apify_client/lib.rs
1//! # apify-client
2//!
3//! An idiomatic Rust client for the [Apify API](https://docs.apify.com/api/v2).
4//!
5//! It provides a resource-oriented interface that mirrors the official
6//! [JavaScript](https://github.com/apify/apify-client-js) and Python clients: start from
7//! an [`ApifyClient`], then drill down into resources (Actors, runs, datasets, key-value
8//! stores, request queues, tasks, schedules, webhooks, the store, users and logs).
9//!
10//! ## Quick start
11//!
12//! ```no_run
13//! use apify_client::ApifyClient;
14//!
15//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
16//! let client = ApifyClient::new("my-api-token");
17//!
18//! // Start an Actor and wait for it to finish.
19//! let run = client
20//! .actor("apify/hello-world")
21//! .call::<serde_json::Value>(None, Default::default(), None)
22//! .await?;
23//!
24//! // Read items from the run's default dataset.
25//! if let Some(dataset_id) = &run.default_dataset_id {
26//! let items = client
27//! .dataset(dataset_id)
28//! .list_items::<serde_json::Value>(Default::default())
29//! .await?;
30//! println!("Got {} items", items.items.len());
31//! }
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! ## Architecture
37//!
38//! - **Public interface**: [`ApifyClient`] and the resource clients in [`clients`].
39//! - **Replaceable transport**: the [`http_client::HttpBackend`] trait, with a default
40//! [`http_client::ReqwestBackend`]. Swap it via
41//! [`ApifyClientBuilder::http_backend`].
42//! - **Cross-cutting behaviour** (auth, `User-Agent`, retries with exponential backoff,
43//! timeouts) lives in [`http_client::HttpClient`] and is applied to every request.
44//!
45//! ## Cancellation
46//!
47//! The reference JavaScript client accepts an `AbortSignal` option on most methods so a caller
48//! can cancel an in-flight request. Rust futures are cancel-safe by construction: they do
49//! nothing until polled, so dropping one (e.g. via `tokio::select!`, or letting a
50//! `tokio::time::timeout` elapse) stops the underlying request without any API for it. This
51//! client therefore exposes no separate cancellation parameter — dropping the future returned
52//! by any method is the idiomatic equivalent.
53
54#![warn(missing_docs)]
55
56mod client;
57pub mod clients;
58pub mod common;
59pub mod error;
60pub mod http_client;
61pub mod models;
62mod version;
63
64pub use client::{ApifyClient, ApifyClientBuilder};
65pub use common::{ListOptions, PaginationList, QueryParams, StorageListOptions};
66pub use error::{ApiError, ApifyClientError, ApifyClientResult};
67pub use http_client::RequestCompression;
68pub use version::{API_SPEC_VERSION, CLIENT_VERSION};
69
70// Re-export the most commonly used option/parameter types for ergonomic access.
71pub use clients::actor::{ActorBuildOptions, ActorStartOptions};
72pub use clients::actor_collection::ActorListOptions;
73pub use clients::dataset::{DatasetDownloadOptions, DatasetListItemsOptions, DownloadItemsFormat};
74pub use clients::key_value_store::{GetRecordOptions, KeyValueStoreKeysIterator, ListKeysOptions};
75pub use clients::log::LogOptions;
76pub use clients::pagination::ListIterator;
77pub use clients::request_queue::{
78 BatchAddRequestsOptions, ListRequestsOptions, RequestQueueRequestsIterator,
79};
80pub use clients::run::{
81 LastRunOptions, RunChargeOptions, RunMetamorphOptions, RunResurrectOptions,
82};
83pub use clients::run_collection::RunListOptions;
84pub use clients::store_collection::{StoreActorIterator, StoreListOptions};
85
86// Compile-test the code snippets in the README and the external `docs/` pages so every
87// in-documentation code snippet stays valid and runnable. Pulling each Markdown file in as
88// a doctest source means `cargo test --doc` (the `Test examples` CI step) compiles every
89// `rust` fenced block; `no_run` blocks are compiled but not executed, runnable blocks run.
90#[doc = include_str!("../README.md")]
91#[cfg(doctest)]
92struct ReadmeDoctests;
93
94#[doc = include_str!("../docs/README.md")]
95#[cfg(doctest)]
96struct DocsReadmeDoctests;
97
98#[doc = include_str!("../docs/actors.md")]
99#[cfg(doctest)]
100struct DocsActorsDoctests;
101
102#[doc = include_str!("../docs/misc.md")]
103#[cfg(doctest)]
104struct DocsMiscDoctests;
105
106#[doc = include_str!("../docs/storages.md")]
107#[cfg(doctest)]
108struct DocsStoragesDoctests;
109
110#[doc = include_str!("../docs/runs.md")]
111#[cfg(doctest)]
112struct DocsRunsDoctests;
113
114#[doc = include_str!("../docs/builds.md")]
115#[cfg(doctest)]
116struct DocsBuildsDoctests;