ferrin-provider-util 0.2.0

Ferrin provider utilities: HTTP transport trait, reqwest transport, SSE decoding, secure URL policy, settings.
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
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
//! Response handlers: turn an [`HttpResponse`] into a typed value or error.

use std::marker::PhantomData;
use std::sync::Arc;

use bytes::Bytes;
use ferrin_spec::BoxFuture;
use ferrin_spec::BoxStream;
use ferrin_spec::Headers;
use ferrin_spec::JsonValue;
use ferrin_spec::error::ApiCallError;
use ferrin_spec::error::EmptyResponseBodyError;
use ferrin_spec::error::JsonParseError;
use ferrin_spec::error::ProviderError;
use ferrin_spec::error::TypeValidationError;
use futures_util::StreamExt;
use serde::de::DeserializeOwned;
use url::Url;

use super::body::DEFAULT_MAX_RESPONSE_BYTES;
use super::body::read_body;
use super::transport::BodyStream;
use super::transport::HttpResponse;
use super::transport::ResponseHead;
use super::transport::TransportError;
use crate::sse;
use crate::sse::SseStreamError;

/// Request information passed to handlers for error reporting.
#[derive(Debug, Clone)]
pub struct ResponseContext {
    /// Request URL.
    pub url: Url,
    /// JSON rendering of the request body, if any.
    pub request_body: Option<Arc<JsonValue>>,
}

impl ResponseContext {
    /// Creates a context.
    #[must_use]
    pub fn new(url: Url, request_body: Option<JsonValue>) -> Self {
        Self {
            url,
            request_body: request_body.map(Arc::new),
        }
    }

    /// Starts an [`ApiCallError`] for this request.
    #[must_use]
    pub fn api_error(&self, message: impl Into<String>) -> ApiCallError {
        let mut error = ApiCallError::new(message, self.url.clone());
        if let Some(body) = &self.request_body {
            error = error.with_request_body((**body).clone());
        }
        error
    }
}

/// Output of a handler.
#[derive(Debug)]
pub struct Handled<T> {
    /// The typed value.
    pub value: T,
    /// The raw JSON body when the handler parsed JSON.
    pub raw: Option<JsonValue>,
    /// Response headers.
    pub headers: Headers,
}

/// Interprets a response.
///
/// Handlers are cheap to clone into the returned future; they must not
/// borrow from `self` across the await.
pub trait ResponseHandler<T>: Send + Sync {
    /// Consumes the response.
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<T>, ProviderError>>;
}

/// Success and failure handlers for one request.
pub struct ResponseHandlers<T> {
    /// Handles 2xx responses.
    pub success: Box<dyn ResponseHandler<T>>,
    /// Handles non-2xx responses, producing the error to return.
    pub failure: Box<dyn ResponseHandler<ProviderError>>,
}

impl<T> std::fmt::Debug for ResponseHandlers<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResponseHandlers").finish_non_exhaustive()
    }
}

impl<T> ResponseHandlers<T> {
    /// Pairs a success handler with a failure handler.
    pub fn new(
        success: impl ResponseHandler<T> + 'static,
        failure: impl ResponseHandler<ProviderError> + 'static,
    ) -> Self {
        Self {
            success: Box::new(success),
            failure: Box::new(failure),
        }
    }
}

/// Result of parsing one streamed chunk.
#[derive(Debug)]
pub enum ParseResult<T> {
    /// The chunk parsed and validated.
    Ok {
        /// Typed value.
        value: T,
        /// Raw JSON value, for `include_raw_chunks`.
        raw: JsonValue,
    },
    /// The chunk could not be read, parsed or validated.
    Err {
        /// The failure.
        error: ProviderError,
        /// Raw text of the chunk when it was received.
        raw: Option<String>,
    },
}

impl<T> ParseResult<T> {
    /// Converts into a `Result`, dropping the raw payloads.
    ///
    /// # Errors
    ///
    /// Returns the parse error.
    pub fn into_result(self) -> Result<T, ProviderError> {
        match self {
            Self::Ok { value, .. } => Ok(value),
            Self::Err { error, .. } => Err(error),
        }
    }
}

