ferriskey-sdk 0.1.0

FerrisKey Rust SDK crate
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
524
525
526
527
//! SDK client entrypoint and request preparation helpers.
//!
//! ## Design Philosophy
//!
//! This module implements a TypeState pattern for the SDK builder, ensuring
//! that the transport layer is always configured before the SDK can be used.
//! The `FerriskeySdk<T>` type is parameterized by the transport, making
//! invalid states unrepresentable at compile time.
//!
//! ## Architecture
//!
//! ```text
//! FerriskeySdkBuilder<Unconfigured>
//!//!     ▼ .transport(transport)
//! FerriskeySdkBuilder<Configured<T>>
//!//!     ▼ .build()
//! FerriskeySdk<T>
//! ```
//!
//! ## tower::Service Integration
//!
//! The SDK accepts any `Transport` (which is a blanket impl over
//! `tower::Service<SdkRequest>`), enabling middleware composition:
//!
//! ```no_run
//! use ferriskey_sdk::{AuthStrategy, FerriskeySdk, HpxTransport, SdkConfig};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = SdkConfig::new("https://api.example.com", AuthStrategy::None);
//! let transport = HpxTransport::default();
//!
//! let sdk = FerriskeySdk::builder(config).transport(transport).build();
//! # Ok(())
//! # }
//! ```

use std::{collections::BTreeMap, future::Future, marker::PhantomData, pin::Pin};

use serde::de::DeserializeOwned;
use tower::{Service, ServiceExt};

use crate::{
    config::{AuthStrategy, SdkConfig},
    encoding::{DecodedResponse, decode_response, encode_request},
    error::SdkError,
    generated::{self, GeneratedOperationDescriptor},
    transport::{SdkRequest, SdkResponse, Transport},
};

// ---------------------------------------------------------------------------
// TypeState markers for FerriskeySdkBuilder
// ---------------------------------------------------------------------------

/// TypeState: transport has not been configured yet.
#[derive(Debug, Clone, Copy)]
pub struct Unconfigured;

/// TypeState: transport has been configured.
#[derive(Debug, Clone, Copy)]
pub struct Configured<T>(PhantomData<T>);

// ---------------------------------------------------------------------------
// OperationInput - Typed request input
// ---------------------------------------------------------------------------

/// Caller-provided request input for a generated FerrisKey operation.
///
/// ## Fluent Builder
///
/// Use [`OperationInput::builder()`] for a fluent API:
///
/// ```
/// use ferriskey_sdk::OperationInput;
///
/// let input = OperationInput::builder()
///     .path_param("id", "123")
///     .query_param("filter", vec!["active".to_string()])
///     .header("x-request-id", "abc")
///     .body(br#"{"name": "test"}"#)
///     .build();
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct OperationInput {
    /// Optional raw request body.
    pub body: Option<Vec<u8>>,
    /// Additional headers to apply to the generated request.
    pub headers: BTreeMap<String, String>,
    /// Path parameters keyed by their template name.
    pub path_params: BTreeMap<String, String>,
    /// Query parameters keyed by name and preserving repeated values.
    pub query_params: BTreeMap<String, Vec<String>>,
}

impl OperationInput {
    /// Create a new empty operation input.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a fluent builder for operation input.
    #[must_use]
    pub fn builder() -> OperationInputBuilder {
        OperationInputBuilder::default()
    }
}

/// Fluent builder for [`OperationInput`].
#[derive(Debug, Default)]
pub struct OperationInputBuilder {
    body: Option<Vec<u8>>,
    headers: BTreeMap<String, String>,
    path_params: BTreeMap<String, String>,
    query_params: BTreeMap<String, Vec<String>>,
}

impl OperationInputBuilder {
    /// Set the request body.
    #[must_use]
    pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = Some(body.into());
        self
    }

    /// Add a header.
    #[must_use]
    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(name.into(), value.into());
        self
    }

    /// Add a path parameter.
    #[must_use]
    pub fn path_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.path_params.insert(name.into(), value.into());
        self
    }

    /// Add a query parameter with a single value.
    #[must_use]
    pub fn query_param_single(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.query_params.insert(name.into(), vec![value.into()]);
        self
    }

    /// Add a query parameter with multiple values.
    #[must_use]
    pub fn query_param(mut self, name: impl Into<String>, values: Vec<String>) -> Self {
        self.query_params.insert(name.into(), values);
        self
    }

    /// Build the operation input.
    #[must_use]
    pub fn build(self) -> OperationInput {
        OperationInput {
            body: self.body,
            headers: self.headers,
            path_params: self.path_params,
            query_params: self.query_params,
        }
    }
}

