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::cloudflare_images_list_images`]
    ///
    ///[`Client::cloudflare_images_list_images`]: crate::Client::cloudflare_images_list_images
    #[derive(Debug, Clone)]
    pub struct CloudflareImagesListImages<'a> {
        client: &'a crate::Client,
        account_id: Result<types::ImagesAccountIdentifier, String>,
        creator: Result<Option<::std::string::String>, String>,
        page: Result<f64, String>,
        per_page: Result<f64, String>,
    }
    impl<'a> CloudflareImagesListImages<'a> {
        pub fn new(client: &'a crate::Client) -> Self {
            Self {
                client: client,
                account_id: Err("account_id was not initialized".to_string()),
                creator: Ok(None),
                page: Err("page was not initialized".to_string()),
                per_page: Err("per_page was not initialized".to_string()),
            }
        }

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

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

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

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

        ///Sends a `GET` request to `/accounts/{account_id}/images/v1`
        pub async fn send(
            self,
        ) -> Result<
            ResponseValue<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
            Error<()>,
        > {
            let Self {
                client,
                account_id,
                creator,
                page,
                per_page,
            } = self;
            let account_id = account_id.map_err(Error::InvalidRequest)?;
            let creator = creator.map_err(Error::InvalidRequest)?;
            let page = page.map_err(Error::InvalidRequest)?;
            let per_page = per_page.map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/accounts/{}/images/v1",
                client.baseurl,
                encode_path(&account_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("creator", &creator))
                .query(&progenitor_client::QueryParam::new("page", &page))
                .query(&progenitor_client::QueryParam::new("per_page", &per_page))
                .headers(header_map)
                .build()?;
            let info = OperationInfo {
                operation_id: "cloudflare_images_list_images",
            };
            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::cloudflare_images_upload_an_image_via_url`]
    ///
    ///[`Client::cloudflare_images_upload_an_image_via_url`]: crate::Client::cloudflare_images_upload_an_image_via_url
    #[derive(Debug, Clone)]
    pub struct CloudflareImagesUploadAnImageViaUrl<'a> {
        client: &'a crate::Client,
        account_id: Result<types::ImagesAccountIdentifier, String>,
        body: Result<types::builder::ImagesImageBasicUpload, String>,
    }
    impl<'a> CloudflareImagesUploadAnImageViaUrl<'a> {
        pub fn new(client: &'a crate::Client) -> Self {
            Self {
                client: client,
                account_id: Err("account_id was not initialized".to_string()),
                body: Ok(::std::default::Default::default()),
            }
        }

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

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

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

        ///Sends a `POST` request to `/accounts/{account_id}/images/v1`
        pub async fn send(
            self,
        ) -> Result<
            ResponseValue<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
            Error<()>,
        > {
            let Self {
                client,
                account_id,
                body,
            } = self;
            let account_id = account_id.map_err(Error::InvalidRequest)?;
            let body = body
                .and_then(|v| types::ImagesImageBasicUpload::try_from(v).map_err(|e| e.to_string()))
                .map_err(Error::InvalidRequest)?;
            let url = format!(
                "{}/accounts/{}/images/v1",
                client.baseurl,
                encode_path(&account_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: "cloudflare_images_upload_an_image_via_url",
            };
            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 keys;
mod stats;
mod variants;

pub use keys::*;
pub use stats::*;
pub use variants::*;