Skip to main content

floopy/resources/
constraints.rs

1use std::sync::Arc;
2
3use reqwest::Method;
4
5use crate::constants::ENDPOINT_CONSTRAINTS;
6use crate::error::Result;
7use crate::http::HttpTransport;
8use crate::options::RequestOptions;
9use crate::types::OrgConstraints;
10
11use super::require;
12
13/// Reads and full-replaces org constraints.
14pub struct Constraints {
15    t: Arc<HttpTransport>,
16}
17
18impl Constraints {
19    pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
20        Self { t }
21    }
22
23    /// Return the current org constraints.
24    ///
25    /// # Errors
26    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
27    /// failure.
28    pub async fn get(&self, req: impl Into<Option<RequestOptions>>) -> Result<OrgConstraints> {
29        let (data, _) = self
30            .t
31            .request(
32                Method::GET,
33                ENDPOINT_CONSTRAINTS,
34                None,
35                &[],
36                req.into().as_ref(),
37            )
38            .await?;
39        require(data)
40    }
41
42    /// Full-replace the org constraints. Any field left `None` is reset to
43    /// `null` server-side (matches the gateway's PUT semantics).
44    ///
45    /// # Errors
46    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
47    /// failure.
48    pub async fn put(
49        &self,
50        constraints: &OrgConstraints,
51        req: impl Into<Option<RequestOptions>>,
52    ) -> Result<OrgConstraints> {
53        let body =
54            serde_json::to_value(constraints).map_err(|e| crate::Error::Decode(e.to_string()))?;
55        let (data, _) = self
56            .t
57            .request(
58                Method::PUT,
59                ENDPOINT_CONSTRAINTS,
60                Some(&body),
61                &[],
62                req.into().as_ref(),
63            )
64            .await?;
65        require(data)
66    }
67}