smallpict 0.0.8

Official Rust SDK for SmallPict Image Optimization API — next-gen compression (AVIF, WebP), Edge CDN delivery, and cache purging.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Blocking client for code that is not async. Do not build or use it inside a tokio runtime:
//! `reqwest::blocking` panics there. Use [`crate::Client`] from async code, or wrap the call in
//! `tokio::task::spawn_blocking`.

use crate::errors::{
    job_failed, job_timeout, reject_url_source, upload_failed, SecretRedacted, SmallPictError,
};
use crate::models::{
    check_folder, passthrough, FallbackMode, JobStatusResult, OptimizeOptions, OptimizeResult,
    PurgeResponse, PurgeType, QueuedJob, QuotaResponse,
};
use crate::request;
use bytes::Bytes;
use reqwest::blocking::Client as ReqwestBlockingClient;
use reqwest::header::{CONTENT_LENGTH, CONTENT_TYPE};
use serde_json::Value;
use std::env;
use std::fs;
use std::path::Path;
use std::time::{Duration, Instant};

/// Synchronous (blocking) client for interacting with the SmallPict API.
#[derive(Clone, Debug)]
pub struct BlockingClient {
    pub(crate) api_key: SecretRedacted<String>,
    pub(crate) secret_key: SecretRedacted<String>,
    pub(crate) base_url: String,
    pub(crate) timeout: Duration,
    pub(crate) max_retries: u32,
    pub(crate) max_retry_delay: Duration,
    pub(crate) fallback_mode: FallbackMode,
    pub(crate) poll_interval: Duration,
    pub(crate) wait_timeout: Duration,
    pub(crate) http_client: ReqwestBlockingClient,
}

impl BlockingClient {
    /// Create a new synchronous Client with default options. The secret key is read from
    /// `SMALLPICT_SECRET_KEY`; use [`BlockingClient::builder`] to pass it explicitly.
    pub fn new(api_key: impl Into<String>) -> Result<Self, SmallPictError> {
        BlockingClientBuilder::new().api_key(api_key).build()
    }

    /// Obtain a [`BlockingClientBuilder`] to configure blocking client options.
    pub fn builder() -> BlockingClientBuilder {
        BlockingClientBuilder::new()
    }

    /// Optimizes raw in-memory image bytes synchronously.
    ///
    /// Queues the job, PUTs the bytes to the pre-signed `upload_url` the API returns, and
    /// polls the job status every `poll_interval` until it is completed. Returns the finished
    /// job (`url`, `format`, `bytes_saved`, `mode_applied`, `quality_applied`, `folder`).
    /// A retry after a 5xx may create a second job until the server honours `Idempotency-Key`.
    ///
    /// Errors: `SmallPictError::Optimize` with code `job_failed` (the server's message,
    /// `details.job_id`) or `upload_failed` (the storage refused the PUT), and
    /// `SmallPictError::Timeout` naming the job when it is still running after `wait_timeout`.
    /// Optimizes raw in-memory image bytes; see [`Self::optimize_bytes`] to avoid the copy.
    pub fn optimize(
        &self,
        data: &[u8],
        options: Option<OptimizeOptions>,
    ) -> Result<OptimizeResult, SmallPictError> {
        self.optimize_bytes(Bytes::copy_from_slice(data), options)
    }

    /// Like [`Self::optimize`] but takes ownership of the buffer, so the upload sends it without a copy.
    pub fn optimize_bytes(
        &self,
        data: Bytes,
        options: Option<OptimizeOptions>,
    ) -> Result<OptimizeResult, SmallPictError> {
        let mut opts = options.unwrap_or_default();
        opts.mode = opts.wire_mode()?;
        if let Some(ref folder) = opts.folder {
            check_folder(folder)?;
        }
        let filename = opts.filename.clone().unwrap_or_else(|| "image.jpg".into());
        let mime_type = opts
            .mime_type
            .clone()
            .or_else(|| opts.content_type.clone())
            .or_else(|| crate::mime::detect_mime(&data).map(str::to_string))
            .unwrap_or_else(|| "image/jpeg".into());

        let payload = serde_json::json!({
            "filename": filename,
            "mime_type": mime_type,
            "content_type": mime_type,
            "filesize": data.len(),
            "options": serde_json::to_value(&opts).map_err(SmallPictError::from)?,
        });

        let queued = match self.request(
            reqwest::Method::POST,
            "/v1/optimize",
            Some(payload),
            opts.idempotency_key.as_deref(),
        ) {
            Ok(val) => serde_json::from_value::<QueuedJob>(val)?,
            Err(e) => {
                if e.is_quota_exceeded() && self.fallback_mode == FallbackMode::Passthrough {
                    return Ok(passthrough(&data, &mime_type));
                }
                return Err(e);
            }
        };

        // The worker waits 30 s from the request for the object: upload now, poll after.
        self.upload(&queued.upload_url, data.clone(), &mime_type)?;

        let deadline = Instant::now() + self.wait_timeout;
        loop {
            std::thread::sleep(
                self.poll_interval
                    .min(deadline.saturating_duration_since(Instant::now())),
            );
            let st = self.get_job_status(&queued.job_id)?;
            match st.status.as_str() {
                "completed" => return Ok(OptimizeResult::from_status(st, data.len() as u64)),
                "failed" => return Err(job_failed(&st.job_id, st.error.as_ref())),
                _ if Instant::now() >= deadline => {
                    return Err(job_timeout(&queued.job_id, self.wait_timeout))
                }
                _ => {}
            }
        }
    }

