toolkit-zero 5.11.0

A feature-selective Rust utility crate — a modular collection of opt-in utilities spanning encryption, HTTP networking, geolocation, and build-time fingerprinting. Enable only the features your project requires.
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912

//! Typed, fluent HTTP client.
//!
//! This module provides a builder-oriented API for issuing HTTP requests
//! against a configurable [`Target`] — a `localhost` port or an arbitrary
//! remote base URL — and deserialising the response body into a concrete Rust
//! type via [`serde`].
//!
//! The entry point is [`Client`]. Call any method constructor
//! ([`get`](Client::get), [`post`](Client::post), etc.) to obtain a
//! [`RequestBuilder`]. Optionally attach a JSON body via
//! [`json`](RequestBuilder::json) or URL query parameters via
//! [`query`](RequestBuilder::query), then finalise with
//! [`send`](RequestBuilder::send) (async) or
//! [`send_sync`](RequestBuilder::send_sync) (blocking).
//! All seven standard HTTP methods are supported.
//!
//! # Builder chains at a glance
//!
//! | Chain | Sends |
//! |---|---|
//! | `client.method(endpoint).send().await` | plain request |
//! | `.json(value).send().await` | `Content-Type: application/json` body |
//! | `.query(params).send().await` | serialised query string |
//!
//! # Example
//!
//! ```rust,no_run
//! use toolkit_zero::socket::client::{Client, Target};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize)] struct Item { id: u32, name: String }
//! #[derive(Serialize)] struct NewItem { name: String }
//! #[derive(Serialize)] struct Filter { page: u32 }
//!
//! # async fn run() -> Result<(), reqwest::Error> {
//! let client = Client::new_async(Target::Localhost(8080));
//!
//! // Plain async GET
//! let items: Vec<Item> = client.get("/items").send().await?;
//!
//! // POST with a JSON body
//! let created: Item = client
//!     .post("/items")
//!     .json(NewItem { name: "widget".into() })
//!     .send()
//!     .await?;
//!
//! // GET with query parameters
//! let page: Vec<Item> = client
//!     .get("/items")
//!     .query(Filter { page: 2 })
//!     .send()
//!     .await?;
//!
//! // Synchronous DELETE
//! let _: Item = client.delete("/items/1").send_sync()?;
//! # Ok(())
//! # }
//! ```

use reqwest::{Client as AsyncClient, blocking::Client as BlockingClient};
use serde::{Serialize, de::DeserializeOwned};
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use crate::socket::SerializationKey;
use crate::serialization::SerializationError;

pub use toolkit_zero_macros::request;

fn build_url(base: &str, endpoint: &str) -> String {
    let ep = endpoint.trim_start_matches('/');
    if ep.is_empty() {
        base.trim_end_matches('/').to_owned()
    } else {
        format!("{}/{}", base.trim_end_matches('/'), ep)
    }
}

#[derive(Clone, Copy, Debug)]
enum HttpMethod {
    Get,
    Post,
    Put,
    Delete,
    Patch,
    Head,
    Options,
}

impl HttpMethod {
    fn apply_async(&self, client: &AsyncClient, url: &str) -> reqwest::RequestBuilder {
        match self {
            HttpMethod::Get     => client.get(url),
            HttpMethod::Post    => client.post(url),
            HttpMethod::Put     => client.put(url),
            HttpMethod::Delete  => client.delete(url),
            HttpMethod::Patch   => client.patch(url),
            HttpMethod::Head    => client.head(url),
            HttpMethod::Options => client.request(reqwest::Method::OPTIONS, url),
        }
    }

    fn apply_sync(&self, client: &BlockingClient, url: &str) -> reqwest::blocking::RequestBuilder {
        match self {
            HttpMethod::Get     => client.get(url),
            HttpMethod::Post    => client.post(url),
            HttpMethod::Put     => client.put(url),
            HttpMethod::Delete  => client.delete(url),
            HttpMethod::Patch   => client.patch(url),
            HttpMethod::Head    => client.head(url),
            HttpMethod::Options => client.request(reqwest::Method::OPTIONS, url),
        }
    }
}

