hyphae-client 0.1.0

Bounded asynchronous Rust client for the Hyphae /v1 API.
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
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
// SPDX-License-Identifier: Apache-2.0

//! Bounded asynchronous Rust client for the public Hyphae HTTP API.
//!
//! This crate depends only on public wire contracts. It does not import the
//! server, engine, storage, query, or retrieval implementations.

use std::time::Duration;

use hyphae_contracts::v1::{
    CapabilitiesV1, CommitReceiptV1, DeleteRequestV1, ErrorV1, GetRequestV1, GetResponseV1,
    HealthV1, ProofV1, PutRequestV1, QueryRequestV1, QueryResponseV1,
};
use reqwest::{
    Method, StatusCode, Url,
    header::{self, HeaderMap, HeaderValue},
};
use serde::{Serialize, de::DeserializeOwned};
use thiserror::Error;

const DEFAULT_RESPONSE_BYTES: usize = 32 * 1024 * 1024;
const DEFAULT_WITNESS_BYTES: usize = 512 * 1024 * 1024;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);

/// Successful typed API value and its response correlation identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ApiResponse<T> {
    /// Decoded public contract value.
    pub value: T,
    /// UUID copied from `X-Request-Id`.
    pub request_id: String,
}

/// Stable server-declared error received from `/v1`.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[error("Hyphae API returned HTTP {status} {code} (request {request_id})")]
pub struct ApiFailure {
    /// HTTP status code.
    pub status: u16,
    /// Stable machine-readable error code.
    pub code: String,
    /// Bounded server diagnostic.
    pub message: String,
    /// UUID matching the response header.
    pub request_id: String,
}

/// Invalid client configuration detected before any network operation.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
pub enum ClientConfigError {
    /// Base URL is not syntactically valid.
    #[error("invalid Hyphae base URL")]
    InvalidBaseUrl,
    /// Only explicit HTTP and HTTPS origins are supported.
    #[error("Hyphae base URL must use http or https")]
    UnsupportedScheme,
    /// Base URL must be one origin without credentials, query, fragment, or path prefix.
    #[error("Hyphae base URL must be an origin without credentials, query, fragment, or path")]
    NonOriginBaseUrl,
    /// Bearer secret cannot be represented safely in one header.
    #[error("invalid bearer token for an HTTP authorization header")]
    InvalidBearerToken,
    /// Every local client bound must be positive.
    #[error("client timeout and response limits must be nonzero")]
    ZeroLimit,
    /// Underlying HTTP client construction failed.
    #[error("failed to construct HTTP client")]
    HttpClient,
}

