Skip to main content

contentstack_api_client_rs/client/
delivery.rs

1pub mod entries;
2
3use reqwest::{
4    Client,
5    header::{HeaderMap, HeaderValue},
6};
7
8use crate::client::{
9    config::{ClientConfig, ClientOptions},
10    delivery::entries::Entries,
11};
12
13/// Async HTTP client for the Contentstack Content Delivery API (CDN).
14///
15/// Holds a connection pool and injects the required authentication headers
16/// on every request automatically.
17pub struct Delivery {
18    pub config: ClientConfig,
19    pub client: Client,
20}
21
22impl Delivery {
23    /// Creates a new `Delivery` client.
24    ///
25    /// # Arguments
26    ///
27    /// * `api_key` - Your stack's API key
28    /// * `delivery_token` - Environment-specific delivery token
29    /// * `environment` - The publishing environment (e.g. `"production"`)
30    /// * `opts` - Optional configuration overrides (region, timeout, max connections)
31    ///
32    /// # Example
33    ///
34    /// ```no_run
35    /// use contentstack_api_client_rs::Delivery;
36    ///
37    /// let client = Delivery::new("my_api_key", "my_delivery_token", "production", None);
38    /// ```
39    pub fn new(
40        api_key: &str,
41        delivery_token: &str,
42        environment: &str,
43        opts: Option<ClientOptions>,
44    ) -> Self {
45        let config = ClientConfig::delivery(api_key, delivery_token, environment, opts);
46        let mut headers = HeaderMap::new();
47
48        headers.insert(
49            "api_key",
50            HeaderValue::from_str(&config.api_key)
51                .expect("api_key contains invalid header characters"),
52        );
53
54        headers.insert(
55            "access_token",
56            HeaderValue::from_str(&config.delivery_token)
57                .expect("delivery_token contains invalid header characters"),
58        );
59
60        if let Some(ref env) = config.environment {
61            headers.insert(
62                "environment",
63                HeaderValue::from_str(env).expect("environment contains invalid header characters"),
64            );
65        }
66
67        let client = Client::builder()
68            .default_headers(headers)
69            .timeout(config.timeout)
70            .pool_max_idle_per_host(config.max_connections)
71            .build()
72            .expect("Failed to build HTTP client");
73
74        Self { config, client }
75    }
76
77    pub fn entries(&self) -> Entries<'_> {
78        Entries {
79            client: &self.client,
80        }
81    }
82}