/// The target server for a [`Client`].
#[derive(Clone)]
pub enum Target {
    /// A locally running server. Provide the port number.
    Localhost(u16),
    /// A remote server. Provide the full base URL (e.g. `"https://example.com"`).
    Remote(String),
}

/// HTTP client for making typed requests against a [`Target`] server.
///
/// A `Client` is created in one of three modes depending on which send variants you need:
///
/// | Constructor | `send()` (async) | `send_sync()` (blocking) | Safe in async context |
/// |---|---|---|---|
/// | [`Client::new_async`] | ✓ | ✗ | ✓ |
/// | [`Client::new_sync`] | ✗ | ✓ | ✓ |
/// | [`Client::new`] | ✓ | ✓ | ✗ — panics if called inside a tokio runtime |
///
/// `reqwest::blocking::Client` internally creates its own single-threaded tokio runtime. If
/// you call `Client::new()` (or `Client::new_sync()`) from within an existing async context
/// (e.g. inside `#[tokio::main]`) it will panic. Use `Client::new_async()` when your program
/// is async-first and only call `Client::new_sync()` / `Client::new()` before entering any
/// async runtime.
///
/// # Example
/// ```rust,no_run
/// # use toolkit_zero::socket::client::{Client, Target};
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Deserialize)] struct Item { id: u32, name: String }
/// # #[derive(Serialize)] struct NewItem { name: String }
/// # async fn example() -> Result<(), reqwest::Error> {
/// // Async-only client — safe inside #[tokio::main]
/// let client = Client::new_async(Target::Localhost(8080));
///
/// let items: Vec<Item> = client.get("/items").send().await?;
/// let created: Item = client.post("/items").json(NewItem { name: "w".into() }).send().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct Client {
    target: Target,
    async_client: Option<AsyncClient>,
    sync_client: Option<BlockingClient>,
}

impl Client {
    /// Creates an **async-only** client. Safe to call from any context, including inside
    /// `#[tokio::main]`. Calling `.send_sync()` on builders from this client will panic.
    pub fn new_async(target: Target) -> Self {
        log::debug!("Creating async-only client");
        Self { target, async_client: Some(AsyncClient::new()), sync_client: None }
    }

    /// Creates a **sync-only** client. **Must not** be called from within an async context
    /// (inside `#[tokio::main]` or similar) — doing so panics. Calling `.send()` on builders
    /// from this client will panic with a message pointing to [`Client::new_async`].
    ///
    /// # Panics
    ///
    /// Panics at construction time if called inside a tokio runtime (same restriction as
    /// `reqwest::blocking::Client`). Prefer [`Client::new_async`] for async contexts.
    pub fn new_sync(target: Target) -> Self {
        log::debug!("Creating sync-only client");
        Self { target, async_client: None, sync_client: Some(BlockingClient::new()) }
    }

    /// Creates a client supporting **both** async and blocking sends.
    ///
    /// # Panics
    ///
    /// **Panics immediately if called from within an async context** (e.g. inside
    /// `#[tokio::main]`, `tokio::spawn`, or any `.await` call chain). This happens because
    /// `reqwest::blocking::Client` creates its own internal tokio runtime, and Rust/tokio
    /// forbids nesting two runtimes in the same thread.
    ///
    /// If you are in an async context, use [`Client::new_async`] instead.
    /// If you only need blocking calls, use [`Client::new_sync`] **before** entering any runtime.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::{Client, Target};
    /// // Correct — called from synchronous main before any async runtime starts
    /// fn main() {
    ///     let client = Client::new(Target::Localhost(8080));
    ///     // ... use client.send_sync() and client.send() via manual runtime
    /// }
    ///
    /// // WRONG — will panic at runtime:
    /// // #[tokio::main]
    /// // async fn main() { let client = Client::new(...); }  // panics!
    /// ```
    pub fn new(target: Target) -> Self {
        // Detect async context early: tokio sets a thread-local when a runtime is active.
        // try_current() succeeds only if we are already inside a tokio runtime — exactly
        // the forbidden case for BlockingClient, so we panic with an actionable message.
        if tokio::runtime::Handle::try_current().is_ok() {
            panic!(
                "Client::new() called inside an async context (tokio runtime detected). \
                 BlockingClient cannot be created inside an existing runtime.\n\
                 → Use Client::new_async(target) if you only need .send() (async).\n\
                 → Use Client::new_sync(target) called before entering any async runtime if you only need .send_sync()."
            );
        }
        log::debug!("Creating dual async+sync client");
        Self {
            target,
            async_client: Some(AsyncClient::new()),
            sync_client: Some(BlockingClient::new()),
        }
    }