/// Transport, bound, envelope, or declared API failure.
#[derive(Debug, Error)]
pub enum ClientError {
    /// HTTP transport failed before a complete bounded response arrived.
    #[error("Hyphae HTTP transport failed: {0}")]
    Transport(#[from] reqwest::Error),
    /// Response declared or exceeded a local byte bound.
    #[error("Hyphae response exceeded local limit {maximum} bytes")]
    ResponseTooLarge {
        /// Configured maximum.
        maximum: usize,
    },
    /// A JSON operation returned a non-JSON media type.
    #[error("Hyphae response did not use a JSON content type")]
    InvalidContentType,
    /// JSON response did not match the public versioned contract.
    #[error("Hyphae response violated the version 1 contract: {0}")]
    InvalidJson(#[from] serde_json::Error),
    /// The request ID header was missing, duplicated, or malformed.
    #[error("Hyphae response has no single valid X-Request-Id header")]
    InvalidRequestId,
    /// Error envelope and header correlation identifiers disagree.
    #[error("Hyphae error envelope request ID differs from its response header")]
    RequestIdMismatch,
    /// Server returned a non-success response that conformed to `ErrorV1`.
    #[error(transparent)]
    Api(#[from] ApiFailure),
    /// A successful operation returned an unexpected HTTP status.
    #[error("Hyphae returned unexpected success status {0}")]
    UnexpectedStatus(u16),
    /// Proof witness reference was not canonical for its proof identity.
    #[error("proof contains a noncanonical witness reference")]
    InvalidWitnessReference,
    /// Downloaded witness digest header differs from the proof.
    #[error("downloaded witness digest header differs from the proof")]
    WitnessDigestMismatch,
    /// Downloaded witness length differs from the proof.
    #[error("downloaded witness length differs from the proof")]
    WitnessLengthMismatch,
}

/// Builder for a bounded public API client.
#[derive(Clone, Debug)]
#[must_use = "a client builder has no effect until build is called"]
pub struct ClientBuilder {
    base_url: Url,
    bearer_token: Option<HeaderValue>,
    timeout: Duration,
    response_bytes: usize,
    witness_bytes: usize,
}

impl ClientBuilder {
    /// Parses one root HTTP(S) origin.
    ///
    /// # Errors
    ///
    /// Rejects malformed URLs, non-HTTP schemes, credentials, query strings,
    /// fragments, and non-root paths.
    pub fn new(base_url: &str) -> Result<Self, ClientConfigError> {
        let mut base_url = Url::parse(base_url).map_err(|_| ClientConfigError::InvalidBaseUrl)?;
        if !matches!(base_url.scheme(), "http" | "https") {
            return Err(ClientConfigError::UnsupportedScheme);
        }
        if !base_url.username().is_empty()
            || base_url.password().is_some()
            || base_url.query().is_some()
            || base_url.fragment().is_some()
            || !matches!(base_url.path(), "" | "/")
        {
            return Err(ClientConfigError::NonOriginBaseUrl);
        }
        base_url.set_path("/");
        Ok(Self {
            base_url,
            bearer_token: None,
            timeout: DEFAULT_TIMEOUT,
            response_bytes: DEFAULT_RESPONSE_BYTES,
            witness_bytes: DEFAULT_WITNESS_BYTES,
        })
    }

    /// Configures an opaque bearer token without retaining a second copy.
    ///
    /// # Errors
    ///
    /// Rejects values that cannot be represented in one HTTP header.
    pub fn bearer_token(mut self, token: &str) -> Result<Self, ClientConfigError> {
        if token.is_empty() {
            return Err(ClientConfigError::InvalidBearerToken);
        }
        let mut value = HeaderValue::from_str(&format!("Bearer {token}"))
            .map_err(|_| ClientConfigError::InvalidBearerToken)?;
        value.set_sensitive(true);
        self.bearer_token = Some(value);
        Ok(self)
    }

    /// Sets the complete request/response deadline.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the maximum complete JSON response bytes.
    pub fn response_bytes(mut self, maximum: usize) -> Self {
        self.response_bytes = maximum;
        self
    }

    /// Sets the maximum complete snapshot witness bytes.
    pub fn witness_bytes(mut self, maximum: usize) -> Self {
        self.witness_bytes = maximum;
        self
    }

    /// Constructs the reusable client.
    ///
    /// # Errors
    ///
    /// Rejects zero limits or an underlying HTTP client configuration error.
    pub fn build(self) -> Result<HyphaeClient, ClientConfigError> {
        if self.timeout.is_zero() || self.response_bytes == 0 || self.witness_bytes == 0 {
            return Err(ClientConfigError::ZeroLimit);
        }
        let http = reqwest::Client::builder()
            .timeout(self.timeout)
            .build()
            .map_err(|_| ClientConfigError::HttpClient)?;
        Ok(HyphaeClient {
            http,
            base_url: self.base_url,
            bearer_token: self.bearer_token,
            response_bytes: self.response_bytes,
            witness_bytes: self.witness_bytes,
        })
    }
}

/// Reusable bounded client for Hyphae HTTP API version 1.
#[derive(Clone, Debug)]
pub struct HyphaeClient {
    http: reqwest::Client,
    base_url: Url,
    bearer_token: Option<HeaderValue>,
    response_bytes: usize,
    witness_bytes: usize,
}

impl HyphaeClient {
    /// Starts a client builder for one root origin.
    ///
    /// # Errors
    ///
    /// Returns a base-URL validation error.
    pub fn builder(base_url: &str) -> Result<ClientBuilder, ClientConfigError> {
        ClientBuilder::new(base_url)
    }

    /// Reports server capabilities and effective limits.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn capabilities(&self) -> Result<ApiResponse<CapabilitiesV1>, ClientError> {
        self.get_json("v1/capabilities", false).await
    }

    /// Reports process liveness.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn liveness(&self) -> Result<ApiResponse<HealthV1>, ClientError> {
        self.get_json("v1/health/live", false).await
    }

    /// Reports engine readiness.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn readiness(&self) -> Result<ApiResponse<HealthV1>, ClientError> {
        self.get_json("v1/health/ready", false).await
    }

