scaleway-rs 0.2.4

A pure Rust scaleway API binding.
Documentation
use crate::ScalewayApi;
use crate::ScalewayError;
use crate::data::ssh_key::ScalewaySSHKey;
use serde::Serialize;

#[derive(Serialize, Debug)]
struct CreateSSHKeyConfig {
    name: String,
    public_key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    project_id: Option<String>,
}

pub struct ScalewayCreateSSHKeyBuilder {
    api: ScalewayApi,
    config: CreateSSHKeyConfig,
}

impl ScalewayCreateSSHKeyBuilder {
    pub fn new(api: ScalewayApi, name: &str, public_key: &str) -> Self {
        ScalewayCreateSSHKeyBuilder {
            api,
            config: CreateSSHKeyConfig {
                name: name.to_string(),
                public_key: public_key.to_string(),
                project_id: None,
            },
        }
    }

    pub fn project_id(mut self, project_id: &str) -> ScalewayCreateSSHKeyBuilder {
        self.config.project_id = Some(project_id.to_string());
        self
    }

    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<ScalewaySSHKey, ScalewayError> {
        Ok(self
            .api
            .post(
                "https://api.scaleway.com/iam/v1alpha1/ssh-keys",
                self.config,
            )?
            .json::<ScalewaySSHKey>()?)
    }

    pub async fn run_async(self) -> Result<ScalewaySSHKey, ScalewayError> {
        Ok(self
            .api
            .post_async(
                "https://api.scaleway.com/iam/v1alpha1/ssh-keys",
                self.config,
            )
            .await?
            .json::<ScalewaySSHKey>()
            .await?)
    }
}