    fn async_client(&self) -> &AsyncClient {
        self.async_client.as_ref()
            .expect("Client was created with new_sync() — call new_async() or new() to use async sends")
    }

    fn sync_client(&self) -> &BlockingClient {
        self.sync_client.as_ref()
            .expect("Client was created with new_async() — call new_sync() or new() to use sync sends")
    }

    /// Returns the base URL derived from the configured [`Target`].
    pub fn base_url(&self) -> String {
        match &self.target {
            Target::Localhost(port) => format!("http://localhost:{}", port),
            Target::Remote(url) => url.clone(),
        }
    }

    fn builder(&self, method: HttpMethod, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        RequestBuilder::new(self, method, endpoint)
    }

    /// Starts a `GET` request builder for `endpoint`.
    pub fn get(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Get, endpoint)
    }

    /// Starts a `POST` request builder for `endpoint`.
    pub fn post(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Post, endpoint)
    }

    /// Starts a `PUT` request builder for `endpoint`.
    pub fn put(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Put, endpoint)
    }

    /// Starts a `DELETE` request builder for `endpoint`.
    pub fn delete(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Delete, endpoint)
    }

    /// Starts a `PATCH` request builder for `endpoint`.
    pub fn patch(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Patch, endpoint)
    }

    /// Starts a `HEAD` request builder for `endpoint`.
    pub fn head(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Head, endpoint)
    }

    /// Starts an `OPTIONS` request builder for `endpoint`.
    pub fn options(&self, endpoint: impl Into<String>) -> RequestBuilder<'_> {
        self.builder(HttpMethod::Options, endpoint)
    }
}

/// A request builder with no body or query parameters attached.
///
/// Obtained from any [`Client`] method constructor. Attach a JSON body via
/// [`json`](RequestBuilder::json) or query parameters via [`query`](RequestBuilder::query),
/// or finalise directly with [`send`](RequestBuilder::send) /
/// [`send_sync`](RequestBuilder::send_sync).
pub struct RequestBuilder<'a> {
    client: &'a Client,
    method: HttpMethod,
    endpoint: String,
}

impl<'a> RequestBuilder<'a> {
    fn new(client: &'a Client, method: HttpMethod, endpoint: impl Into<String>) -> Self {
        let endpoint = endpoint.into();
        log::debug!("Building {:?} request for endpoint '{}'", method, endpoint);
        Self { client, method, endpoint }
    }

