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 OrganizationsApi<'a> {
    pub(crate) client: &'a Client,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListOrganizationsParams {
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
    #[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. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
    #[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. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to descending.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<PaginationOrder>,
    /// The domains of an Organization. Any Organization with a matching domain will be returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domains: Option<Vec<String>>,
    /// Searchable text for an Organization. Matches against the organization name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateOrganizationParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: OrganizationInput,
}

impl CreateOrganizationParams {
    /// Construct a new `CreateOrganizationParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: OrganizationInput) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateOrganizationParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: UpdateOrganization,
}

impl UpdateOrganizationParams {
    /// Construct a new `UpdateOrganizationParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: UpdateOrganization) -> Self {
        Self { body }
    }
}

impl<'a> OrganizationsApi<'a> {
    /// List Organizations
    ///
    /// Get a list of all of your existing organizations matching the criteria specified.
    pub async fn list_organizations(
        &self,
        params: ListOrganizationsParams,
    ) -> Result<OrganizationList, Error> {
        self.list_organizations_with_options(params, None).await
    }

    /// Variant of [`Self::list_organizations`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_organizations_with_options(
        &self,
        params: ListOrganizationsParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<OrganizationList, Error> {
        let path = "/organizations".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 `Organization`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<Organization> = self
    ///     .list_organizations_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_organizations_auto_paging(
        &self,
        params: ListOrganizationsParams,
    ) -> impl futures_util::Stream<Item = Result<Organization, Error>> + '_ {
        crate::pagination::auto_paginate_pages(move |after| {
            let mut params = params.clone();
            params.after = after;
            async move {
                let page = self.list_organizations(params).await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }

    /// Create an Organization
    ///
    /// Creates a new organization in the current environment.
    pub async fn create_organization(
        &self,
        params: CreateOrganizationParams,
    ) -> Result<Organization, Error> {
        self.create_organization_with_options(params, None).await
    }

    /// Variant of [`Self::create_organization`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_organization_with_options(
        &self,
        params: CreateOrganizationParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<Organization, Error> {
        let path = "/organizations".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get an Organization by External ID
    ///
    /// Get the details of an existing organization by an [external identifier](https://workos.com/docs/authkit/metadata/external-identifiers).
    pub async fn get_organization_by_external_id(
        &self,
        external_id: &str,
    ) -> Result<Organization, Error> {
        self.get_organization_by_external_id_with_options(external_id, None)
            .await
    }

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

    /// Get an Organization
    ///
    /// Get the details of an existing organization.
    pub async fn get_organization(&self, id: &str) -> Result<Organization, Error> {
        self.get_organization_with_options(id, None).await
    }

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

    /// Update an Organization
    ///
    /// Updates an organization in the current environment.
    pub async fn update_organization(
        &self,
        id: &str,
        params: UpdateOrganizationParams,
    ) -> Result<Organization, Error> {
        self.update_organization_with_options(id, params, None)
            .await
    }

    /// Variant of [`Self::update_organization`] that accepts per-request [`crate::RequestOptions`].
    pub async fn update_organization_with_options(
        &self,
        id: &str,
        params: UpdateOrganizationParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<Organization, Error> {
        let id = crate::client::path_segment(id);
        let path = format!("/organizations/{id}");
        let method = http::Method::PUT;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Delete an Organization
    ///
    /// Permanently deletes an organization in the current environment. It cannot be undone.
    pub async fn delete_organization(&self, id: &str) -> Result<(), Error> {
        self.delete_organization_with_options(id, None).await
    }

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

    /// Get Audit Log Configuration
    ///
    /// Get the unified view of audit log trail and stream configuration for an organization.
    pub async fn get_audit_log_configuration(
        &self,
        id: &str,
    ) -> Result<AuditLogConfiguration, Error> {
        self.get_audit_log_configuration_with_options(id, None)
            .await
    }

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