use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::sync::Arc;
use reqwest::Method;
use crate::client::WyrdClient;
pub use wyrd_spec::auth::SecretBearer;
pub use wyrd_spec::error::WyrdError;
pub use wyrd_spec::ids::{ConnectionName, OperatorConnectionId};
pub use wyrd_spec::operator_connection::{
CreateOperatorConnectionRequest, HttpAuthScheme, HttpConnectionAuth, HttpsOrigin,
OperatorConnectionConfig, OperatorConnectionStatus, OperatorConnectionView, OperatorProvider,
UpdateOperatorConnectionRequest,
};
#[derive(Clone)]
pub struct OperatorConnections {
client: Arc<WyrdClient>,
}
impl Debug for OperatorConnections {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("OperatorConnections")
.finish_non_exhaustive()
}
}
impl OperatorConnections {
pub async fn connect() -> Result<Self, WyrdError> {
Ok(Self {
client: Arc::new(WyrdClient::connect_ambient().await?),
})
}
pub async fn connect_with(client: &WyrdClient) -> Result<Self, WyrdError> {
Ok(Self {
client: Arc::new(client.connected().await?),
})
}
pub async fn create(
&self,
request: &CreateOperatorConnectionRequest,
) -> Result<OperatorConnectionView, WyrdError> {
self.client
.http
.request_json(Method::POST, "/v1/operator-connections", Some(request))
.await
}
pub async fn list(&self) -> Result<Vec<OperatorConnectionView>, WyrdError> {
self.client
.http
.request_json::<(), _>(Method::GET, "/v1/operator-connections", None)
.await
}
pub async fn get(
&self,
connection_id: &OperatorConnectionId,
) -> Result<OperatorConnectionView, WyrdError> {
self.client
.http
.request_json::<(), _>(Method::GET, &Self::path(connection_id), None)
.await
}
pub async fn update(
&self,
connection_id: &OperatorConnectionId,
request: &UpdateOperatorConnectionRequest,
) -> Result<OperatorConnectionView, WyrdError> {
self.client
.http
.request_json(Method::PATCH, &Self::path(connection_id), Some(request))
.await
}
pub async fn disable(
&self,
connection_id: &OperatorConnectionId,
) -> Result<OperatorConnectionView, WyrdError> {
self.client
.http
.request_json::<(), _>(Method::DELETE, &Self::path(connection_id), None)
.await
}
fn path(connection_id: &OperatorConnectionId) -> String {
format!("/v1/operator-connections/{connection_id}")
}
}