    /// Atomically stores a structured-record batch.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn put(
        &self,
        request: &PutRequestV1,
    ) -> Result<ApiResponse<CommitReceiptV1>, ClientError> {
        self.post_json("v1/kv/put", request).await
    }

    /// Atomically deletes a binary-key batch.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn delete(
        &self,
        request: &DeleteRequestV1,
    ) -> Result<ApiResponse<CommitReceiptV1>, ClientError> {
        self.post_json("v1/kv/delete", request).await
    }

    /// Gets proven key presence or absence.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn get(
        &self,
        request: &GetRequestV1,
    ) -> Result<ApiResponse<GetResponseV1>, ClientError> {
        self.post_json("v1/kv/get", request).await
    }

    /// Executes a deterministic proof-bearing structured query.
    ///
    /// # Errors
    ///
    /// Returns a transport, bound, contract, correlation, or API error.
    pub async fn query(
        &self,
        request: &QueryRequestV1,
    ) -> Result<ApiResponse<QueryResponseV1>, ClientError> {
        self.post_json("v1/query", request).await
    }

    /// Downloads the exact snapshot witness referenced by a proof.
    ///
    /// # Errors
    ///
    /// Rejects a noncanonical reference, transport/bound/API failure, or a
    /// digest header that disagrees with the proof.
    pub async fn download_witness(
        &self,
        proof: &ProofV1,
    ) -> Result<ApiResponse<Vec<u8>>, ClientError> {
        let expected_path = format!(
            "/v1/witnesses/{}/{}",
            proof.checkpoint_sequence, proof.snapshot_digest
        );
        if proof.witness.path != expected_path {
            return Err(ClientError::InvalidWitnessReference);
        }
        if proof.witness.file_bytes > u64::try_from(self.witness_bytes).unwrap_or(u64::MAX) {
            return Err(ClientError::ResponseTooLarge {
                maximum: self.witness_bytes,
            });
        }
        let response = self
            .request(Method::GET, expected_path.trim_start_matches('/'), true)
            .send()
            .await?;
        if !response.status().is_success() {
            return Err(self.decode_api_failure(response).await?);
        }
        if response.status() != StatusCode::OK {
            return Err(ClientError::UnexpectedStatus(response.status().as_u16()));
        }
        let request_id = request_id(response.headers())?;
        let expected_digest = format!("blake3={}", proof.snapshot_digest);
        if single_header(response.headers(), "digest") != Some(expected_digest.as_str()) {
            return Err(ClientError::WitnessDigestMismatch);
        }
        let value = read_bounded(response, self.witness_bytes).await?;
        if u64::try_from(value.len()) != Ok(proof.witness.file_bytes) {
            return Err(ClientError::WitnessLengthMismatch);
        }
        Ok(ApiResponse { value, request_id })
    }

    async fn get_json<T: DeserializeOwned>(
        &self,
        path: &str,
        authenticated: bool,
    ) -> Result<ApiResponse<T>, ClientError> {
        let response = self
            .request(Method::GET, path, authenticated)
            .send()
            .await?;
        self.decode_json(response).await
    }

    async fn post_json<RequestBody: Serialize, ResponseBody: DeserializeOwned>(
        &self,
        path: &str,
        request: &RequestBody,
    ) -> Result<ApiResponse<ResponseBody>, ClientError> {
        let response = self
            .request(Method::POST, path, true)
            .json(request)
            .send()
            .await?;
        self.decode_json(response).await
    }

    fn request(&self, method: Method, path: &str, authenticated: bool) -> reqwest::RequestBuilder {
        let mut request = self.http.request(method, self.endpoint(path));
        if authenticated && let Some(token) = &self.bearer_token {
            request = request.header(header::AUTHORIZATION, token.clone());
        }
        request
    }

    fn endpoint(&self, path: &str) -> Url {
        let mut endpoint = self.base_url.clone();
        endpoint.set_path(&format!("/{}", path.trim_start_matches('/')));
        endpoint
    }

    async fn decode_json<T: DeserializeOwned>(
        &self,
        response: reqwest::Response,
    ) -> Result<ApiResponse<T>, ClientError> {
        if !response.status().is_success() {
            return Err(self.decode_api_failure(response).await?);
        }
        if response.status() != StatusCode::OK {
            return Err(ClientError::UnexpectedStatus(response.status().as_u16()));
        }
        require_json(response.headers())?;
        let request_id = request_id(response.headers())?;
        let encoded = read_bounded(response, self.response_bytes).await?;
        Ok(ApiResponse {
            value: serde_json::from_slice(&encoded)?,
            request_id,
        })
    }

    async fn decode_api_failure(
        &self,
        response: reqwest::Response,
    ) -> Result<ClientError, ClientError> {
        let status = response.status().as_u16();
        require_json(response.headers())?;
        let header_request_id = request_id(response.headers())?;
        let encoded = read_bounded(response, self.response_bytes).await?;
        let envelope: ErrorV1 = serde_json::from_slice(&encoded)?;
        if envelope.request_id != header_request_id {
            return Err(ClientError::RequestIdMismatch);
        }
        Ok(ClientError::Api(ApiFailure {
            status,
            code: envelope.code,
            message: envelope.message,
            request_id: envelope.request_id,
        }))
    }
}