/// Parses `text` as JSON and deserializes it into `T`.
#[must_use]
pub fn parse_json_chunk<T: DeserializeOwned>(text: &str) -> ParseResult<T> {
    match serde_json::from_str::<JsonValue>(text) {
        Ok(raw) => match serde_json::from_value::<T>(raw.clone()) {
            Ok(value) => ParseResult::Ok { value, raw },
            Err(error) => ParseResult::Err {
                error: TypeValidationError::new(raw, error).into(),
                raw: Some(text.to_owned()),
            },
        },
        Err(error) => ParseResult::Err {
            error: JsonParseError::new(text, error).into(),
            raw: Some(text.to_owned()),
        },
    }
}

async fn read_text(
    context: &ResponseContext,
    head: &ResponseHead,
    body: BodyStream,
    max_bytes: u64,
) -> Result<String, ProviderError> {
    let bytes = read_body(&head.headers, body, max_bytes)
        .await
        .map_err(|error| body_error(context, head, error))?;
    Ok(String::from_utf8_lossy(&bytes).into_owned())
}

fn body_error(
    context: &ResponseContext,
    head: &ResponseHead,
    error: TransportError,
) -> ProviderError {
    if error.is_cancelled() {
        return ProviderError::Cancelled;
    }
    let retryable = error.is_retryable();
    context
        .api_error(format!("failed to read response body: {}", error.message))
        .with_status(head.status)
        .with_response(head.headers.clone(), None)
        .retryable(retryable)
        .with_cause(error)
        .into()
}

/// Parses a JSON body into `T`, keeping the raw value.
pub struct JsonResponseHandler<T> {
    max_bytes: u64,
    _marker: PhantomData<fn() -> T>,
}

impl<T> std::fmt::Debug for JsonResponseHandler<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JsonResponseHandler")
            .field("max_bytes", &self.max_bytes)
            .finish()
    }
}

impl<T> JsonResponseHandler<T> {
    /// Creates the handler with the default body limit.
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_bytes: DEFAULT_MAX_RESPONSE_BYTES,
            _marker: PhantomData,
        }
    }

    /// Sets the body limit.
    #[must_use]
    pub fn with_max_bytes(mut self, max_bytes: u64) -> Self {
        self.max_bytes = max_bytes;
        self
    }
}

impl<T> Default for JsonResponseHandler<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: DeserializeOwned + Send + 'static> ResponseHandler<T> for JsonResponseHandler<T> {
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<T>, ProviderError>> {
        let max_bytes = self.max_bytes;
        Box::pin(async move {
            let head = response.head();
            let text = read_text(&context, &head, response.body, max_bytes).await?;
            match parse_json_chunk::<T>(&text) {
                ParseResult::Ok { value, raw } => Ok(Handled {
                    value,
                    raw: Some(raw),
                    headers: head.headers,
                }),
                ParseResult::Err { error, .. } => Err(context
                    .api_error("invalid JSON response")
                    .with_status(head.status)
                    .with_response(head.headers, Some(text))
                    .with_cause(error)
                    .into()),
            }
        })
    }
}

/// Parses a JSON body into `T`.
#[must_use]
pub fn json_response_handler<T>() -> JsonResponseHandler<T> {
    JsonResponseHandler::new()
}

/// Reads the body as text.
#[derive(Debug, Clone)]
pub struct TextResponseHandler {
    max_bytes: u64,
}

impl ResponseHandler<String> for TextResponseHandler {
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<String>, ProviderError>> {
        let max_bytes = self.max_bytes;
        Box::pin(async move {
            let head = response.head();
            let text = read_text(&context, &head, response.body, max_bytes).await?;
            Ok(Handled {
                value: text,
                raw: None,
                headers: head.headers,
            })
        })
    }
}

/// Reads the body as text.
#[must_use]
pub fn text_response_handler() -> TextResponseHandler {
    TextResponseHandler {
        max_bytes: DEFAULT_MAX_RESPONSE_BYTES,
    }
}

type ToMessage<E> = Arc<dyn Fn(&E) -> String + Send + Sync>;
type IsRetryable<E> = Arc<dyn Fn(&ResponseHead, Option<&E>) -> bool + Send + Sync>;

/// Parses an error body into `E` and builds an `ApiCallError` from it.
///
/// Empty or unparsable bodies produce an error whose message is the status
/// reason phrase.
pub struct JsonErrorResponseHandler<E> {
    to_message: ToMessage<E>,
    is_retryable: Option<IsRetryable<E>>,
    max_bytes: u64,
}

impl<E> std::fmt::Debug for JsonErrorResponseHandler<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JsonErrorResponseHandler")
            .field("max_bytes", &self.max_bytes)
            .finish_non_exhaustive()
    }
}

