workos 1.0.0

Official Rust SDK for the WorkOS API
Documentation
// Code generated by oagen. DO NOT EDIT.

use crate::client::Client;
#[allow(unused_imports)]
use crate::enums::*;
use crate::error::Error;
#[allow(unused_imports)]
use crate::models::*;
use serde::Serialize;

pub struct FeatureFlagsApi<'a> {
    pub(crate) client: &'a Client,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListFeatureFlagsParams {
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Upper limit on the number of objects to return, between `1` and `100`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Order the results by the creation time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<PaginationOrder>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListOrganizationFeatureFlagsParams {
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Upper limit on the number of objects to return, between `1` and `100`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Order the results by the creation time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<PaginationOrder>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListUserFeatureFlagsParams {
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Upper limit on the number of objects to return, between `1` and `100`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Order the results by the creation time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<PaginationOrder>,
}

impl<'a> FeatureFlagsApi<'a> {
    /// List feature flags
    ///
    /// Get a list of all of your existing feature flags matching the criteria specified.
    pub async fn list_feature_flags(
        &self,
        params: ListFeatureFlagsParams,
    ) -> Result<FlagList, Error> {
        self.list_feature_flags_with_options(params, None).await
    }

    /// Variant of [`Self::list_feature_flags`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_feature_flags_with_options(
        &self,
        params: ListFeatureFlagsParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FlagList, Error> {
        let path = "/feature-flags".to_string();
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Returns an async [`futures_util::Stream`] that yields every `Flag`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<Flag> = self
    ///     .list_feature_flags_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_feature_flags_auto_paging(
        &self,
        params: ListFeatureFlagsParams,
    ) -> impl futures_util::Stream<Item = Result<Flag, Error>> + '_ {
        crate::pagination::auto_paginate_pages(move |after| {
            let mut params = params.clone();
            params.after = after;
            async move {
                let page = self.list_feature_flags(params).await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }

    /// Get a feature flag
    ///
    /// Get the details of an existing feature flag by its slug.
    pub async fn get_feature_flag(&self, slug: &str) -> Result<Flag, Error> {
        self.get_feature_flag_with_options(slug, None).await
    }

    /// Variant of [`Self::get_feature_flag`] that accepts per-request [`crate::RequestOptions`].
    pub async fn get_feature_flag_with_options(
        &self,
        slug: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<Flag, Error> {
        let slug = crate::client::path_segment(slug);
        let path = format!("/feature-flags/{slug}");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Disable a feature flag
    ///
    /// Disables a feature flag in the current environment.
    pub async fn disable_feature_flag(&self, slug: &str) -> Result<FeatureFlag, Error> {
        self.disable_feature_flag_with_options(slug, None).await
    }

    /// Variant of [`Self::disable_feature_flag`] that accepts per-request [`crate::RequestOptions`].
    pub async fn disable_feature_flag_with_options(
        &self,
        slug: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FeatureFlag, Error> {
        let slug = crate::client::path_segment(slug);
        let path = format!("/feature-flags/{slug}/disable");
        let method = http::Method::PUT;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Enable a feature flag
    ///
    /// Enables a feature flag in the current environment.
    pub async fn enable_feature_flag(&self, slug: &str) -> Result<FeatureFlag, Error> {
        self.enable_feature_flag_with_options(slug, None).await
    }

    /// Variant of [`Self::enable_feature_flag`] that accepts per-request [`crate::RequestOptions`].
    pub async fn enable_feature_flag_with_options(
        &self,
        slug: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FeatureFlag, Error> {
        let slug = crate::client::path_segment(slug);
        let path = format!("/feature-flags/{slug}/enable");
        let method = http::Method::PUT;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Add a feature flag target
    ///
    /// Enables a feature flag for a specific target in the current environment. Currently, supported targets include users and organizations.
    pub async fn add_flag_target(&self, resource_id: &str, slug: &str) -> Result<(), Error> {
        self.add_flag_target_with_options(resource_id, slug, None)
            .await
    }

    /// Variant of [`Self::add_flag_target`] that accepts per-request [`crate::RequestOptions`].
    pub async fn add_flag_target_with_options(
        &self,
        resource_id: &str,
        slug: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<(), Error> {
        let resource_id = crate::client::path_segment(resource_id);
        let slug = crate::client::path_segment(slug);
        let path = format!("/feature-flags/{slug}/targets/{resource_id}");
        let method = http::Method::POST;
        self.client
            .request_with_query_opts_empty(method, &path, &(), options)
            .await
    }

    /// Remove a feature flag target
    ///
    /// Removes a target from the feature flag's target list in the current environment. Currently, supported targets include users and organizations.
    pub async fn remove_flag_target(&self, resource_id: &str, slug: &str) -> Result<(), Error> {
        self.remove_flag_target_with_options(resource_id, slug, None)
            .await
    }

    /// Variant of [`Self::remove_flag_target`] that accepts per-request [`crate::RequestOptions`].
    pub async fn remove_flag_target_with_options(
        &self,
        resource_id: &str,
        slug: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<(), Error> {
        let resource_id = crate::client::path_segment(resource_id);
        let slug = crate::client::path_segment(slug);
        let path = format!("/feature-flags/{slug}/targets/{resource_id}");
        let method = http::Method::DELETE;
        self.client
            .request_with_query_opts_empty(method, &path, &(), options)
            .await
    }

    /// List enabled feature flags for an organization
    ///
    /// Get a list of all enabled feature flags for an organization.
    pub async fn list_organization_feature_flags(
        &self,
        organization_id: &str,
        params: ListOrganizationFeatureFlagsParams,
    ) -> Result<FlagList, Error> {
        self.list_organization_feature_flags_with_options(organization_id, params, None)
            .await
    }

    /// Variant of [`Self::list_organization_feature_flags`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_organization_feature_flags_with_options(
        &self,
        organization_id: &str,
        params: ListOrganizationFeatureFlagsParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FlagList, Error> {
        let organization_id = crate::client::path_segment(organization_id);
        let path = format!("/organizations/{organization_id}/feature-flags");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Returns an async [`futures_util::Stream`] that yields every `Flag`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<Flag> = self
    ///     .list_organization_feature_flags_auto_paging(organization_id, params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_organization_feature_flags_auto_paging(
        &self,
        organization_id: impl Into<String>,
        params: ListOrganizationFeatureFlagsParams,
    ) -> impl futures_util::Stream<Item = Result<Flag, Error>> + '_ {
        let organization_id: String = organization_id.into();
        crate::pagination::auto_paginate_pages(move |after| {
            let organization_id = organization_id.clone();
            let mut params = params.clone();
            params.after = after;
            async move {
                let page = self
                    .list_organization_feature_flags(&organization_id, params)
                    .await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }

    /// List enabled feature flags for a user
    ///
    /// Get a list of all enabled feature flags for the provided user. This includes feature flags enabled specifically for the user as well as any organizations that the user is a member of.
    pub async fn list_user_feature_flags(
        &self,
        user_id: &str,
        params: ListUserFeatureFlagsParams,
    ) -> Result<FlagList, Error> {
        self.list_user_feature_flags_with_options(user_id, params, None)
            .await
    }

    /// Variant of [`Self::list_user_feature_flags`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_user_feature_flags_with_options(
        &self,
        user_id: &str,
        params: ListUserFeatureFlagsParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FlagList, Error> {
        let user_id = crate::client::path_segment(user_id);
        let path = format!("/user_management/users/{user_id}/feature-flags");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Returns an async [`futures_util::Stream`] that yields every `Flag`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<Flag> = self
    ///     .list_user_feature_flags_auto_paging(user_id, params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_user_feature_flags_auto_paging(
        &self,
        user_id: impl Into<String>,
        params: ListUserFeatureFlagsParams,
    ) -> impl futures_util::Stream<Item = Result<Flag, Error>> + '_ {
        let user_id: String = user_id.into();
        crate::pagination::auto_paginate_pages(move |after| {
            let user_id = user_id.clone();
            let mut params = params.clone();
            params.after = after;
            async move {
                let page = self.list_user_feature_flags(&user_id, params).await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }
}