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::members_retrieve`]
    ///
    ///[`Client::members_retrieve`]: crate::Client::members_retrieve
    #[derive(Debug, Clone)]
    pub struct MembersRetrieve<'a> {
        client: &'a crate::Client,
        organization_id: Result<types::OrganizationsApiOrganizationId, String>,
        member_id: Result<types::OrganizationsApiMemberId, String>,
    }
    impl<'a> MembersRetrieve<'a> {
        pub fn new(client: &'a crate::Client) -> Self {
            Self {
                client: client,
                organization_id: Err("organization_id was not initialized".to_string()),
                member_id: Err("member_id was not initialized".to_string()),
            }
        }

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

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

        ///Sends a `GET` request to
        /// `/organizations/{organization_id}/members/{member_id}`
        pub async fn send(
            self,
        ) -> Result<
            ResponseValue<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
            Error<()>,
        > {
            let Self {
                client,
                organization_id,
                member_id,
            } = self;
            let organization_id = organization_id.map_err(Error::InvalidRequest)?;
            let member_id = member_id.map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/organizations/{}/members/{}",
                client.baseurl,
                encode_path(&organization_id.to_string()),
                encode_path(&member_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"),
                )
                .headers(header_map)
                .build()?;
            let info = OperationInfo {
                operation_id: "members_retrieve",
            };
            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::members_delete`]
    ///
    ///[`Client::members_delete`]: crate::Client::members_delete
    #[derive(Debug, Clone)]
    pub struct MembersDelete<'a> {
        client: &'a crate::Client,
        organization_id: Result<types::OrganizationsApiOrganizationId, String>,
        member_id: Result<types::OrganizationsApiMemberId, String>,
        body: Result<types::builder::MembersDeleteBody, String>,
    }
    impl<'a> MembersDelete<'a> {
        pub fn new(client: &'a crate::Client) -> Self {
            Self {
                client: client,
                organization_id: Err("organization_id was not initialized".to_string()),
                member_id: Err("member_id was not initialized".to_string()),
                body: Ok(::std::default::Default::default()),
            }
        }

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

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

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

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

        ///Sends a `DELETE` request to
        /// `/organizations/{organization_id}/members/{member_id}`
        pub async fn send(self) -> Result<ResponseValue<()>, Error<()>> {
            let Self {
                client,
                organization_id,
                member_id,
                body,
            } = self;
            let organization_id = organization_id.map_err(Error::InvalidRequest)?;
            let member_id = member_id.map_err(Error::InvalidRequest)?;
            let body = body
                .and_then(|v| types::MembersDeleteBody::try_from(v).map_err(|e| e.to_string()))
                .map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/organizations/{}/members/{}",
                client.baseurl,
                encode_path(&organization_id.to_string()),
                encode_path(&member_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
                .delete(url)
                .json(&body)
                .headers(header_map)
                .build()?;
            let info = OperationInfo {
                operation_id: "members_delete",
            };
            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() {
                204u16 => Ok(ResponseValue::empty(response)),
                _ => Err(Error::UnexpectedResponse(response)),
            }
        }
    }

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

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

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

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

        ///Sends a `POST` request to
        /// `/organizations/{organization_id}/members:batchCreate`
        pub async fn send(
            self,
        ) -> Result<
            ResponseValue<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
            Error<()>,
        > {
            let Self {
                client,
                organization_id,
                body,
            } = self;
            let organization_id = organization_id.map_err(Error::InvalidRequest)?;
            let body = body
                .and_then(|v| {
                    types::OrganizationsApiBatchCreateMembersRequest::try_from(v)
                        .map_err(|e| e.to_string())
                })
                .map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/organizations/{}/members:batchCreate",
                client.baseurl,
                encode_path(&organization_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: "members_batch_create",
            };
            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)),
            }
        }
    }