    /// Attaches a JSON-serialisable body, transitioning to [`JsonRequestBuilder`].
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize)] struct NewItem { name: String }
    /// # #[derive(Deserialize)] struct Item { id: u32, name: String }
    /// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let item: Item = client
    ///     .post("/items")
    ///     .json(NewItem { name: "widget".to_string() })
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn json<T: Serialize>(self, body: T) -> JsonRequestBuilder<'a, T> {
        log::trace!("Attaching JSON body to {:?} request for '{}'", self.method, self.endpoint);
        JsonRequestBuilder { client: self.client, method: self.method, endpoint: self.endpoint, body }
    }

    /// Attaches query parameters that serialise into the URL query string, transitioning to
    /// [`QueryRequestBuilder`].
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize)] struct SearchParams { q: String, page: u32 }
    /// # #[derive(Deserialize)] struct SearchResult { items: Vec<String> }
    /// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let results: SearchResult = client
    ///     .get("/search")
    ///     .query(SearchParams { q: "rust".to_string(), page: 1 })
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn query<T: Serialize>(self, params: T) -> QueryRequestBuilder<'a, T> {
        log::trace!("Attaching query params to {:?} request for '{}'", self.method, self.endpoint);
        QueryRequestBuilder { client: self.client, method: self.method, endpoint: self.endpoint, params }
    }

    /// Sends the request asynchronously and deserialises the response body as `R`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)] struct User { id: u32, name: String }
    /// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let user: User = client.get("/users/1").send().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send<R: DeserializeOwned>(self) -> Result<R, reqwest::Error> {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending async {:?} to '{}'", self.method, url);
        let resp = self.method.apply_async(&self.client.async_client(), &url)
            .send().await?;
        log::debug!("Response status: {}", resp.status());
        resp.error_for_status()?.json::<R>().await
    }

    /// Sends the request synchronously and deserialises the response body as `R`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)] struct User { id: u32, name: String }
    /// # fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let user: User = client.get("/users/1").send_sync()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn send_sync<R: DeserializeOwned>(self) -> Result<R, reqwest::Error> {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending sync {:?} to '{}'", self.method, url);
        let resp = self.method.apply_sync(&self.client.sync_client(), &url)
            .send()?;
        log::debug!("Response status: {}", resp.status());
        resp.error_for_status()?.json::<R>()
    }

    /// Attaches an authenticated-encrypted body (ChaCha20-Poly1305), transitioning to
    /// [`EncryptedBodyRequestBuilder`].
    ///
    /// The body is sealed with the given [`SerializationKey`] and sent as
    /// `application/octet-stream`; the response is opened with the same key.
    /// For plain-JSON routes use `.json(body)` instead.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use bincode::{Encode, Decode};
    /// # #[derive(Encode)] struct Req { value: i32 }
    /// # #[derive(Decode)] struct Resp { result: i32 }
    /// # async fn example(client: &Client) -> Result<(), toolkit_zero::socket::client::ClientError> {
    /// use toolkit_zero::socket::SerializationKey;
    /// let resp: Resp = client
    ///     .post("/compute")
    ///     .encryption(Req { value: 42 }, SerializationKey::Default)
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn encryption<T: bincode::Encode>(self, body: T, key: SerializationKey) -> EncryptedBodyRequestBuilder<'a, T> {
        log::trace!("Attaching encrypted body to {:?} request for '{}'", self.method, self.endpoint);
        EncryptedBodyRequestBuilder { client: self.client, method: self.method, endpoint: self.endpoint, body, key }
    }

    /// Attaches authenticated-encrypted query parameters (ChaCha20-Poly1305), transitioning
    /// to [`EncryptedQueryRequestBuilder`].
    ///
    /// The params are sealed and sent as `?data=<base64url>`; the response is opened
    /// with the same key. For plain query-string routes use `.query(params)` instead.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use bincode::{Encode, Decode};
    /// # #[derive(Encode)] struct Filter { page: u32 }
    /// # #[derive(Decode)] struct Page { items: Vec<String> }
    /// # async fn example(client: &Client) -> Result<(), toolkit_zero::socket::client::ClientError> {
    /// use toolkit_zero::socket::SerializationKey;
    /// let page: Page = client
    ///     .get("/items")
    ///     .encrypted_query(Filter { page: 1 }, SerializationKey::Default)
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn encrypted_query<T: bincode::Encode>(self, params: T, key: SerializationKey) -> EncryptedQueryRequestBuilder<'a, T> {
        log::trace!("Attaching encrypted query to {:?} request for '{}'", self.method, self.endpoint);
        EncryptedQueryRequestBuilder { client: self.client, method: self.method, endpoint: self.endpoint, params, key }
    }
}