    /// PUTs the bytes to the pre-signed `upload_url` with only `Content-Type` and
    /// `Content-Length`: S3 refuses other signed headers. No retry: the worker stops
    /// waiting for the object 30 s after the job was queued.
    fn upload(&self, url: &str, data: Bytes, mime_type: &str) -> Result<(), SmallPictError> {
        let resp = self
            .http_client
            .put(url)
            .header(CONTENT_TYPE, mime_type)
            .header(CONTENT_LENGTH, data.len())
            .body(data)
            .timeout(self.timeout)
            .send()
            .map_err(|e| SmallPictError::Network {
                source: e.without_url(),
            })?;
        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().unwrap_or_default();
            return Err(upload_failed(status.as_u16(), &body));
        }
        Ok(())
    }

    /// Reads an image file from local disk and performs synchronous optimization. An
    /// `http(s)://` path returns `SmallPictError::Validation`: the API does not fetch URLs.
    pub fn optimize_file(
        &self,
        path: impl AsRef<Path>,
        options: Option<OptimizeOptions>,
    ) -> Result<OptimizeResult, SmallPictError> {
        let path = path.as_ref();
        reject_url_source(path)?;
        let bytes = fs::read(path)?;

        let mut opts = options.unwrap_or_default();
        if opts.filename.is_none() {
            if let Some(file_stem) = path.file_name().and_then(|s| s.to_str()) {
                opts.filename = Some(file_stem.to_string());
            }
        }

        self.optimize_bytes(Bytes::from(bytes), Some(opts))
    }

    /// Retrieves real-time account quota usage synchronously.
    pub fn get_quota(&self) -> Result<QuotaResponse, SmallPictError> {
        let val = self.request(reqwest::Method::GET, "/v1/quota", None, None)?;
        serde_json::from_value(val).map_err(SmallPictError::from)
    }

    /// Invalidates cached assets across global Edge CDN nodes synchronously.
    pub fn purge_cdn(
        &self,
        urls: &[impl AsRef<str>],
        purge_type: PurgeType,
    ) -> Result<PurgeResponse, SmallPictError> {
        let url_list: Vec<String> = urls.iter().map(|u| u.as_ref().to_string()).collect();
        let payload = serde_json::json!({
            "purge_type": purge_type.to_string(),
            "urls": url_list,
        });

        let val = self.request(reqwest::Method::POST, "/v1/purge", Some(payload), None)?;
        serde_json::from_value(val).map_err(SmallPictError::from)
    }

    /// Purges every file delivered under one upload folder (`purge_type: "folder"`).
    /// A malformed folder returns `SmallPictError::Validation` without an HTTP call.
    /// The server limits folder purges per hour; past the limit it answers 429
    /// `purge_rate_limited` (`SmallPictError::RateLimit`), which is not retried.
    pub fn purge_folder(&self, folder: &str) -> Result<PurgeResponse, SmallPictError> {
        check_folder(folder)?;
        let payload = serde_json::json!({
            "purge_type": "folder",
            "folder": folder,
        });

        let val = self.request(reqwest::Method::POST, "/v1/purge", Some(payload), None)?;
        serde_json::from_value(val).map_err(SmallPictError::from)
    }

    /// Verifies if the configured API key is valid synchronously.
    pub fn validate_key(&self) -> bool {
        self.get_quota().is_ok()
    }

    /// Polls current progress of an asynchronous image conversion task synchronously.
    pub fn get_job_status(&self, job_id: &str) -> Result<JobStatusResult, SmallPictError> {
        if job_id.is_empty() {
            return Err(SmallPictError::Validation {
                message: "job_id is required".into(),
                details: None,
            });
        }

        let path = format!("/v1/optimize/status?job_id={}", urlencoding::encode(job_id));
        let val = self.request(reqwest::Method::GET, &path, None, None)?;
        serde_json::from_value(val).map_err(SmallPictError::from)
    }

    pub(crate) fn request(
        &self,
        method: reqwest::Method,
        path: &str,
        payload: Option<Value>,
        idempotency_key: Option<&str>,
    ) -> Result<Value, SmallPictError> {
        let prepared = request::prepare(&self.base_url, path, payload.as_ref(), idempotency_key)?;
        let mut attempt = 0;

        while attempt <= self.max_retries {
            attempt += 1;
            let headers =
                request::headers(&self.api_key.0, &self.secret_key.0, &method, &prepared)?;
            let mut req = self
                .http_client
                .request(method.clone(), &prepared.url)
                .headers(headers)
                .timeout(self.timeout);
            if let Some(ref b) = prepared.body {
                req = req.body(b.clone());
            }

            let resp = match req.send() {
                Ok(r) => r,
                Err(e) => {
                    match request::transport_retry(attempt, self.max_retries, self.max_retry_delay)
                    {
                        Some(delay) => {
                            std::thread::sleep(delay);
                            continue;
                        }
                        None => return Err(SmallPictError::Network { source: e }),
                    }
                }
            };
            let status = resp.status();
            let resp_headers = resp.headers().clone();
            let body = resp.bytes().map_err(SmallPictError::from)?;
            let response = request::Response::new(status, &resp_headers, body);

            match request::classify(response, attempt, self.max_retries, self.max_retry_delay) {
                request::Outcome::Retry(delay) => std::thread::sleep(delay),
                request::Outcome::Done(result) => return result,
            }
        }

        Err(SmallPictError::Timeout {
            message: "Request failed after maximum retries".into(),
        })
    }
}