impl<E> Clone for JsonErrorResponseHandler<E> {
    fn clone(&self) -> Self {
        Self {
            to_message: Arc::clone(&self.to_message),
            is_retryable: self.is_retryable.clone(),
            max_bytes: self.max_bytes,
        }
    }
}

impl<E> JsonErrorResponseHandler<E> {
    /// Creates the handler with a message extractor.
    pub fn new(to_message: impl Fn(&E) -> String + Send + Sync + 'static) -> Self {
        Self {
            to_message: Arc::new(to_message),
            is_retryable: None,
            max_bytes: DEFAULT_MAX_RESPONSE_BYTES,
        }
    }

    /// Overrides retryability based on the response and parsed error.
    #[must_use]
    pub fn with_is_retryable(
        mut self,
        is_retryable: impl Fn(&ResponseHead, Option<&E>) -> bool + Send + Sync + 'static,
    ) -> Self {
        self.is_retryable = Some(Arc::new(is_retryable));
        self
    }

    /// Sets the body limit.
    #[must_use]
    pub fn with_max_bytes(mut self, max_bytes: u64) -> Self {
        self.max_bytes = max_bytes;
        self
    }
}

impl<E: DeserializeOwned + Send + Sync + 'static> ResponseHandler<ProviderError>
    for JsonErrorResponseHandler<E>
{
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<ProviderError>, ProviderError>> {
        let this = self.clone();
        Box::pin(async move {
            let head = response.head();
            let text = read_text(&context, &head, response.body, this.max_bytes).await?;
            let status_message = head
                .status
                .canonical_reason()
                .unwrap_or("request failed")
                .to_owned();
            let (message, data, parsed) = if text.trim().is_empty() {
                (status_message, None, None)
            } else {
                match serde_json::from_str::<JsonValue>(&text) {
                    Ok(raw) => match serde_json::from_value::<E>(raw.clone()) {
                        Ok(parsed) => ((this.to_message)(&parsed), Some(raw), Some(parsed)),
                        Err(_) => (status_message, None, None),
                    },
                    Err(_) => (status_message, None, None),
                }
            };
            let mut error = context
                .api_error(message)
                .with_status(head.status)
                .with_response(head.headers.clone(), Some(text));
            if let Some(data) = data {
                error = error.with_data(data);
            }
            if let Some(is_retryable) = &this.is_retryable {
                error = error.retryable(is_retryable(&head, parsed.as_ref()));
            }
            Ok(Handled {
                value: error.into(),
                raw: None,
                headers: head.headers,
            })
        })
    }
}

/// Builds a [`JsonErrorResponseHandler`].
pub fn json_error_response_handler<E>(
    to_message: impl Fn(&E) -> String + Send + Sync + 'static,
) -> JsonErrorResponseHandler<E> {
    JsonErrorResponseHandler::new(to_message)
}

/// Produces an `ApiCallError` from the status code and body text.
#[derive(Debug, Clone)]
pub struct StatusCodeErrorResponseHandler {
    max_bytes: u64,
}

impl ResponseHandler<ProviderError> for StatusCodeErrorResponseHandler {
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<ProviderError>, ProviderError>> {
        let max_bytes = self.max_bytes;
        Box::pin(async move {
            let head = response.head();
            let text = read_text(&context, &head, response.body, max_bytes).await?;
            let error = context
                .api_error(head.status.canonical_reason().unwrap_or("request failed"))
                .with_status(head.status)
                .with_response(head.headers.clone(), Some(text));
            Ok(Handled {
                value: error.into(),
                raw: None,
                headers: head.headers,
            })
        })
    }
}

/// Builds a [`StatusCodeErrorResponseHandler`].
#[must_use]
pub fn status_code_error_response_handler() -> StatusCodeErrorResponseHandler {
    StatusCodeErrorResponseHandler {
        max_bytes: DEFAULT_MAX_RESPONSE_BYTES,
    }
}

/// Reads the whole body as bytes.
#[derive(Debug, Clone)]
pub struct BinaryResponseHandler {
    max_bytes: u64,
}

impl BinaryResponseHandler {
    /// Sets the body limit.
    #[must_use]
    pub fn with_max_bytes(mut self, max_bytes: u64) -> Self {
        self.max_bytes = max_bytes;
        self
    }
}