/// A request builder that will send a JSON-serialised body.
///
/// Obtained from [`RequestBuilder::json`]. Finalise with [`send`](JsonRequestBuilder::send)
/// (async) or [`send_sync`](JsonRequestBuilder::send_sync) (sync).
///
/// # Example
/// ```rust,no_run
/// # use toolkit_zero::socket::client::Client;
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize)] struct UpdateItem { name: String }
/// # #[derive(Deserialize)] struct Item { id: u32, name: String }
/// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
/// // Async PUT
/// let updated: Item = client
///     .put("/items/42")
///     .json(UpdateItem { name: "new name".to_string() })
///     .send()
///     .await?;
///
/// // Sync PATCH
/// let patched: Item = client
///     .patch("/items/42")
///     .json(UpdateItem { name: "new name".to_string() })
///     .send_sync()?;
/// # Ok(())
/// # }
/// ```
pub struct JsonRequestBuilder<'a, T> {
    client: &'a Client,
    method: HttpMethod,
    endpoint: String,
    body: T,
}

impl<'a, T: Serialize> JsonRequestBuilder<'a, T> {
    /// Sends the request asynchronously with the JSON body and deserialises the response as `R`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize)] struct Payload { value: i32 }
    /// # #[derive(Deserialize)] struct Ack { received: bool }
    /// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let ack: Ack = client
    ///     .post("/process")
    ///     .json(Payload { value: 42 })
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send<R: DeserializeOwned>(self) -> Result<R, reqwest::Error> {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending async {:?} with JSON body to '{}'", self.method, url);
        let resp = self.method.apply_async(&self.client.async_client(), &url)
            .json(&self.body)
            .send().await?;
        log::debug!("Response status: {}", resp.status());
        resp.error_for_status()?.json::<R>().await
    }

    /// Sends the request synchronously with the JSON body and deserialises the response as `R`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize)] struct Payload { value: i32 }
    /// # #[derive(Deserialize)] struct Ack { received: bool }
    /// # fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let ack: Ack = client
    ///     .post("/process")
    ///     .json(Payload { value: 42 })
    ///     .send_sync()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn send_sync<R: DeserializeOwned>(self) -> Result<R, reqwest::Error> {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending sync {:?} with JSON body to '{}'", self.method, url);
        let resp = self.method.apply_sync(&self.client.sync_client(), &url)
            .json(&self.body)
            .send()?;
        log::debug!("Response status: {}", resp.status());
        resp.error_for_status()?.json::<R>()
    }
}

/// A request builder that will append serialisable query parameters to the URL.
///
/// Obtained from [`RequestBuilder::query`]. Finalise with [`send`](QueryRequestBuilder::send)
/// (async) or [`send_sync`](QueryRequestBuilder::send_sync) (sync).
///
/// # Example
/// ```rust,no_run
/// # use toolkit_zero::socket::client::Client;
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize)] struct Filters { status: String, limit: u32 }
/// # #[derive(Deserialize)] struct Item { id: u32, name: String }
/// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
/// // Async GET with query params
/// let items: Vec<Item> = client
///     .get("/items")
///     .query(Filters { status: "active".to_string(), limit: 20 })
///     .send()
///     .await?;
///
/// // Sync variant
/// let items: Vec<Item> = client
///     .get("/items")
///     .query(Filters { status: "active".to_string(), limit: 20 })
///     .send_sync()?;
/// # Ok(())
/// # }
/// ```
pub struct QueryRequestBuilder<'a, T> {
    client: &'a Client,
    method: HttpMethod,
    endpoint: String,
    params: T,
}

