foundation_deployment_docker 0.1.3

Docker Engine API v1.53 client over DynNetClient + valtron (bollard-free)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Container lifecycle operations for [`super::DockerClient`] — restart, kill,
//! rename, resize, top, changes, logs, stats, export, archive, attach, list, prune.
//!
//! WHY: These are the remaining container endpoints from the Docker Engine API
//! (v1.53) that complete the container lifecycle surface. Binary-stream endpoints
//! (logs, export, archive, attach) and live stats return their body unread, as
//! an async stream: a followed log or a stats feed never ends, and a
//! container's filesystem can be gigabytes.
//!
//! WHAT: One `impl DockerClient` block with a method per endpoint, following the
//! same pattern as `client/mod.rs` — construct the generated `Args` struct, call
//! into the transport, return the body.
//!
//! HOW: State-mutating commands (restart, kill, rename, resize, prune) use the
//! generated async functions that return `ApiResponse<()>`. Query endpoints (top,
//! changes, list) use the generated functions and return the deserialized
//! body. Streaming endpoints use the generated functions too, which check the
//! status and hand back the body unread; it is wrapped in a [`LogStream`]
//! (multiplexed stdout/stderr), a [`ByteBody`] (tar archives) or a
//! [`StatsStream`] (one stats sample per line; a single sample is the first
//! line of a `stream=false` answer).

use foundation_netio::shared::http::SimpleHeader;
use foundation_netio::PreparedRequestBuilder;

use crate::client::body::{ByteBody, LogStream, StatsStream};
use crate::error::DockerError;
use crate::generated::archive::{container_archive_request, ContainerArchiveArgs};
use crate::generated::attach::{container_attach_request, ContainerAttachArgs};
use crate::generated::export::{container_export_request, ContainerExportArgs};
use crate::generated::logs::{container_logs_request, ContainerLogsArgs};
use crate::generated::shared::ApiError;
use crate::generated::update::{container_update_request, ContainerUpdateArgs, ContainerUpdateResponse};

use crate::generated::changes::{container_changes_request, ContainerChangesArgs};
use crate::generated::json::{container_list_request, ContainerListArgs};
use crate::generated::kill::{container_kill_request, ContainerKillArgs};
use crate::generated::prune::{container_prune_request, ContainerPruneArgs};
use crate::generated::rename::{container_rename_request, ContainerRenameArgs};
use crate::generated::resize::{container_resize_request, ContainerResizeArgs};
use crate::generated::restart::{container_restart_request, ContainerRestartArgs};
use crate::generated::stats::{container_stats_request, ContainerStatsArgs, ContainerStatsResponse};
use crate::generated::top::{container_top_request, ContainerTopArgs, ContainerTopResponse};

use super::DockerClient;
use super::NoMod;

/// What [`DockerClient::container_logs`] asks for (`GET
/// /containers/{id}/logs` query parameters).
///
/// The default reads everything the container has written so far, stdout
/// and stderr, without following — the daemon rejects a request that asks for
/// neither stream.
#[derive(Debug, Clone)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "one field per boolean query parameter of the endpoint"
)]
pub struct ContainerLogsOptions {
    /// Keep the stream open and send new output as the container writes it
    /// (`follow`). The stream then ends only when the container stops or the
    /// caller drops it.
    pub follow: bool,
    /// Include stdout (`stdout`).
    pub stdout: bool,
    /// Include stderr (`stderr`).
    pub stderr: bool,
    /// Only output written at or after this UNIX timestamp (`since`).
    pub since: Option<String>,
    /// Only output written before this UNIX timestamp (`until`).
    pub until: Option<String>,
    /// Prefix every line with its timestamp (`timestamps`).
    pub timestamps: bool,
    /// Only this many lines from the end (`tail`); `None` is all of them.
    pub tail: Option<String>,
}

impl Default for ContainerLogsOptions {
    fn default() -> Self {
        Self {
            follow: false,
            stdout: true,
            stderr: true,
            since: None,
            until: None,
            timestamps: false,
            tail: None,
        }
    }
}

