use custom_error::custom_error;
use tonic::transport::{Channel, Endpoint};
use super::zitadel::{
admin::v1::admin_service_client::AdminServiceClient,
auth::v1::auth_service_client::AuthServiceClient,
management::v1::management_service_client::ManagementServiceClient,
};
custom_error! {
pub ClientError
InvalidUrl = "the provided url is invalid",
ConnectionError = "could not connect to provided endpoint",
}
async fn get_channel(api_endpoint: &str) -> Result<Channel, ClientError> {
Endpoint::from_shared(api_endpoint.to_string())
.map_err(|_| ClientError::InvalidUrl)?
.connect()
.await
.map_err(|_| ClientError::ConnectionError)
}
#[cfg(feature = "api")]
pub async fn create_admin_client(
api_endpoint: &str,
) -> Result<AdminServiceClient<Channel>, ClientError> {
let channel = get_channel(api_endpoint).await?;
Ok(AdminServiceClient::new(channel))
}
#[cfg(feature = "api")]
pub async fn create_auth_client(
api_endpoint: &str,
) -> Result<AuthServiceClient<Channel>, ClientError> {
let channel = get_channel(api_endpoint).await?;
Ok(AuthServiceClient::new(channel))
}
#[cfg(feature = "api")]
pub async fn create_management_client(
api_endpoint: &str,
) -> Result<ManagementServiceClient<Channel>, ClientError> {
let channel = get_channel(api_endpoint).await?;
Ok(ManagementServiceClient::new(channel))
}
#[cfg(all(feature = "api", feature = "interceptors"))]
pub mod with_access_token {
use tonic::codegen::InterceptedService;
use tonic::transport::Channel;
use super::{get_channel, ClientError};
use crate::api::interceptors::AccessTokenInterceptor;
use crate::api::zitadel::{
admin::v1::admin_service_client::AdminServiceClient,
auth::v1::auth_service_client::AuthServiceClient,
management::v1::management_service_client::ManagementServiceClient,
};
#[cfg(feature = "api")]
pub async fn create_admin_client(
api_endpoint: &str,
access_token: &str,
) -> Result<AdminServiceClient<InterceptedService<Channel, AccessTokenInterceptor>>, ClientError>
{
let channel = get_channel(api_endpoint).await?;
Ok(AdminServiceClient::with_interceptor(
channel,
AccessTokenInterceptor::new(access_token),
))
}
#[cfg(feature = "api")]
pub async fn create_auth_client(
api_endpoint: &str,
access_token: &str,
) -> Result<AuthServiceClient<InterceptedService<Channel, AccessTokenInterceptor>>, ClientError>
{
let channel = get_channel(api_endpoint).await?;
Ok(AuthServiceClient::with_interceptor(
channel,
AccessTokenInterceptor::new(access_token),
))
}
#[cfg(feature = "api")]
pub async fn create_management_client(
api_endpoint: &str,
access_token: &str,
) -> Result<
ManagementServiceClient<InterceptedService<Channel, AccessTokenInterceptor>>,
ClientError,
> {
let channel = get_channel(api_endpoint).await?;
Ok(ManagementServiceClient::with_interceptor(
channel,
AccessTokenInterceptor::new(access_token),
))
}
}
#[cfg(all(feature = "api", feature = "interceptors", feature = "credentials"))]
pub mod with_service_account {
use tonic::codegen::InterceptedService;
use tonic::transport::Channel;
use super::{get_channel, ClientError};
use crate::api::interceptors::ServiceAccountInterceptor;
use crate::api::zitadel::{
admin::v1::admin_service_client::AdminServiceClient,
auth::v1::auth_service_client::AuthServiceClient,
management::v1::management_service_client::ManagementServiceClient,
};
use crate::credentials::{AuthenticationOptions, ServiceAccount};
#[cfg(feature = "api")]
pub async fn create_admin_client(
api_endpoint: &str,
service_account: &ServiceAccount,
auth_options: Option<AuthenticationOptions>,
) -> Result<
AdminServiceClient<InterceptedService<Channel, ServiceAccountInterceptor>>,
ClientError,
> {
let channel = get_channel(api_endpoint).await?;
Ok(AdminServiceClient::with_interceptor(
channel,
ServiceAccountInterceptor::new(api_endpoint, service_account, auth_options),
))
}
#[cfg(feature = "api")]
pub async fn create_auth_client(
api_endpoint: &str,
service_account: &ServiceAccount,
auth_options: Option<AuthenticationOptions>,
) -> Result<
AuthServiceClient<InterceptedService<Channel, ServiceAccountInterceptor>>,
ClientError,
> {
let channel = get_channel(api_endpoint).await?;
Ok(AuthServiceClient::with_interceptor(
channel,
ServiceAccountInterceptor::new(api_endpoint, service_account, auth_options),
))
}
#[cfg(feature = "api")]
pub async fn create_management_client(
api_endpoint: &str,
service_account: &ServiceAccount,
auth_options: Option<AuthenticationOptions>,
) -> Result<
ManagementServiceClient<InterceptedService<Channel, ServiceAccountInterceptor>>,
ClientError,
> {
let channel = get_channel(api_endpoint).await?;
Ok(ManagementServiceClient::with_interceptor(
channel,
ServiceAccountInterceptor::new(api_endpoint, service_account, auth_options),
))
}
}