impl<'a, T: Serialize> QueryRequestBuilder<'a, T> {
    /// Sends the request asynchronously with query parameters and deserialises the response as `R`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize)] struct Params { page: u32 }
    /// # #[derive(Deserialize)] struct Page { items: Vec<String> }
    /// # async fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let page: Page = client
    ///     .get("/feed")
    ///     .query(Params { page: 2 })
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send<R: DeserializeOwned>(self) -> Result<R, reqwest::Error> {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending async {:?} with query params to '{}'", self.method, url);
        let resp = self.method.apply_async(&self.client.async_client(), &url)
            .query(&self.params)
            .send().await?;
        log::debug!("Response status: {}", resp.status());
        resp.error_for_status()?.json::<R>().await
    }

    /// Sends the request synchronously with query parameters and deserialises the response as `R`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use toolkit_zero::socket::client::Client;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize)] struct Params { page: u32 }
    /// # #[derive(Deserialize)] struct Page { items: Vec<String> }
    /// # fn example(client: &Client) -> Result<(), reqwest::Error> {
    /// let page: Page = client
    ///     .get("/feed")
    ///     .query(Params { page: 2 })
    ///     .send_sync()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn send_sync<R: DeserializeOwned>(self) -> Result<R, reqwest::Error> {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending sync {:?} with query params to '{}'", self.method, url);
        let resp = self.method.apply_sync(&self.client.sync_client(), &url)
            .query(&self.params)
            .send()?;
        log::debug!("Response status: {}", resp.status());
        resp.error_for_status()?.json::<R>()
    }
}

// ─── ClientError ─────────────────────────────────────────────────────────────

/// Error returned by [`EncryptedBodyRequestBuilder`] and [`EncryptedQueryRequestBuilder`].
///
/// Wraps either a transport-level [`reqwest::Error`] or a cipher failure.
#[derive(Debug)]
#[non_exhaustive]
pub enum ClientError {
    /// The underlying HTTP transport failed (connection refused, timeout, etc.).
    Transport(reqwest::Error),
    /// Sealing or opening failed (wrong key, corrupted bytes, etc.).
    Serialization(SerializationError),
}

impl std::fmt::Display for ClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Transport(e)      => write!(f, "transport error: {e}"),
            Self::Serialization(e)  => write!(f, "serialization error: {e}"),
        }
    }
}

impl std::error::Error for ClientError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Transport(e)     => Some(e),
            Self::Serialization(e) => Some(e),
        }
    }
}

impl From<reqwest::Error> for ClientError {
    fn from(e: reqwest::Error) -> Self { Self::Transport(e) }
}

impl From<SerializationError> for ClientError {
    fn from(e: SerializationError) -> Self { Self::Serialization(e) }
}

// ─── EncryptedBodyRequestBuilder ─────────────────────────────────────────────

/// A request builder that seals the body (ChaCha20-Poly1305) before sending.
///
/// Obtained from [`RequestBuilder::encryption`]. Finalise with
/// [`send`](EncryptedBodyRequestBuilder::send) (async) or
/// [`send_sync`](EncryptedBodyRequestBuilder::send_sync) (sync).
///
/// The expected response is also sealed and is opened transparently.
pub struct EncryptedBodyRequestBuilder<'a, T> {
    client: &'a Client,
    method: HttpMethod,
    endpoint: String,
    body: T,
    key: SerializationKey,
}

impl<'a, T: bincode::Encode> EncryptedBodyRequestBuilder<'a, T> {
    /// Sends the request asynchronously.
    ///
    /// Before the request leaves, the body is sealed using the [`SerializationKey`]
    /// supplied to [`.encryption()`](RequestBuilder::encryption).  The server receives a
    /// raw `application/octet-stream` payload.  When the response arrives, its bytes are
    /// opened with the same key to produce `R`.  If either sealing or opening fails
    /// the error is wrapped in [`ClientError::Serialization`].
    pub async fn send<R>(self) -> Result<R, ClientError>
    where
        R: bincode::Decode<()>,
    {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending async {:?} with encrypted body to '{}'", self.method, url);
        let sealed = crate::serialization::seal(&self.body, self.key.veil_key())?;
        let resp = self.method.apply_async(&self.client.async_client(), &url)
            .header("content-type", "application/octet-stream")
            .body(sealed)
            .send().await?;
        log::debug!("Response status: {}", resp.status());
        let bytes = resp.bytes().await?;
        Ok(crate::serialization::open::<R, _>(&bytes, self.key.veil_key())?)
    }

