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

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListOrganizationApiKeysParams {
    /// 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, Serialize)]
pub struct CreateOrganizationApiKeyParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: CreateOrganizationApiKey,
}

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

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

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

impl<'a> ApiKeysApi<'a> {
    /// List API keys for an organization
    ///
    /// Get a list of all API keys for an organization.
    pub async fn list_organization_api_keys(
        &self,
        organization_id: &str,
        params: ListOrganizationApiKeysParams,
    ) -> Result<OrganizationApiKeyList, Error> {
        self.list_organization_api_keys_with_options(organization_id, params, None)
            .await
    }

    /// Variant of [`Self::list_organization_api_keys`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_organization_api_keys_with_options(
        &self,
        organization_id: &str,
        params: ListOrganizationApiKeysParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<OrganizationApiKeyList, Error> {
        let organization_id = crate::client::path_segment(organization_id);
        let path = format!("/organizations/{organization_id}/api_keys");
        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 `OrganizationApiKey`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<OrganizationApiKey> = self
    ///     .list_organization_api_keys_auto_paging(organization_id, params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_organization_api_keys_auto_paging(
        &self,
        organization_id: impl Into<String>,
        params: ListOrganizationApiKeysParams,
    ) -> impl futures_util::Stream<Item = Result<OrganizationApiKey, 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_api_keys(&organization_id, params)
                    .await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }

    /// Create an API key for an organization
    ///
    /// Create a new API key for an organization.
    pub async fn create_organization_api_key(
        &self,
        organization_id: &str,
        params: CreateOrganizationApiKeyParams,
    ) -> Result<OrganizationApiKeyWithValue, Error> {
        self.create_organization_api_key_with_options(organization_id, params, None)
            .await
    }

    /// Variant of [`Self::create_organization_api_key`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_organization_api_key_with_options(
        &self,
        organization_id: &str,
        params: CreateOrganizationApiKeyParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<OrganizationApiKeyWithValue, Error> {
        let organization_id = crate::client::path_segment(organization_id);
        let path = format!("/organizations/{organization_id}/api_keys");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Validate API key
    ///
    /// Validate an API key value and return the API key object if valid.
    pub async fn create_validation(
        &self,
        params: CreateValidationParams,
    ) -> Result<ApiKeyValidationResponse, Error> {
        self.create_validation_with_options(params, None).await
    }

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

    /// Delete an API key
    ///
    /// Permanently deletes an API key. This action cannot be undone. Once deleted, any requests using this API key will fail authentication.
    pub async fn delete_api_key(&self, id: &str) -> Result<(), Error> {
        self.delete_api_key_with_options(id, None).await
    }

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