// ---------------------------------------------------------------------------
// OperationCall - Bound operation
// ---------------------------------------------------------------------------

/// Generated operation entrypoint bound to a specific SDK instance.
///
/// ## Associated Types
///
/// The transport type `T` flows through the entire call chain, ensuring
/// type safety from SDK construction through request execution.
#[derive(Clone, Copy, Debug)]
pub struct OperationCall<'sdk, T: Transport + Clone> {
    descriptor: &'static GeneratedOperationDescriptor,
    sdk: &'sdk FerriskeySdk<T>,
}

impl<T: Transport + Clone> OperationCall<'_, T> {
    /// Access the generated descriptor for this operation.
    #[must_use]
    pub const fn descriptor(&self) -> &'static GeneratedOperationDescriptor {
        self.descriptor
    }

    /// Build a canonical SDK request for this generated operation.
    pub fn to_request(&self, input: OperationInput) -> Result<SdkRequest, SdkError> {
        encode_request(self.descriptor, input)
    }

    /// Execute this operation through the SDK transport.
    pub fn execute(
        &self,
        input: OperationInput,
    ) -> Pin<Box<dyn Future<Output = Result<SdkResponse, SdkError>> + Send + '_>>
    where
        <T as Service<SdkRequest>>::Future: Send,
    {
        Box::pin(async move {
            let request = self.to_request(input)?;
            self.sdk.execute(request).await
        })
    }

    /// Execute this operation and decode the documented response payload.
    pub fn execute_decoded(
        &self,
        input: OperationInput,
    ) -> Pin<Box<dyn Future<Output = Result<DecodedResponse, SdkError>> + Send + '_>>
    where
        <T as Service<SdkRequest>>::Future: Send,
    {
        Box::pin(async move {
            let response = self.execute(input).await?;
            decode_response(self.descriptor, response)
        })
    }
}

// ---------------------------------------------------------------------------
// TagClient - Tag-scoped view
// ---------------------------------------------------------------------------

/// Tag-scoped SDK view over the generated operation registry.
///
/// ## Extension Trait Pattern
///
/// Tag-specific convenience methods can be added via extension traits
/// without modifying the core `TagClient` type.
#[derive(Clone, Copy, Debug)]
pub struct TagClient<'sdk, T: Transport + Clone> {
    sdk: &'sdk FerriskeySdk<T>,
    tag: &'static str,
}

impl<T: Transport + Clone> TagClient<'_, T> {
    /// Access the tag name associated with this client.
    #[must_use]
    pub const fn tag(&self) -> &'static str {
        self.tag
    }

    /// Iterate over the generated descriptors assigned to this tag.
    pub fn descriptors(&self) -> impl Iterator<Item = &'static GeneratedOperationDescriptor> + '_ {
        generated::OPERATION_DESCRIPTORS.iter().filter(move |descriptor| descriptor.tag == self.tag)
    }

    /// Resolve an operation within this tag-scoped view.
    #[must_use]
    pub fn operation(&self, operation_id: &str) -> Option<OperationCall<'_, T>> {
        self.descriptors()
            .find(|descriptor| descriptor.operation_id == operation_id)
            .map(|descriptor| OperationCall { descriptor, sdk: self.sdk })
    }
}

// ---------------------------------------------------------------------------
// FerriskeySdk - Main SDK type
// ---------------------------------------------------------------------------