    /// Sends the request synchronously.
    ///
    /// The body is sealed with the configured [`SerializationKey`] before the wire
    /// send.  The response bytes, once received, are opened with the same key to
    /// produce `R`.  Any cipher failure is wrapped in [`ClientError::Serialization`].
    pub fn send_sync<R>(self) -> Result<R, ClientError>
    where
        R: bincode::Decode<()>,
    {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending sync {:?} with encrypted body to '{}'", self.method, url);
        let sealed = crate::serialization::seal(&self.body, self.key.veil_key())?;
        let resp = self.method.apply_sync(&self.client.sync_client(), &url)
            .header("content-type", "application/octet-stream")
            .body(sealed)
            .send()?;
        log::debug!("Response status: {}", resp.status());
        let bytes = resp.bytes()?;
        Ok(crate::serialization::open::<R, _>(&bytes, self.key.veil_key())?)
    }
}

// ─── EncryptedQueryRequestBuilder ────────────────────────────────────────────

/// A request builder that seals query params (ChaCha20-Poly1305) and sends them as
/// `?data=<base64url>`.
///
/// Obtained from [`RequestBuilder::encrypted_query`]. Finalise with
/// [`send`](EncryptedQueryRequestBuilder::send) (async) or
/// [`send_sync`](EncryptedQueryRequestBuilder::send_sync) (sync).
///
/// The expected response is also sealed and is opened transparently.
pub struct EncryptedQueryRequestBuilder<'a, T> {
    client: &'a Client,
    method: HttpMethod,
    endpoint: String,
    params: T,
    key: SerializationKey,
}

impl<'a, T: bincode::Encode> EncryptedQueryRequestBuilder<'a, T> {
    /// Sends the request asynchronously.
    ///
    /// The params are sealed with the configured [`SerializationKey`] and
    /// base64url-encoded, then appended to the URL as `?data=<base64url>`.  When the
    /// response arrives, its bytes are opened with the same key to produce `R`.
    /// Any cipher failure is wrapped in [`ClientError::Serialization`].
    pub async fn send<R>(self) -> Result<R, ClientError>
    where
        R: bincode::Decode<()>,
    {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending async {:?} with encrypted query to '{}'", self.method, url);
        let sealed = crate::serialization::seal(&self.params, self.key.veil_key())?;
        let b64 = URL_SAFE_NO_PAD.encode(&sealed);
        let resp = self.method.apply_async(&self.client.async_client(), &url)
            .query(&[("data", &b64)])
            .send().await?;
        log::debug!("Response status: {}", resp.status());
        let bytes = resp.bytes().await?;
        Ok(crate::serialization::open::<R, _>(&bytes, self.key.veil_key())?)
    }

    /// Sends the request synchronously.
    ///
    /// Same behaviour as [`send`](Self::send) — params are sealed and base64url-encoded
    /// as `?data=<value>`, and the sealed response bytes are opened to `R` — but the
    /// network call blocks the current thread.  Any cipher failure is wrapped in
    /// [`ClientError::Serialization`].
    pub fn send_sync<R>(self) -> Result<R, ClientError>
    where
        R: bincode::Decode<()>,
    {
        let url = build_url(&self.client.base_url(), &self.endpoint);
        log::info!("Sending sync {:?} with encrypted query to '{}'", self.method, url);
        let sealed = crate::serialization::seal(&self.params, self.key.veil_key())?;
        let b64 = URL_SAFE_NO_PAD.encode(&sealed);
        let resp = self.method.apply_sync(&self.client.sync_client(), &url)
            .query(&[("data", &b64)])
            .send()?;
        log::debug!("Response status: {}", resp.status());
        let bytes = resp.bytes()?;
        Ok(crate::serialization::open::<R, _>(&bytes, self.key.veil_key())?)
    }
}

