Skip to main content

floopy/resources/
routing.rs

1use std::sync::Arc;
2
3use reqwest::Method;
4
5use crate::constants::ENDPOINT_ROUTING_EXPLAIN;
6use crate::error::Result;
7use crate::http::HttpTransport;
8use crate::options::RequestOptions;
9use crate::types::{RoutingExplainParams, RoutingExplainResult};
10
11use super::require;
12
13/// The routing dry-run (Pro plan).
14pub struct Routing {
15    t: Arc<HttpTransport>,
16}
17
18impl Routing {
19    pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
20        Self { t }
21    }
22
23    /// Run the router and firewall without calling a provider.
24    /// `would_select` is `None` when the firewall would block the request.
25    ///
26    /// # Errors
27    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
28    /// failure.
29    pub async fn explain(
30        &self,
31        params: RoutingExplainParams,
32        req: impl Into<Option<RequestOptions>>,
33    ) -> Result<RoutingExplainResult> {
34        let body =
35            serde_json::to_value(&params).map_err(|e| crate::Error::Decode(e.to_string()))?;
36        let (data, _) = self
37            .t
38            .request(
39                Method::POST,
40                ENDPOINT_ROUTING_EXPLAIN,
41                Some(&body),
42                &[],
43                req.into().as_ref(),
44            )
45            .await?;
46        require(data)
47    }
48}