/// FerrisKey SDK entrypoint parameterized by a transport implementation.
///
/// ## Type-Driven Design
///
/// The generic parameter `T: Transport` ensures that:
/// 1. The transport type is known at compile time
/// 2. Invalid transport configurations are caught before runtime
/// 3. The compiler can optimize based on the concrete transport type
///
/// ## Builder Pattern
///
/// Use [`FerriskeySdk::builder()`] for a fluent, type-safe construction:
///
/// ```no_run
/// use ferriskey_sdk::{AuthStrategy, FerriskeySdk, HpxTransport, SdkConfig};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let config = SdkConfig::new("https://api.example.com", AuthStrategy::None);
/// let sdk = FerriskeySdk::builder(config).transport(HpxTransport::default()).build();
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct FerriskeySdk<T: Transport + Clone> {
    config: SdkConfig,
    transport: T,
}

impl<T: Transport + Clone> FerriskeySdk<T> {
    /// Construct a new SDK instance directly.
    ///
    /// Prefer using [`Self::builder()`] for a more fluent API.
    #[must_use]
    pub const fn new(config: SdkConfig, transport: T) -> Self {
        Self { config, transport }
    }

    /// Create a typed builder with the required configuration.
    ///
    /// The builder ensures the transport is set before calling `.build()`.
    #[must_use]
    pub const fn builder(config: SdkConfig) -> FerriskeySdkBuilder<T, Unconfigured> {
        FerriskeySdkBuilder { config, transport: None, _state: PhantomData }
    }

    /// Access the SDK configuration.
    #[must_use]
    pub const fn config(&self) -> &SdkConfig {
        &self.config
    }

    /// Access the underlying transport.
    #[must_use]
    pub const fn transport(&self) -> &T {
        &self.transport
    }

    /// Access the full generated operation registry.
    #[must_use]
    pub const fn operations(&self) -> &'static [GeneratedOperationDescriptor] {
        generated::OPERATION_DESCRIPTORS
    }

    /// Access a tag-scoped SDK view.
    #[must_use]
    pub const fn tag(&self, tag: &'static str) -> TagClient<'_, T> {
        TagClient { sdk: self, tag }
    }

    /// Resolve a generated operation by its operation ID.
    #[must_use]
    pub fn operation(&self, operation_id: &str) -> Option<OperationCall<'_, T>> {
        generated::OPERATION_DESCRIPTORS
            .iter()
            .find(|descriptor| descriptor.operation_id == operation_id)
            .map(|descriptor| OperationCall { descriptor, sdk: self })
    }

    /// Execute a generated operation through the canonical SDK request path.
    pub fn execute_operation(
        &self,
        operation_id: &str,
        input: OperationInput,
    ) -> Pin<Box<dyn Future<Output = Result<SdkResponse, SdkError>> + Send + '_>>
    where
        <T as Service<SdkRequest>>::Future: Send,
    {
        let resolved_operation = self.operation(operation_id);
        let requested_operation_id = operation_id.to_string();

        Box::pin(async move {
            let Some(operation) = resolved_operation else {
                return Err(SdkError::UnknownOperation { operation_id: requested_operation_id });
            };

            operation.execute(input).await
        })
    }

    /// Prepare a request by resolving its URL and applying auth.
    ///
    /// ## Design Decision: Result Type
    ///
    /// Returns `Result<SdkRequest, SdkError>` rather than panicking,
    /// enabling callers to handle configuration errors gracefully.
    pub fn prepare_request(&self, mut request: SdkRequest) -> Result<SdkRequest, SdkError> {
        request.path = resolve_url(self.config.base_url(), &request.path)?;

        if request.requires_auth {
            match self.config.auth() {
                AuthStrategy::Bearer(token) => {
                    request.headers.insert("authorization".to_string(), format!("Bearer {token}"));
                }
                AuthStrategy::None => return Err(SdkError::MissingAuth),
            }
        }

        Ok(request)
    }

    /// Execute a request through the configured transport.
    ///
    /// Uses `tower::ServiceExt::oneshot` for clean single-request execution.
    pub fn execute(
        &self,
        request: SdkRequest,
    ) -> Pin<Box<dyn Future<Output = Result<SdkResponse, SdkError>> + Send + '_>>
    where
        <T as Service<SdkRequest>>::Future: Send,
    {
        // We need to clone transport for the async block since oneshot consumes self
        let transport = self.transport.clone();

        Box::pin(async move {
            let prepared_request = self.prepare_request(request)?;

            // Use tower's oneshot for clean execution
            transport.oneshot(prepared_request).await.map_err(SdkError::Transport)
        })
    }

    /// Execute a request and decode a JSON response for the expected status.
    pub fn execute_json<Output>(
        &self,
        request: SdkRequest,
        expected_status: u16,
    ) -> Pin<Box<dyn Future<Output = Result<Output, SdkError>> + Send + '_>>
    where
        Output: DeserializeOwned + Send + 'static,
        <T as Service<SdkRequest>>::Future: Send,
    {
        Box::pin(async move {
            let response = self.execute(request).await?;

            if response.status != expected_status {
                return Err(SdkError::UnexpectedStatus {
                    expected: expected_status,
                    actual: response.status,
                });
            }

            serde_json::from_slice(&response.body).map_err(SdkError::Decode)
        })
    }
}

