fiberplane_models/providers/
http.rs

1use crate::debug_print_bytes;
2use bytes::Bytes;
3#[cfg(feature = "fp-bindgen")]
4use fp_bindgen::prelude::*;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7use std::fmt::{self, Debug, Formatter};
8use typed_builder::TypedBuilder;
9
10/// HTTP request options.
11#[derive(Clone, Debug, Deserialize, Serialize, TypedBuilder)]
12#[cfg_attr(
13    feature = "fp-bindgen",
14    derive(Serializable),
15    fp(rust_module = "fiberplane_models::providers")
16)]
17#[non_exhaustive]
18#[serde(rename_all = "camelCase")]
19pub struct HttpRequest {
20    pub url: String,
21    pub method: HttpRequestMethod,
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub headers: Option<HashMap<String, String>>,
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub body: Option<Bytes>,
26}
27
28/// Possible errors that may happen during an HTTP request.
29#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
30#[cfg_attr(
31    feature = "fp-bindgen",
32    derive(Serializable),
33    fp(rust_module = "fiberplane_models::providers")
34)]
35#[non_exhaustive]
36#[serde(tag = "type", rename_all = "snake_case")]
37pub enum HttpRequestError {
38    Offline,
39    NoRoute,
40    ConnectionRefused,
41    Timeout,
42    ResponseTooBig,
43    #[cfg_attr(feature = "fp-bindgen", fp(rename_all = "camelCase"))]
44    ServerError {
45        status_code: u16,
46        response: Bytes,
47    },
48    #[cfg_attr(feature = "fp-bindgen", fp(rename_all = "camelCase"))]
49    Other {
50        reason: String,
51    },
52}
53
54impl Debug for HttpRequestError {
55    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
56        match self {
57            Self::Offline => f.write_str("Offline"),
58            Self::NoRoute => f.write_str("NoRoute"),
59            Self::ConnectionRefused => f.write_str("ConnectionRefused"),
60            Self::Timeout => f.write_str("Timeout"),
61            Self::ResponseTooBig => f.write_str("ResponseTooBig"),
62            Self::ServerError {
63                status_code,
64                response,
65            } => f
66                .debug_struct("ServerError")
67                .field("status_code", status_code)
68                .field("response_length", &response.len())
69                .field("response", &debug_print_bytes(response))
70                .finish(),
71            Self::Other { reason } => f.debug_struct("Other").field("reason", reason).finish(),
72        }
73    }
74}
75
76/// HTTP request method.
77// Note: we use SCREAMING_SNAKE_CASE here because this is
78// effectively a constant
79#[derive(Clone, Debug, Deserialize, Serialize)]
80#[cfg_attr(
81    feature = "fp-bindgen",
82    derive(Serializable),
83    fp(rust_module = "fiberplane_models::providers")
84)]
85#[non_exhaustive]
86#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
87pub enum HttpRequestMethod {
88    Delete,
89    Get,
90    Head,
91    Post,
92}
93
94/// Response to an HTTP request.
95#[derive(Clone, Deserialize, Serialize, TypedBuilder)]
96#[cfg_attr(
97    feature = "fp-bindgen",
98    derive(Serializable),
99    fp(rust_module = "fiberplane_models::providers")
100)]
101#[non_exhaustive]
102#[serde(rename_all = "camelCase")]
103pub struct HttpResponse {
104    pub body: Bytes,
105    pub headers: HashMap<String, String>,
106    pub status_code: u16,
107}
108
109impl Debug for HttpResponse {
110    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
111        f.debug_struct("HttpResponse")
112            .field("status_code", &self.status_code)
113            .field("headers", &self.headers)
114            .field("body_length", &self.body.len())
115            .field("body", &debug_print_bytes(&self.body))
116            .finish()
117    }
118}