use crate::{Error, Result, proto};
use std::{future::Future, sync::Arc, time::Duration};
use tokio_util::sync::CancellationToken;
#[derive(Clone, Debug)]
pub struct Client {
pub(crate) inner: Arc<Inner>,
}
#[derive(Debug)]
pub(crate) struct Inner {
transport: octelium::Client,
pub(crate) closed: CancellationToken,
timeout: Option<Duration>,
}
impl Client {
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
pub async fn connect(domain: impl Into<String>) -> Result<Self> {
Self::builder().domain(domain).build().await
}
pub async fn from_env() -> Result<Self> {
Self::builder().build().await
}
pub fn from_transport(transport: octelium::Client) -> Self {
Self {
inner: Arc::new(Inner {
transport,
closed: CancellationToken::new(),
timeout: Some(Duration::from_secs(30)),
}),
}
}
pub fn domain(&self) -> &str {
self.inner.transport.domain()
}
pub fn api_endpoint(&self) -> &str {
self.inner.transport.api_endpoint()
}
pub fn transport(&self) -> &octelium::Client {
&self.inner.transport
}
pub async fn access_token(&self) -> Result<String> {
self.operation(self.inner.timeout, async {
Ok(self.inner.transport.access_token().await?)
})
.await
}
pub fn close(&self) {
self.inner.closed.cancel();
self.inner.transport.close();
}
pub fn is_closed(&self) -> bool {
self.inner.closed.is_cancelled() || self.inner.transport.is_closed()
}
pub fn workspaces(&self) -> crate::Workspaces {
crate::Workspaces::new(self.clone())
}
pub fn spaces(&self) -> crate::Spaces {
crate::Spaces::new(self.clone())
}
pub fn templates(&self) -> crate::Templates {
crate::Templates::new(self.clone())
}
pub fn snapshots(&self) -> crate::Snapshots {
crate::Snapshots::new(self.clone())
}
pub fn volumes(&self) -> crate::Volumes {
crate::Volumes::new(self.clone())
}
pub fn secrets(&self) -> crate::Secrets {
crate::Secrets::new(self.clone())
}
pub fn user_secrets(&self) -> crate::UserSecrets {
crate::UserSecrets::new(self.clone())
}
pub fn git_providers(&self) -> crate::GitProviders {
crate::GitProviders::new(self.clone())
}
pub fn memberships(&self) -> crate::Memberships {
crate::Memberships::new(self.clone())
}
pub fn regions(&self) -> crate::Regions {
crate::Regions::new(self.clone())
}
pub fn user_config(&self) -> crate::UserConfig {
crate::UserConfig::new(self.clone())
}
pub fn management(&self) -> crate::Management {
crate::Management::new(self.clone())
}
pub fn main_service(
&self,
) -> proto::main_service_client::MainServiceClient<octelium::AuthenticatedChannel> {
proto::main_service_client::MainServiceClient::new(self.inner.transport.channel())
}
pub fn workspace_service(
&self,
) -> proto::workspace_service_client::WorkspaceServiceClient<octelium::AuthenticatedChannel>
{
proto::workspace_service_client::WorkspaceServiceClient::new(self.inner.transport.channel())
}
pub fn management_service(
&self,
) -> proto::management_service_client::ManagementServiceClient<octelium::AuthenticatedChannel>
{
proto::management_service_client::ManagementServiceClient::new(
self.inner.transport.channel(),
)
}
#[cfg(feature = "http")]
pub fn http(&self) -> octelium::HttpClient {
self.inner.transport.http()
}
pub(crate) fn default_timeout(&self) -> Option<Duration> {
self.inner.timeout
}
pub(crate) fn rpc<'a, T: Send + 'a>(
&'a self,
f: impl Future<Output = std::result::Result<tonic::Response<T>, tonic::Status>> + Send + 'a,
) -> std::pin::Pin<Box<dyn Future<Output = Result<T>> + Send + 'a>> {
Box::pin(self.operation(self.inner.timeout, async move { Ok(f.await?.into_inner()) }))
}
pub(crate) async fn operation<T>(
&self,
timeout: Option<Duration>,
f: impl Future<Output = Result<T>>,
) -> Result<T> {
let deadline = match timeout {
Some(t) => Some(
tokio::time::Instant::now()
.checked_add(t)
.ok_or_else(|| Error::InvalidArgument("timeout is too large".into()))?,
),
None => None,
};
self.operation_until(deadline, f).await
}
pub(crate) async fn operation_until<T>(
&self,
deadline: Option<tokio::time::Instant>,
f: impl Future<Output = Result<T>>,
) -> Result<T> {
if self.is_closed() {
return Err(Error::Closed);
}
let result = tokio::select! {
biased;
_ = self.inner.closed.cancelled() => Err(Error::Closed),
result = async {
match deadline {
Some(d) => tokio::time::timeout_at(d,f).await.map_err(|_| Error::DeadlineExceeded)?,
None => f.await,
}
} => result,
};
if self.is_closed() {
Err(Error::Closed)
} else {
result
}
}
}
#[derive(Debug)]
#[must_use = "call build to create the client"]
pub struct ClientBuilder {
inner: octelium::ClientBuilder,
domain: Option<String>,
timeout: Option<Duration>,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self {
inner: octelium::ClientBuilder::new()
.user_agent(concat!("cordium-rust/", env!("CARGO_PKG_VERSION"))),
domain: None,
timeout: Some(Duration::from_secs(30)),
}
}
}
impl ClientBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn domain(mut self, v: impl Into<String>) -> Self {
self.domain = Some(v.into());
self
}
pub fn timeout(mut self, v: Option<Duration>) -> Self {
self.timeout = v;
self
}
pub fn configure_transport(
mut self,
f: impl FnOnce(octelium::ClientBuilder) -> octelium::ClientBuilder,
) -> Self {
self.inner = f(self.inner);
self
}
pub fn api_endpoint(mut self, v: impl Into<String>) -> Self {
self.inner = self.inner.api_endpoint(v);
self
}
pub fn access_token(mut self, v: impl Into<String>) -> Self {
self.inner = self.inner.access_token(v);
self
}
pub fn access_token_provider(mut self, v: impl octelium::AccessTokenProvider) -> Self {
self.inner = self.inner.access_token_provider(v);
self
}
pub fn authenticator(mut self, v: impl octelium::Authenticator) -> Self {
self.inner = self.inner.authenticator(v);
self
}
pub fn without_environment_credentials(mut self) -> Self {
self.inner = self.inner.without_environment_credentials();
self
}
pub fn scopes<I, S>(mut self, v: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.inner = self.inner.scopes(v);
self
}
pub fn authenticate_on_build(mut self, v: bool) -> Self {
self.inner = self.inner.authenticate_on_build(v);
self
}
pub async fn build(mut self) -> Result<Client> {
if self.timeout.is_some_and(|t| t.is_zero()) {
return Err(Error::InvalidArgument("timeout must be positive".into()));
}
if let Some(domain) = self.domain.or_else(|| {
std::env::var("CORDIUM_DOMAIN")
.ok()
.filter(|s| !s.trim().is_empty())
}) {
self.inner = self.inner.domain(domain);
}
let transport = self.inner.build().await?;
Ok(Client {
inner: Arc::new(Inner {
transport,
closed: CancellationToken::new(),
timeout: self.timeout,
}),
})
}
}