// ---------------------------------------------------------------------------
// FerriskeySdkBuilder - Type-safe builder
// ---------------------------------------------------------------------------

/// Typed builder for [`FerriskeySdk`] with compile-time validation.
///
/// ## Type-State Pattern
///
/// The builder uses phantom type parameters to track whether the transport
/// has been configured. Calling `.build()` before setting the transport
/// is a compile-time error.
#[derive(Debug)]
pub struct FerriskeySdkBuilder<T: Transport + Clone, S> {
    config: SdkConfig,
    transport: Option<T>,
    _state: PhantomData<S>,
}

impl<T: Transport + Clone> FerriskeySdkBuilder<T, Unconfigured> {
    /// Set the transport. Transitions to `Configured` state.
    #[must_use]
    pub fn transport(mut self, transport: T) -> FerriskeySdkBuilder<T, Configured<T>> {
        self.transport = Some(transport);
        FerriskeySdkBuilder { config: self.config, transport: self.transport, _state: PhantomData }
    }
}

impl<T: Transport + Clone> FerriskeySdkBuilder<T, Configured<T>> {
    /// Build the SDK instance. Available only when transport is configured.
    ///
    /// # Panics
    ///
    /// Panics if the transport was somehow not set (should be impossible
    /// due to type-state guarantees).
    #[must_use]
    #[expect(clippy::expect_used)]
    pub fn build(self) -> FerriskeySdk<T> {
        FerriskeySdk {
            config: self.config,
            transport: self.transport.expect("transport must be set in Configured state"),
        }
    }
}

// ---------------------------------------------------------------------------
// Extension traits for fluent API
// ---------------------------------------------------------------------------

/// Extension trait for convenient SDK construction.
pub trait SdkExt: Sized {
    /// The transport type for this SDK.
    type Transport: Transport + Clone;

    /// Create an SDK with a fluent one-liner.
    ///
    /// ```no_run
    /// use ferriskey_sdk::{AuthStrategy, FerriskeySdk, HpxTransport, SdkConfig, SdkExt};
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = SdkConfig::new("https://api.example.com", AuthStrategy::None);
    /// let sdk = FerriskeySdk::with_transport(config, HpxTransport::default());
    /// # Ok(())
    /// # }
    /// ```
    fn with_transport(
        config: SdkConfig,
        transport: Self::Transport,
    ) -> FerriskeySdk<Self::Transport>;
}

impl<T: Transport + Clone> SdkExt for FerriskeySdk<T> {
    type Transport = T;

    fn with_transport(config: SdkConfig, transport: T) -> Self {
        Self::new(config, transport)
    }
}

// ---------------------------------------------------------------------------
// URL resolution
// ---------------------------------------------------------------------------

/// Resolve a URL from base and path components.
fn resolve_url(base_url: &str, path: &str) -> Result<String, SdkError> {
    if path.starts_with("http://") || path.starts_with("https://") {
        return Ok(path.to_string());
    }

    let trimmed_base = base_url.trim_end_matches('/');
    let trimmed_path = path.trim_start_matches('/');

    if trimmed_base.is_empty() || trimmed_path.is_empty() {
        return Err(SdkError::InvalidUrl { base_url: base_url.to_string(), path: path.to_string() });
    }

    Ok(format!("{trimmed_base}/{trimmed_path}"))
}