/// Builder pattern for configuring and instantiating a [`BlockingClient`].
#[derive(Debug, Clone)]
pub struct BlockingClientBuilder {
    api_key: Option<String>,
    secret_key: Option<String>,
    base_url: Option<String>,
    timeout: Duration,
    max_retries: u32,
    max_retry_delay: Duration,
    fallback_mode: FallbackMode,
    poll_interval: Duration,
    wait_timeout: Duration,
}

impl Default for BlockingClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl BlockingClientBuilder {
    pub fn new() -> Self {
        Self {
            api_key: env::var("SMALLPICT_API_KEY").ok(),
            secret_key: env::var("SMALLPICT_SECRET_KEY").ok(),
            base_url: env::var("SMALLPICT_BASE_URL").ok(),
            timeout: Duration::from_secs(30),
            max_retries: 3,
            max_retry_delay: Duration::from_secs(30),
            fallback_mode: FallbackMode::Throw,
            poll_interval: Duration::from_secs(1),
            wait_timeout: Duration::from_secs(120),
        }
    }

    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    pub fn secret_key(mut self, secret: impl Into<String>) -> Self {
        self.secret_key = Some(secret.into());
        self
    }

    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = Some(url.into().trim_end_matches('/').to_string());
        self
    }

    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    pub fn max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }

    /// Longest wait between attempts, whatever `Retry-After` says (default 30 s).
    pub fn max_retry_delay(mut self, delay: Duration) -> Self {
        self.max_retry_delay = delay;
        self
    }

    pub fn fallback_mode(mut self, mode: FallbackMode) -> Self {
        self.fallback_mode = mode;
        self
    }

    /// How often `optimize()` polls the job status while waiting (default 1 s).
    pub fn poll_interval(mut self, interval: Duration) -> Self {
        self.poll_interval = interval;
        self
    }

    /// How long `optimize()` waits for the job to finish before `SmallPictError::Timeout`
    /// (default 120 s). The job keeps running on the server.
    pub fn wait_timeout(mut self, timeout: Duration) -> Self {
        self.wait_timeout = timeout;
        self
    }

    pub fn build(self) -> Result<BlockingClient, SmallPictError> {
        let key = self.api_key.ok_or_else(|| SmallPictError::Validation {
            message: "Missing required SmallPict API key. Provide `.api_key(...)` or set SMALLPICT_API_KEY environment variable.".into(),
            details: None,
        })?;

        if key.trim().is_empty() {
            return Err(SmallPictError::Validation {
                message: "API key cannot be empty".into(),
                details: None,
            });
        }

        let secret = self
            .secret_key
            .filter(|s| !s.trim().is_empty())
            .ok_or_else(|| SmallPictError::Validation {
                message: "Missing required SmallPict secret key. Provide `.secret_key(...)` or set SMALLPICT_SECRET_KEY environment variable.".into(),
                details: None,
            })?;

        let base = self
            .base_url
            .unwrap_or_else(|| "https://api.smallpict.app".to_string());

        let http_client = ReqwestBlockingClient::builder()
            .timeout(self.timeout)
            .redirect(reqwest::redirect::Policy::none())
            .build()
            .map_err(SmallPictError::from)?;

        Ok(BlockingClient {
            api_key: SecretRedacted(key),
            secret_key: SecretRedacted(secret),
            base_url: base,
            timeout: self.timeout,
            max_retries: self.max_retries,
            max_retry_delay: self.max_retry_delay,
            fallback_mode: self.fallback_mode,
            poll_interval: self.poll_interval,
            wait_timeout: self.wait_timeout,
            http_client,
        })
    }
}