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
//! The HTTP layer underneath [`CanopyClient`](crate::CanopyClient).
//!
//! Everything above this layer — the generated wire types, the per-endpoint
//! methods, gzipping, status handling, JSON parsing — is transport-agnostic. A
//! caller that can't (or doesn't want to) reach canopy over
//! [`ReqwestTransport`](crate::ReqwestTransport), the default, implements
//! [`CanopyTransport`] and keeps the whole typed interface on top of it.
use Arc;
use Bytes;
use Result;
/// A request built by [`CanopyClient`](crate::CanopyClient), ready for a
/// [`CanopyTransport`] to send.
///
/// The URI is the endpoint **path** in origin form (path plus query, no scheme
/// or authority), e.g. `/backup-target` — resolving it against a base URL is
/// the transport's job. The body is already serialised and gzipped when there is
/// one (with `content-type` and `content-encoding` set to match) and empty when
/// there isn't.
pub type CanopyRequest = Request;
/// A response handed back to [`CanopyClient`](crate::CanopyClient) by a
/// [`CanopyTransport`], with its body buffered.
///
/// The status is interpreted by the client: a non-2xx becomes a
/// [`CanopyHttpError`](crate::CanopyHttpError) carrying the body, and a success
/// has its body parsed into the endpoint's response type.
pub type CanopyResponse = Response;
/// The HTTP transport a [`CanopyClient`](crate::CanopyClient) sends through.
///
/// Implement this to route canopy calls somewhere of your own choosing — a
/// proxy that isn't a plain HTTP proxy, an in-process handler, a recorded
/// fixture in tests — and pass it to
/// [`CanopyClient::with_transport`](crate::CanopyClient::with_transport). The
/// generated per-endpoint methods, the wire types, and the error handling all
/// work unchanged on top; callers who don't need this get
/// [`ReqwestTransport`](crate::ReqwestTransport) and never see this trait.
///
/// # Contract
///
/// - Requests arrive with a path-only URI (see [`CanopyRequest`]); the transport
/// decides what host, scheme, and auth to use, and may rewrite the path (the
/// default transport prefixes `/public` when it goes over the tailnet).
/// - Return canopy's response as-is, non-2xx included: statuses are the client's
/// to interpret, since endpoints give meaning to specific codes (e.g. a
/// backup-target `412` means the device is dormant, see
/// [`TargetOutcome::from_result`](crate::TargetOutcome::from_result)).
/// - `Err` is for a failure to obtain any response at all (connect, timeout,
/// protocol error).
///
/// # Example
///
/// ```no_run
/// use bestool_canopy::{
/// CanopyClient, CanopyRequest, CanopyResponse, CanopyTransport, async_trait,
/// };
/// use miette::Result;
///
/// struct MyProxy;
///
/// #[async_trait]
/// impl CanopyTransport for MyProxy {
/// async fn call(&self, request: CanopyRequest) -> Result<CanopyResponse> {
/// // Hand `request` to whatever reaches canopy from here, and return
/// // what comes back.
/// todo!()
/// }
/// }
///
/// # async fn example() -> Result<()> {
/// let client = CanopyClient::with_transport(MyProxy);
/// let servers = client.servers().await?;
/// # Ok(())
/// # }
/// ```