/// What [`DockerClient::container_attach`] asks for (`POST
/// /containers/{id}/attach` query parameters).
///
/// The default attaches to the live stdout and stderr, as `docker attach
/// --no-stdin` does: no earlier output, no stdin.
#[derive(Debug, Clone)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "one field per boolean query parameter of the endpoint"
)]
pub struct ContainerAttachOptions {
    /// The key sequence that detaches from the container (`detachKeys`);
    /// `None` keeps the daemon's default.
    pub detach_keys: Option<String>,
    /// Replay the output written before attaching (`logs`).
    pub logs: bool,
    /// Stream output as the container writes it (`stream`).
    pub stream: bool,
    /// Attach stdin (`stdin`).
    pub stdin: bool,
    /// Attach stdout (`stdout`).
    pub stdout: bool,
    /// Attach stderr (`stderr`).
    pub stderr: bool,
}

impl Default for ContainerAttachOptions {
    fn default() -> Self {
        Self {
            detach_keys: None,
            logs: false,
            stream: true,
            stdin: false,
            stdout: true,
            stderr: true,
        }
    }
}

impl DockerClient {
    // ── restart_container ───────────────────────────────────────────────

    /// Restart a container (`POST /containers/{id}/restart`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn restart_container(
        &self,
        id: &str,
        signal: Option<&str>,
        t: Option<u32>,
    ) -> Result<(), DockerError> {
        let args = ContainerRestartArgs {
            id: id.to_string(),
            signal: signal.map(str::to_string),
            t: t.map(|v| v.to_string()),
        };
        container_restart_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(())
    }

    // ── kill_container ──────────────────────────────────────────────────

    /// Kill a container (`POST /containers/{id}/kill`).
    ///
    /// WHY: Sends a POSIX signal (default `SIGKILL`) to the container's main
    /// process, bypassing the normal stop sequence.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn kill_container(
        &self,
        id: &str,
        signal: Option<&str>,
    ) -> Result<(), DockerError> {
        let args = ContainerKillArgs {
            id: id.to_string(),
            signal: signal.map(str::to_string),
        };
        container_kill_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(())
    }

    // ── rename_container ────────────────────────────────────────────────

    /// Rename a container (`POST /containers/{id}/rename`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn rename_container(&self, id: &str, name: &str) -> Result<(), DockerError> {
        let args = ContainerRenameArgs {
            id: id.to_string(),
            name: Some(name.to_string()),
        };
        container_rename_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(())
    }

    // ── resize_container ────────────────────────────────────────────────

    /// Resize the TTY of a container (`POST /containers/{id}/resize`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn resize_container(
        &self,
        id: &str,
        h: Option<u32>,
        w: Option<u32>,
    ) -> Result<(), DockerError> {
        let args = ContainerResizeArgs {
            id: id.to_string(),
            h: h.map(|v| v.to_string()),
            w: w.map(|v| v.to_string()),
        };
        container_resize_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(())
    }

    // ── container_top ───────────────────────────────────────────────────

    /// List processes running inside a container (`GET /containers/{id}/top`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn container_top(
        &self,
        id: &str,
        ps_args: Option<&str>,
    ) -> Result<ContainerTopResponse, DockerError> {
        let args = ContainerTopArgs {
            id: id.to_string(),
            ps_args: ps_args.map(str::to_string),
        };
        let response = container_top_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(response.body)
    }

    // ── container_changes ───────────────────────────────────────────────

    /// Get changes on a container's filesystem (`GET /containers/{id}/changes`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn container_changes(
        &self,
        id: &str,
    ) -> Result<serde_json::Value, DockerError> {
        let args = ContainerChangesArgs {
            id: id.to_string(),
        };
        let response = container_changes_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(response.body)
    }

    // ── container_logs ──────────────────────────────────────────────────

    /// Get a container's logs (`GET /containers/{id}/logs`).
    ///
    /// WHY: With `follow`, the daemon keeps the stream open for as long as the
    /// container runs; even without it, a chatty container's log is unbounded.
    ///
    /// WHAT: Returns the log unread, as a [`LogStream`] of demultiplexed
    /// stdout/stderr frames (for a container without a TTY).
    ///
    /// [`ContainerLogsOptions::default`] reads stdout and stderr written so
    /// far, without following.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status (checked
    /// before the body is read); read errors come out of the stream.
    pub async fn container_logs(
        &self,
        id: &str,
        opts: &ContainerLogsOptions,
    ) -> Result<LogStream, DockerError> {
        let args = ContainerLogsArgs {
            id: id.to_string(),
            follow: Some(opts.follow.to_string()),
            stdout: Some(opts.stdout.to_string()),
            stderr: Some(opts.stderr.to_string()),
            since: opts.since.clone(),
            until: opts.until.clone(),
            timestamps: Some(opts.timestamps.to_string()),
            tail: opts.tail.clone(),
        };
        let response =
            container_logs_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(LogStream::new(response.body, self.limits().max_log_frame))
    }

    // ── container_stats ─────────────────────────────────────────────────

    /// One resource-usage sample of a container (`GET
    /// /containers/{id}/stats?stream=false`).
    ///
    /// WHAT: Asks for a single sample and reads the one line the daemon
    /// answers with. With `one_shot`, the daemon does not wait for a second
    /// CPU sample, so the answer comes back at once but `precpu_stats` is
    /// empty (no CPU percentage can be computed from it); without it the
    /// daemon takes about a second to fill `precpu_stats`. For a live feed use
    /// [`Self::container_stats_stream`].
    ///
    /// # Errors
    ///
    /// [`DockerError::Api`] for a non-2xx status (checked before the body is
    /// read), [`DockerError::Transport`] if sending or reading fails, and
    /// [`DockerError::JsonParse`] if the line is not a stats object or the
    /// daemon sends no line at all.
    pub async fn container_stats(
        &self,
        id: &str,
        one_shot: bool,
    ) -> Result<ContainerStatsResponse, DockerError> {
        let mut stats = self.send_container_stats(id, false, one_shot).await?;
        match stats.next().await {
            Some(sample) => sample,
            None => Err(DockerError::JsonParse(format!(
                "container {id} stats: the daemon answered with an empty body"
            ))),
        }
    }

    /// A container's live resource usage (`GET
    /// /containers/{id}/stats?stream=true`), one sample per line.
    ///
    /// WHAT: Returns the feed unread, as a [`StatsStream`] of the generated
    /// [`ContainerStatsResponse`]. The daemon writes a sample about once a
    /// second for as long as the container runs; the stream ends only when the
    /// caller drops it (which closes the connection) or the daemon closes the
    /// feed.
    ///
    /// There is no `one_shot` here: the API allows `one-shot` only with
    /// `stream=false`, and dockerd rejects the pair with a `400`.
    ///
    /// # Errors
    ///
    /// [`DockerError::Api`] for a non-2xx status (checked before the body is
    /// read) or [`DockerError::Transport`] if sending fails; malformed samples
    /// and read errors come out of the stream.
    pub async fn container_stats_stream(&self, id: &str) -> Result<StatsStream, DockerError> {
        self.send_container_stats(id, true, false).await
    }

    /// Send `GET /containers/{id}/stats` and hand back its body unread.
    async fn send_container_stats(
        &self,
        id: &str,
        stream: bool,
        one_shot: bool,
    ) -> Result<StatsStream, DockerError> {
        let args = ContainerStatsArgs {
            id: id.to_string(),
            stream: Some(stream.to_string()),
            one_shot: Some(one_shot.to_string()),
        };
        let response =
            container_stats_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(StatsStream::new(response.body))
    }

    // ── container_export ────────────────────────────────────────────────

    /// Export a container's filesystem as a tar archive (`GET
    /// /containers/{id}/export`).
    ///
    /// WHAT: Returns the archive unread, as a [`ByteBody`] of chunks — write it
    /// where it is going chunk by chunk; a filesystem can be gigabytes.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status (checked
    /// before the body is read); read errors come out of the stream.
    pub async fn container_export(&self, id: &str) -> Result<ByteBody, DockerError> {
        let args = ContainerExportArgs { id: id.to_string() };
        let response =
            container_export_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(ByteBody::new(response.body))
    }

    // ── container_archive ───────────────────────────────────────────────

    /// Get an archive of a file or directory in a container (`GET
    /// /containers/{id}/archive`).
    ///
    /// WHAT: Returns the tar archive unread, as a [`ByteBody`] of chunks.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status (checked
    /// before the body is read); read errors come out of the stream.
    pub async fn container_archive(&self, id: &str, path: &str) -> Result<ByteBody, DockerError> {
        let args = ContainerArchiveArgs {
            id: id.to_string(),
            path: Some(path.to_string()),
        };
        let response =
            container_archive_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(ByteBody::new(response.body))
    }

    // ── container_attach ────────────────────────────────────────────────

    /// Attach to a container's stdio (`POST /containers/{id}/attach`).
    ///
    /// WHY: With `stream`, the daemon keeps this open for as long as the
    /// container runs. The upgrade to WebSocket for interactive sessions is
    /// handled via the `ws` module.
    ///
    /// WHAT: Returns the output unread, as a [`LogStream`] of demultiplexed
    /// frames (for a container without a TTY).
    ///
    /// [`ContainerAttachOptions::default`] attaches to the live stdout and
    /// stderr.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status (checked
    /// before the body is read); read errors come out of the stream.
    pub async fn container_attach(
        &self,
        id: &str,
        opts: &ContainerAttachOptions,
    ) -> Result<LogStream, DockerError> {
        let args = ContainerAttachArgs {
            id: id.to_string(),
            detach_keys: opts.detach_keys.clone(),
            logs: Some(opts.logs.to_string()),
            stream: Some(opts.stream.to_string()),
            stdin: Some(opts.stdin.to_string()),
            stdout: Some(opts.stdout.to_string()),
            stderr: Some(opts.stderr.to_string()),
        };
        let response =
            container_attach_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(LogStream::new(response.body, self.limits().max_log_frame))
    }

    // ── container_list ──────────────────────────────────────────────────

    /// List containers (`GET /containers/json`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn container_list(
        &self,
        all: bool,
        limit: Option<u32>,
        size: bool,
        filters: Option<&str>,
    ) -> Result<serde_json::Value, DockerError> {
        let args = ContainerListArgs {
            all: Some(all.to_string()),
            limit: limit.map(|v| v.to_string()),
            size: Some(size.to_string()),
            filters: filters.map(str::to_string),
        };
        let response = container_list_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(response.body)
    }

    // ── container_prune ─────────────────────────────────────────────────

    /// Delete stopped containers (`POST /containers/prune`).
    ///
    /// WHY: The generated function returns `ApiResponse<()>` — the prune
    /// response body (deleted IDs + space reclaimed) is not yet modelled as a
    /// typed struct by the generator.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn container_prune(
        &self,
        filters: Option<&str>,
    ) -> Result<(), DockerError> {
        let args = ContainerPruneArgs {
            filters: filters.map(str::to_string),
        };
        container_prune_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(())
    }

    // ── update_container ────────────────────────────────────────────────

    /// Update a container's resource-limit configuration (`POST
    /// /containers/{id}/update`).
    ///
    /// WHY: Matches bollard's `update_container` — lets callers change CPU,
    /// memory, restart-policy and other `HostConfig`-style limits on a running
    /// container without recreating it.
    ///
    /// WHAT: `config` is the Docker `ContainerUpdate` body (a `HostConfig`
    /// subset plus `RestartPolicy`), read into the generated
    /// `ContainerUpdateRequest`. Returns the `ContainerUpdateResponse` (any
    /// warnings emitted by the daemon).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError::JsonParse`] if `config` does not fit the
    /// `ContainerUpdateRequest` schema, and [`DockerError`] on transport
    /// failure or non-2xx status.
    pub async fn update_container(
        &self,
        id: &str,
        config: serde_json::Value,
    ) -> Result<ContainerUpdateResponse, DockerError> {
        let args = ContainerUpdateArgs {
            id: id.to_string(),
            body: super::generated_body("container update", config)?,
        };
        let response =
            container_update_request(self.api(), &args, &self.api_url().await?, None::<NoMod>).await?;
        Ok(response.body)
    }

    // ── upload_to_container ─────────────────────────────────────────────

    /// Upload a tar archive of files into a container (`PUT
    /// /containers/{id}/archive`).
    ///
    /// WHY: Matches bollard's `upload_to_container` — the inverse of
    /// [`Self::container_archive`]. The generated `put_container_archive`
    /// function only wires query parameters (it never sets the request body),
    /// so we build the request directly to attach the raw tar bytes with the
    /// `application/x-tar` content type Docker requires.
    ///
    /// WHAT: `tar` is a raw (optionally gzip/bzip2/xz-compressed) tar stream.
    /// `path` is the destination directory inside the container. When
    /// `no_overwrite_dir_non_dir` is set, Docker refuses to replace a
    /// directory with a non-directory (and vice-versa); `copy_uidgid` copies
    /// the archive's UID/GID instead of remapping to the container user.
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure or non-2xx status.
    pub async fn upload_to_container(
        &self,
        id: &str,
        path: &str,
        tar: Vec<u8>,
        no_overwrite_dir_non_dir: Option<bool>,
        copy_uidgid: Option<bool>,
    ) -> Result<(), DockerError> {
        let endpoint_url = format!("{}/containers/{id}/archive", self.api_url().await?);

        let mut builder = PreparedRequestBuilder::put(&endpoint_url)
            .map_err(|e| ApiError::RequestBuildFailed(e.to_string()))?;

        builder = builder.query("path", Some(path));
        builder = builder.query(
            "noOverwriteDirNonDir",
            no_overwrite_dir_non_dir.map(|v| v.to_string()).as_deref(),
        );
        builder = builder.query("copyUIDGID", copy_uidgid.map(|v| v.to_string()).as_deref());
        builder = builder.header(SimpleHeader::custom("Content-Type"), "application/x-tar");
        builder = builder.body_bytes(tar);

        let response = self.http().send_async(builder.build()).await
            .map_err(|e| ApiError::RequestSendFailed(e.to_string()))?;

        let status: usize = response.get_status().into();
        let headers = response.get_headers_ref().clone();
        if status < 200 || status >= 300 {
            return Err(ApiError::HttpStatus { code: status as u16, headers, body: None }.into());
        }
        Ok(())
    }

    // ── get_container_archive_info ──────────────────────────────────────

    /// Retrieve metadata about a path inside a container (`HEAD
    /// /containers/{id}/archive`).
    ///
    /// WHY: Matches bollard's `get_container_archive_info` — the cheap `HEAD`
    /// companion to [`Self::container_archive`]. Docker returns the stat as a
    /// base64-encoded JSON blob in the `X-Docker-Container-Path-Stat` header
    /// rather than the response body.
    ///
    /// WHAT: Returns the raw (still base64-encoded) header value, or an error
    /// if the path does not exist. Callers decode it into a `FileInfo`
    /// (`name`, `size`, `mode`, `mtime`, `linkTarget`).
    ///
    /// # Errors
    ///
    /// Returns [`DockerError`] on transport failure, non-2xx status, or a
    /// missing stat header.
    pub async fn get_container_archive_info(
        &self,
        id: &str,
        path: &str,
    ) -> Result<String, DockerError> {
        let endpoint_url = format!("{}/containers/{id}/archive", self.api_url().await?);

        let mut builder = PreparedRequestBuilder::head(&endpoint_url)
            .map_err(|e| ApiError::RequestBuildFailed(e.to_string()))?;
        builder = builder.query("path", Some(path));

        let response = self.http().send_async(builder.build()).await
            .map_err(|e| ApiError::RequestSendFailed(e.to_string()))?;

        let status: usize = response.get_status().into();
        let headers = response.get_headers_ref().clone();
        if status < 200 || status >= 300 {
            return Err(ApiError::HttpStatus { code: status as u16, headers, body: None }.into());
        }
        // Docker returns the stat blob in `X-Docker-Container-Path-Stat`.
        // HTTP header names are case-insensitive and the transport preserves
        // the daemon's original casing, so scan case-insensitively rather than
        // assuming an exact key.
        let stat = headers
            .iter()
            .find(|(k, _)| k.to_string().eq_ignore_ascii_case("x-docker-container-path-stat"))
            .and_then(|(_, v)| v.first())
            .cloned();
        stat.ok_or_else(|| {
            ApiError::RequestSendFailed("missing X-Docker-Container-Path-Stat header".to_string())
                .into()
        })
    }
}