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

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

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

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

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

#[derive(Debug, Clone, Default, Serialize)]
pub struct GetUserConnectedAccountParams {
    /// An [Organization](https://workos.com/docs/reference/organization) identifier. Optional parameter if the connection is scoped to an organization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub organization_id: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct DeleteUserConnectedAccountParams {
    /// An [Organization](https://workos.com/docs/reference/organization) identifier. Optional parameter if the connection is scoped to an organization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub organization_id: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListUserDataProvidersParams {
    /// An [Organization](https://workos.com/docs/reference/organization) identifier. Optional parameter to filter connections for a specific organization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub organization_id: Option<String>,
}

impl<'a> PipesApi<'a> {
    /// Get authorization URL
    ///
    /// Generates an OAuth authorization URL to initiate the connection flow for a user. Redirect the user to the returned URL to begin the OAuth flow with the third-party provider.
    pub async fn authorize_data_integration(
        &self,
        slug: &str,
        params: AuthorizeDataIntegrationParams,
    ) -> Result<DataIntegrationAuthorizeUrlResponse, Error> {
        self.authorize_data_integration_with_options(slug, params, None)
            .await
    }

    /// Variant of [`Self::authorize_data_integration`] that accepts per-request [`crate::RequestOptions`].
    pub async fn authorize_data_integration_with_options(
        &self,
        slug: &str,
        params: AuthorizeDataIntegrationParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<DataIntegrationAuthorizeUrlResponse, Error> {
        let slug = crate::client::path_segment(slug);
        let path = format!("/data-integrations/{slug}/authorize");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get an access token for a connected account
    ///
    /// Fetches a valid OAuth access token for a user's connected account. WorkOS automatically handles token refresh, ensuring you always receive a valid, non-expired token.
    pub async fn create_data_integration_token(
        &self,
        slug: &str,
        params: CreateDataIntegrationTokenParams,
    ) -> Result<DataIntegrationAccessTokenResponse, Error> {
        self.create_data_integration_token_with_options(slug, params, None)
            .await
    }

    /// Variant of [`Self::create_data_integration_token`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_data_integration_token_with_options(
        &self,
        slug: &str,
        params: CreateDataIntegrationTokenParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<DataIntegrationAccessTokenResponse, Error> {
        let slug = crate::client::path_segment(slug);
        let path = format!("/data-integrations/{slug}/token");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get a connected account
    ///
    /// Retrieves a user's [connected account](https://workos.com/docs/reference/pipes/connected-account) for a specific provider.
    pub async fn get_user_connected_account(
        &self,
        user_id: &str,
        slug: &str,
        params: GetUserConnectedAccountParams,
    ) -> Result<ConnectedAccount, Error> {
        self.get_user_connected_account_with_options(user_id, slug, params, None)
            .await
    }

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

    /// Delete a connected account
    ///
    /// Disconnects WorkOS's account for the user, including removing any stored access and refresh tokens. The user will need to reauthorize if they want to reconnect. This does not revoke access on the provider side.
    pub async fn delete_user_connected_account(
        &self,
        user_id: &str,
        slug: &str,
        params: DeleteUserConnectedAccountParams,
    ) -> Result<(), Error> {
        self.delete_user_connected_account_with_options(user_id, slug, params, None)
            .await
    }

    /// Variant of [`Self::delete_user_connected_account`] that accepts per-request [`crate::RequestOptions`].
    pub async fn delete_user_connected_account_with_options(
        &self,
        user_id: &str,
        slug: &str,
        params: DeleteUserConnectedAccountParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<(), Error> {
        let user_id = crate::client::path_segment(user_id);
        let slug = crate::client::path_segment(slug);
        let path = format!("/user_management/users/{user_id}/connected_accounts/{slug}");
        let method = http::Method::DELETE;
        self.client
            .request_with_query_opts_empty(method, &path, &params, options)
            .await
    }

    /// List providers
    ///
    /// Retrieves a list of available providers and the user's connection status for each. Returns all providers configured for your environment, along with the user's [connected account](https://workos.com/docs/reference/pipes/connected-account) information where applicable.
    pub async fn list_user_data_providers(
        &self,
        user_id: &str,
        params: ListUserDataProvidersParams,
    ) -> Result<DataIntegrationsListResponse, Error> {
        self.list_user_data_providers_with_options(user_id, params, None)
            .await
    }

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