impl ResponseHandler<Bytes> for BinaryResponseHandler {
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<Bytes>, ProviderError>> {
        let max_bytes = self.max_bytes;
        Box::pin(async move {
            let head = response.head();
            let bytes = read_body(&head.headers, response.body, max_bytes)
                .await
                .map_err(|error| body_error(&context, &head, error))?;
            Ok(Handled {
                value: bytes,
                raw: None,
                headers: head.headers,
            })
        })
    }
}

/// Builds a [`BinaryResponseHandler`].
#[must_use]
pub fn binary_response_handler() -> BinaryResponseHandler {
    BinaryResponseHandler {
        max_bytes: DEFAULT_MAX_RESPONSE_BYTES,
    }
}

/// Passes the body stream through.
#[derive(Debug, Clone, Default)]
pub struct BinaryStreamResponseHandler;

impl ResponseHandler<BodyStream> for BinaryStreamResponseHandler {
    fn handle(
        &self,
        _context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<BodyStream>, ProviderError>> {
        Box::pin(async move {
            Ok(Handled {
                value: response.body,
                raw: None,
                headers: response.headers,
            })
        })
    }
}

/// Builds a [`BinaryStreamResponseHandler`].
#[must_use]
pub fn binary_stream_response_handler() -> BinaryStreamResponseHandler {
    BinaryStreamResponseHandler
}

pub(super) fn stream_error(
    context: &ResponseContext,
    head: &ResponseHead,
    error: TransportError,
) -> ProviderError {
    if error.is_cancelled() {
        return ProviderError::Cancelled;
    }
    let retryable = error.is_retryable();
    context
        .api_error(format!(
            "failed to process successful response: {}",
            error.message
        ))
        .with_status(head.status)
        .with_response(head.headers.clone(), None)
        .retryable(retryable)
        .with_cause(error)
        .into()
}

/// Decodes a server-sent-event body into JSON chunks of type `T`.
///
/// `data: [DONE]` events are skipped; parse failures and transport errors
/// become [`ParseResult::Err`] items so the stream keeps going.
pub struct EventSourceResponseHandler<T> {
    max_event_bytes: usize,
    _marker: PhantomData<fn() -> T>,
}

impl<T> std::fmt::Debug for EventSourceResponseHandler<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EventSourceResponseHandler")
            .field("max_event_bytes", &self.max_event_bytes)
            .finish()
    }
}

impl<T> EventSourceResponseHandler<T> {
    /// Creates the handler with the default event size limit.
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_event_bytes: sse::DEFAULT_MAX_EVENT_BYTES,
            _marker: PhantomData,
        }
    }

    /// Sets the maximum size of one event.
    #[must_use]
    pub fn with_max_event_bytes(mut self, max_event_bytes: usize) -> Self {
        self.max_event_bytes = max_event_bytes;
        self
    }
}

impl<T> Default for EventSourceResponseHandler<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: DeserializeOwned + Send + 'static> ResponseHandler<BoxStream<'static, ParseResult<T>>>
    for EventSourceResponseHandler<T>
{
    fn handle(
        &self,
        context: ResponseContext,
        response: HttpResponse,
    ) -> BoxFuture<'static, Result<Handled<BoxStream<'static, ParseResult<T>>>, ProviderError>>
    {
        let max_event_bytes = self.max_event_bytes;
        Box::pin(async move {
            if response.headers.get_str("content-length") == Some("0") {
                return Err(EmptyResponseBodyError::new().into());
            }
            let head = response.head();
            let events = sse::decode_stream(response.body, max_event_bytes);
            let stream = events.filter_map(move |item| {
                let context = context.clone();
                let head = head.clone();
                async move {
                    match item {
                        Ok(event) if event.data == "[DONE]" => None,
                        Ok(event) => Some(parse_json_chunk::<T>(&event.data)),
                        Err(SseStreamError::Transport(error)) => Some(ParseResult::Err {
                            error: stream_error(&context, &head, error),
                            raw: None,
                        }),
                        Err(SseStreamError::Decode(error)) => Some(ParseResult::Err {
                            error: context
                                .api_error(format!("invalid event stream: {error}"))
                                .with_status(head.status)
                                .with_response(head.headers.clone(), None)
                                .into(),
                            raw: None,
                        }),
                    }
                }
            });
            let value: BoxStream<'static, ParseResult<T>> = Box::pin(stream);
            Ok(Handled {
                value,
                raw: None,
                headers: response.headers,
            })
        })
    }
}

/// Builds an [`EventSourceResponseHandler`].
#[must_use]
pub fn event_source_response_handler<T>() -> EventSourceResponseHandler<T> {
    EventSourceResponseHandler::new()
}