cloudflare-api 0.1.0

Typed Rust client bindings for the Cloudflare API generated from OpenAPI definitions.
Documentation
use super::types;
use crate::{
    ByteStream, ClientHooks, ClientInfo, Error, OperationInfo, RequestBuilderExt,
    ResponseValue, encode_path,
};

    ///Builder for [`Client::page_rules_list_page_rules`]
    ///
    ///[`Client::page_rules_list_page_rules`]: crate::Client::page_rules_list_page_rules
    #[derive(Debug, Clone)]
    pub struct PageRulesListPageRules<'a> {
        client: &'a crate::Client,
        zone_id: Result<types::ZonesSchemasIdentifier, String>,
        direction: Result<types::PageRulesListPageRulesDirection, String>,
        pattern: Result<types::PageRulesListPageRulesMatch, String>,
        order: Result<types::PageRulesListPageRulesOrder, String>,
        status: Result<types::PageRulesListPageRulesStatus, String>,
    }
    impl<'a> PageRulesListPageRules<'a> {
        pub fn new(client: &'a crate::Client) -> Self {
            Self {
                client: client,
                zone_id: Err("zone_id was not initialized".to_string()),
                direction: Err("direction was not initialized".to_string()),
                pattern: Err("pattern was not initialized".to_string()),
                order: Err("order was not initialized".to_string()),
                status: Err("status was not initialized".to_string()),
            }
        }

        pub fn zone_id<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::ZonesSchemasIdentifier>,
        {
            self.zone_id = value.try_into().map_err(|_| {
                "conversion to `ZonesSchemasIdentifier` for zone_id failed".to_string()
            });
            self
        }

        pub fn direction<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::PageRulesListPageRulesDirection>,
        {
            self.direction = value.try_into().map_err(|_| {
                "conversion to `PageRulesListPageRulesDirection` for direction failed".to_string()
            });
            self
        }

        pub fn pattern<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::PageRulesListPageRulesMatch>,
        {
            self.pattern = value.try_into().map_err(|_| {
                "conversion to `PageRulesListPageRulesMatch` for pattern failed".to_string()
            });
            self
        }

        pub fn order<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::PageRulesListPageRulesOrder>,
        {
            self.order = value.try_into().map_err(|_| {
                "conversion to `PageRulesListPageRulesOrder` for order failed".to_string()
            });
            self
        }

        pub fn status<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::PageRulesListPageRulesStatus>,
        {
            self.status = value.try_into().map_err(|_| {
                "conversion to `PageRulesListPageRulesStatus` for status failed".to_string()
            });
            self
        }

        ///Sends a `GET` request to `/zones/{zone_id}/pagerules`
        pub async fn send(
            self,
        ) -> Result<
            ResponseValue<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
            Error<()>,
        > {
            let Self {
                client,
                zone_id,
                direction,
                pattern,
                order,
                status,
            } = self;
            let zone_id = zone_id.map_err(Error::InvalidRequest)?;
            let direction = direction.map_err(Error::InvalidRequest)?;
            let pattern = pattern.map_err(Error::InvalidRequest)?;
            let order = order.map_err(Error::InvalidRequest)?;
            let status = status.map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/zones/{}/pagerules",
                client.baseurl,
                encode_path(&zone_id.to_string()),
            );
            let mut header_map = ::reqwest::header::HeaderMap::with_capacity(1usize);
            header_map.append(
                ::reqwest::header::HeaderName::from_static("api-version"),
                ::reqwest::header::HeaderValue::from_static(crate::Client::api_version()),
            );
            #[allow(unused_mut)]
            let mut request = client
                .client
                .get(url)
                .header(
                    ::reqwest::header::ACCEPT,
                    ::reqwest::header::HeaderValue::from_static("application/json"),
                )
                .query(&progenitor_client::QueryParam::new("direction", &direction))
                .query(&progenitor_client::QueryParam::new("match", &pattern))
                .query(&progenitor_client::QueryParam::new("order", &order))
                .query(&progenitor_client::QueryParam::new("status", &status))
                .headers(header_map)
                .build()?;
            let info = OperationInfo {
                operation_id: "page_rules_list_page_rules",
            };
            client.pre(&mut request, &info).await?;
            let result = client.exec(request, &info).await;
            client.post(&result, &info).await?;
            let response = result?;
            match response.status().as_u16() {
                200u16 => ResponseValue::from_response(response).await,
                _ => Err(Error::UnexpectedResponse(response)),
            }
        }
    }

    ///Builder for [`Client::page_rules_create_a_page_rule`]
    ///
    ///[`Client::page_rules_create_a_page_rule`]: crate::Client::page_rules_create_a_page_rule
    #[derive(Debug, Clone)]
    pub struct PageRulesCreateAPageRule<'a> {
        client: &'a crate::Client,
        zone_id: Result<types::ZonesSchemasIdentifier, String>,
        body: Result<types::builder::PageRulesCreateAPageRuleBody, String>,
    }
    impl<'a> PageRulesCreateAPageRule<'a> {
        pub fn new(client: &'a crate::Client) -> Self {
            Self {
                client: client,
                zone_id: Err("zone_id was not initialized".to_string()),
                body: Ok(::std::default::Default::default()),
            }
        }

        pub fn zone_id<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::ZonesSchemasIdentifier>,
        {
            self.zone_id = value.try_into().map_err(|_| {
                "conversion to `ZonesSchemasIdentifier` for zone_id failed".to_string()
            });
            self
        }

        pub fn body<V>(mut self, value: V) -> Self
        where
            V: std::convert::TryInto<types::PageRulesCreateAPageRuleBody>,
            <V as std::convert::TryInto<types::PageRulesCreateAPageRuleBody>>::Error:
                std::fmt::Display,
        {
            self.body = value.try_into().map(From::from).map_err(|s| {
                format!(
                    "conversion to `PageRulesCreateAPageRuleBody` for body failed: {}",
                    s
                )
            });
            self
        }

        pub fn body<F>(mut self, f: F) -> Self
        where
            F: std::ops::FnOnce(
                types::builder::PageRulesCreateAPageRuleBody,
            ) -> types::builder::PageRulesCreateAPageRuleBody,
        {
            self.body = self.body.map(f);
            self
        }

        ///Sends a `POST` request to `/zones/{zone_id}/pagerules`
        pub async fn send(
            self,
        ) -> Result<
            ResponseValue<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
            Error<()>,
        > {
            let Self {
                client,
                zone_id,
                body,
            } = self;
            let zone_id = zone_id.map_err(Error::InvalidRequest)?;
            let body = body
                .and_then(|v| {
                    types::PageRulesCreateAPageRuleBody::try_from(v).map_err(|e| e.to_string())
                })
                .map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/zones/{}/pagerules",
                client.baseurl,
                encode_path(&zone_id.to_string()),
            );
            let mut header_map = ::reqwest::header::HeaderMap::with_capacity(1usize);
            header_map.append(
                ::reqwest::header::HeaderName::from_static("api-version"),
                ::reqwest::header::HeaderValue::from_static(crate::Client::api_version()),
            );
            #[allow(unused_mut)]
            let mut request = client
                .client
                .post(url)
                .header(
                    ::reqwest::header::ACCEPT,
                    ::reqwest::header::HeaderValue::from_static("application/json"),
                )
                .json(&body)
                .headers(header_map)
                .build()?;
            let info = OperationInfo {
                operation_id: "page_rules_create_a_page_rule",
            };
            client.pre(&mut request, &info).await?;
            let result = client.exec(request, &info).await;
            client.post(&result, &info).await?;
            let response = result?;
            match response.status().as_u16() {
                200u16 => ResponseValue::from_response(response).await,
                _ => Err(Error::UnexpectedResponse(response)),
            }
        }
    }

mod by;

pub use by::*;