async fn read_bounded(
    mut response: reqwest::Response,
    maximum: usize,
) -> Result<Vec<u8>, ClientError> {
    if response
        .content_length()
        .is_some_and(|length| length > u64::try_from(maximum).unwrap_or(u64::MAX))
    {
        return Err(ClientError::ResponseTooLarge { maximum });
    }
    let mut encoded = Vec::new();
    while let Some(chunk) = response.chunk().await? {
        let next_length = encoded
            .len()
            .checked_add(chunk.len())
            .ok_or(ClientError::ResponseTooLarge { maximum })?;
        if next_length > maximum {
            return Err(ClientError::ResponseTooLarge { maximum });
        }
        encoded.extend_from_slice(&chunk);
    }
    Ok(encoded)
}

fn require_json(headers: &HeaderMap) -> Result<(), ClientError> {
    let content_type = single_header(headers, header::CONTENT_TYPE.as_str())
        .ok_or(ClientError::InvalidContentType)?;
    let media_type = content_type
        .split(';')
        .next()
        .unwrap_or_default()
        .trim()
        .to_ascii_lowercase();
    if media_type == "application/json"
        || (media_type.starts_with("application/") && media_type.ends_with("+json"))
    {
        Ok(())
    } else {
        Err(ClientError::InvalidContentType)
    }
}

fn request_id(headers: &HeaderMap) -> Result<String, ClientError> {
    single_header(headers, "x-request-id")
        .map(ToOwned::to_owned)
        .ok_or(ClientError::InvalidRequestId)
}

fn single_header<'headers>(headers: &'headers HeaderMap, name: &str) -> Option<&'headers str> {
    let mut values = headers.get_all(name).iter();
    let value = values.next()?;
    if values.next().is_some() {
        return None;
    }
    value.to_str().ok()
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::{ClientBuilder, ClientConfigError};

    #[test]
    fn builder_accepts_only_bounded_root_http_origins() -> Result<(), ClientConfigError> {
        ClientBuilder::new("http://127.0.0.1:8787")?.build()?;
        assert!(matches!(
            ClientBuilder::new("file:///tmp/hyphae"),
            Err(ClientConfigError::UnsupportedScheme)
        ));
        assert!(matches!(
            ClientBuilder::new("https://user@example.test/"),
            Err(ClientConfigError::NonOriginBaseUrl)
        ));
        assert!(matches!(
            ClientBuilder::new("https://example.test/prefix"),
            Err(ClientConfigError::NonOriginBaseUrl)
        ));
        Ok(())
    }

    #[test]
    fn builder_rejects_invalid_secrets_and_zero_limits() -> Result<(), ClientConfigError> {
        assert!(matches!(
            ClientBuilder::new("http://localhost")?.bearer_token("bad\nsecret"),
            Err(ClientConfigError::InvalidBearerToken)
        ));
        assert!(matches!(
            ClientBuilder::new("http://localhost")?
                .timeout(Duration::ZERO)
                .build(),
            Err(ClientConfigError::ZeroLimit)
        ));
        Ok(())
    }
}