// ─── ClientBuilder ────────────────────────────────────────────────────────────

/// Fluent builder for [`Client`] with optional timeout and other configuration.
///
/// Use this instead of the bare `Client::new_*` constructors when you need to
/// configure a request timeout.
///
/// # Example
///
/// ```rust,no_run
/// use std::time::Duration;
/// use toolkit_zero::socket::client::{ClientBuilder, Target};
///
/// // Async client with a 10-second timeout
/// let client = ClientBuilder::new(Target::Localhost(8080))
///     .timeout(Duration::from_secs(10))
///     .build_async()
///     .unwrap();
///
/// // Sync client with a 30-second timeout
/// let client = ClientBuilder::new(Target::Remote("https://api.example.com".to_string()))
///     .timeout(Duration::from_secs(30))
///     .build_sync()
///     .unwrap();
/// ```
pub struct ClientBuilder {
    target:  Target,
    timeout: Option<std::time::Duration>,
}

impl ClientBuilder {
    /// Create a new builder for the given [`Target`].
    pub fn new(target: Target) -> Self {
        Self { target, timeout: None }
    }

    /// Set a request timeout.
    ///
    /// Both the async and blocking reqwest clients will respect this duration.
    /// Requests that do not complete within the timeout are cancelled and return
    /// a [`reqwest::Error`] with `is_timeout()` = `true`.
    pub fn timeout(mut self, duration: std::time::Duration) -> Self {
        self.timeout = Some(duration);
        self
    }

    /// Build an **async-only** [`Client`]. Safe to call from any context,
    /// including inside `#[tokio::main]`.
    ///
    /// # Errors
    ///
    /// Returns [`reqwest::Error`] if the TLS backend cannot be initialized.
    pub fn build_async(self) -> Result<Client, reqwest::Error> {
        log::debug!("Building async-only client (timeout={:?})", self.timeout);
        let mut builder = AsyncClient::builder();
        if let Some(t) = self.timeout {
            builder = builder.timeout(t);
        }
        Ok(Client {
            target:       self.target,
            async_client: Some(builder.build()?),
            sync_client:  None,
        })
    }

    /// Build a **sync-only** [`Client`].
    ///
    /// # Panics
    ///
    /// Panics if called from within an async context (same restriction as
    /// `reqwest::blocking::Client`). See [`Client::new_sync`] for details.
    ///
    /// # Errors
    ///
    /// Returns [`reqwest::Error`] if the TLS backend cannot be initialized.
    pub fn build_sync(self) -> Result<Client, reqwest::Error> {
        log::debug!("Building sync-only client (timeout={:?})", self.timeout);
        let mut builder = BlockingClient::builder();
        if let Some(t) = self.timeout {
            builder = builder.timeout(t);
        }
        Ok(Client {
            target:       self.target,
            async_client: None,
            sync_client:  Some(builder.build()?),
        })
    }

    /// Build a client that supports **both** async and blocking sends.
    ///
    /// # Panics
    ///
    /// Panics if called from within an async context. See [`Client::new`] for details.
    ///
    /// # Errors
    ///
    /// Returns [`reqwest::Error`] if the TLS backend cannot be initialized.
    pub fn build(self) -> Result<Client, reqwest::Error> {
        if tokio::runtime::Handle::try_current().is_ok() {
            panic!(
                "ClientBuilder::build() called inside an async context. \
                 Use ClientBuilder::build_async() for async-only clients."
            );
        }
        log::debug!("Building dual async+sync client (timeout={:?})", self.timeout);
        let mut async_builder   = AsyncClient::builder();
        let mut sync_builder    = BlockingClient::builder();
        if let Some(t) = self.timeout {
            async_builder = async_builder.timeout(t);
            sync_builder  = sync_builder.timeout(t);
        }
        Ok(Client {
            target:       self.target,
            async_client: Some(async_builder.build()?),
            sync_client:  Some(sync_builder.build()?),
        })
    }
}