use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::action_api;
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::integration_api as api;
use crate::gen::manager::models;
use crate::http::{collect_all, fetch_page, Page};
use crate::retry::{with_retry, RetryPolicy};
use crate::token::SharedCfg;
pub struct IntegrationsResource {
pub(crate) cfg: SharedCfg<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl IntegrationsResource {
pub async fn list_all(&self) -> Result<Vec<models::Integration>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
collect_all(&self.retry, map_manager_err, |page| async move {
let r = api::list_integrations(cfg, Some(page), None).await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn list_page(
&self,
page: i32,
per_page: Option<i32>,
) -> Result<Page<models::Integration>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
fetch_page(&self.retry, map_manager_err, || async move {
let r = api::list_integrations(cfg, Some(page), per_page).await?;
Ok((
r.items,
r.pagination.pages,
r.pagination.current,
Some(r.pagination.total),
))
})
.await
}
pub async fn create(
&self,
body: models::IntegrationCreateRequest,
) -> Result<models::IntegrationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::create_integration(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn get(&self, id: &str) -> Result<models::IntegrationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || api::get_integration(cfg, id))
.await
.map_err(map_manager_err)
}
pub async fn update(
&self,
id: &str,
body: models::IntegrationUpdateRequest,
) -> Result<models::IntegrationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::update_integration(cfg, id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || api::delete_integration(cfg, id))
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn available(
&self,
) -> Result<models::IntegrationListAvailableIntegrationsResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || api::list_available_integrations(cfg))
.await
.map_err(map_manager_err)
}
pub async fn add_association(
&self,
integration_id: &str,
association_id: &str,
action_name: &str,
) -> Result<models::IntegrationAddAssociationResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::add_integration_association(cfg, integration_id, association_id, action_name)
})
.await
.map_err(map_manager_err)
}
pub async fn remove_association(
&self,
integration_id: &str,
association_id: &str,
action_name: &str,
) -> Result<models::IntegrationRemoveAssociationResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::delete_integration_association(cfg, integration_id, association_id, action_name)
})
.await
.map_err(map_manager_err)
}
pub async fn provider_logo(
&self,
provider_name: models::IntegrationProvider,
size: &str,
) -> Result<models::GetIntegrationProviderLogoResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::get_integration_provider_logo(cfg, provider_name, size)
})
.await
.map_err(map_manager_err)
}
pub async fn provider_session_variables(
&self,
provider: &str,
) -> Result<models::IntegrationSessionVariableItemsResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::list_provider_session_variables(cfg, provider)
})
.await
.map_err(map_manager_err)
}
pub async fn dispatch_action(
&self,
integration_id: &str,
action: &str,
body: models::IntegrationDispatchActionRequest,
) -> Result<models::IntegrationDispatchActionResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::dispatch_action(cfg, integration_id, action, None, None, Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn action_variables(
&self,
provider: models::IntegrationProvider,
action_name: &str,
) -> Result<models::VariableDefinitionItemsResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::list_single_action_session_variables(cfg, provider, action_name)
})
.await
.map_err(map_manager_err)
}
pub async fn providers(&self) -> Result<models::IntegrationObjectListResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || api::list_integration_providers(cfg))
.await
.map_err(map_manager_err)
}
pub async fn provider_template(
&self,
provider: &str,
) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::get_integration_provider_template(cfg, provider)
})
.await
.map_err(map_manager_err)
}
pub async fn template(
&self,
r#type: &str,
provider: &str,
) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::get_integration_template(cfg, r#type, provider)
})
.await
.map_err(map_manager_err)
}
pub async fn authorize(
&self,
id: &str,
body: std::collections::HashMap<String, serde_json::Value>,
) -> Result<models::IntegrationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::authorize_integration(cfg, id, Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn clone(&self, id: &str) -> Result<models::IntegrationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || api::clone_integration(cfg, id))
.await
.map_err(map_manager_err)
}
pub async fn integrate(
&self,
id: &str,
body: std::collections::HashMap<String, serde_json::Value>,
) -> Result<models::IntegrationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::integrate_integration(cfg, id, Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn bulk_update(
&self,
body: models::BulkUpdateIntegrationsRequest,
) -> Result<models::DefaultV2MessageResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::bulk_update_integrations(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn bulk_delete(
&self,
ids: Vec<String>,
) -> Result<models::DefaultV2MessageResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
for id in &ids {
uuid::Uuid::parse_str(id)
.map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))?;
}
let body = models::IntegrationBulkIdsRequest { ids };
with_retry(&self.retry, false, || {
api::bulk_delete_integrations(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn type_actions(
&self,
r#type: &str,
) -> Result<models::IntegrationObjectListResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::list_integration_type_actions(cfg, r#type)
})
.await
.map_err(map_manager_err)
}
pub async fn dispatch_type_action(
&self,
r#type: &str,
id: &str,
action: &str,
body: std::collections::HashMap<String, serde_json::Value>,
) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::dispatch_integration_type_action(cfg, r#type, id, action, Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn api_proxy_get(&self, integration_id: &str, uri: &str) -> Result<(), ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::integration_api_proxy_get(cfg, integration_id, uri)
})
.await
.map_err(map_manager_err)
}
pub async fn api_proxy_post(
&self,
integration_id: &str,
uri: &str,
body: std::collections::HashMap<String, serde_json::Value>,
) -> Result<(), ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::integration_api_proxy_post(cfg, integration_id, uri, Some(body.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn list_tokens(
&self,
id: &str,
) -> Result<models::IntegrationTokenListResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || api::list_integration_tokens(cfg, id))
.await
.map_err(map_manager_err)
}
pub async fn get_token(
&self,
id: &str,
token_id: &str,
) -> Result<models::IntegrationTokenItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::get_integration_token(cfg, id, token_id)
})
.await
.map_err(map_manager_err)
}
pub async fn delete_token(
&self,
id: &str,
token_id: &str,
) -> Result<models::DefaultV2MessageResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::delete_integration_token(cfg, id, token_id)
})
.await
.map_err(map_manager_err)
}
pub async fn refresh_token(
&self,
id: &str,
token_id: &str,
) -> Result<models::IntegrationTokenItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::refresh_integration_token(cfg, id, token_id)
})
.await
.map_err(map_manager_err)
}
pub async fn list_actions(
&self,
r#type: Option<&str>,
) -> Result<models::ObjectListResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || action_api::list_actions(cfg, r#type))
.await
.map_err(map_manager_err)
}
pub async fn list_action_params(
&self,
provider_name: &str,
provider_action_name: &str,
) -> Result<models::ObjectListResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
action_api::list_action_params(cfg, provider_name, provider_action_name)
})
.await
.map_err(map_manager_err)
}
pub async fn execute_action(
&self,
action_type: &str,
action_name: &str,
body: Option<std::collections::HashMap<String, serde_json::Value>>,
) -> Result<models::DefaultV2MessageResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
action_api::execute_action(cfg, action_type, action_name, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn dispatch_action_get(
&self,
integration_id: &str,
action: &str,
call_id: Option<&str>,
session_id: Option<&str>,
) -> Result<models::IntegrationDispatchActionResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || {
api::dispatch_action_get(cfg, integration_id, action, call_id, session_id)
})
.await
.map_err(map_manager_err)
}
}