diff --git a/src/auth.rs b/src/auth.rs index 9b9f88a..eb49848 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,7 +1,6 @@ use crate::config::{api_client_configuration_from_token, Config}; use crate::Arguments; use anyhow::Error; -use fiberplane::api_client::logout; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Response, Server, StatusCode}; use qstring::QString; @@ -110,9 +109,9 @@ pub async fn handle_login_command(args: Arguments) -> Result<(), Error> { /// removed from the config file if that was used. pub async fn handle_logout_command(args: Arguments) -> Result<(), Error> { if let Some(token) = args.token { - let api_config = api_client_configuration_from_token(&token, args.base_url)?; + let client = api_client_configuration_from_token(&token, args.base_url)?; - logout(&api_config).await?; + client.logout().await?; info!("You are logged out"); } else { @@ -120,9 +119,9 @@ pub async fn handle_logout_command(args: Arguments) -> Result<(), Error> { match config.api_token { Some(token) => { - let api_config = api_client_configuration_from_token(&token, args.base_url)?; + let client = api_client_configuration_from_token(&token, args.base_url)?; - logout(&api_config).await?; + client.logout().await?; config.api_token = None; config.save().await?; diff --git a/src/config.rs b/src/config.rs index 3f1ccf0..0a3aefc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Error, Result}; use directories::ProjectDirs; -use fiberplane::api_client::clients::{default_config, ApiClient}; +use fiberplane::api_client::clients::default_config; +use fiberplane::api_client::ApiClient; use hyper::http::HeaderValue; use hyper::HeaderMap; use serde::{Deserialize, Serialize}; diff --git a/src/daemons.rs b/src/daemons.rs index 71108f9..4dee616 100644 --- a/src/daemons.rs +++ b/src/daemons.rs @@ -4,7 +4,6 @@ use crate::output::{output_details, output_json, output_list, GenericKeyValue}; use anyhow::{anyhow, Result}; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::{data_source_list, proxy_create, proxy_delete, proxy_get, proxy_list}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::data_sources::{DataSource, DataSourceStatus}; use fiberplane::models::names::Name; @@ -178,7 +177,8 @@ async fn handle_create_command(args: CreateArgs) -> Result<()> { let mut new_proxy = NewProxy::builder().name(name).build(); new_proxy.description = args.description; - let proxy = proxy_create(&client, workspace_id, new_proxy) + let proxy = client + .proxy_create(workspace_id, new_proxy) .await .map_err(|err| anyhow!("Error adding daemon: {err:?}"))?; @@ -206,8 +206,8 @@ struct ProxySummaryWithConnectedDataSources { async fn handle_list_command(args: ListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let proxies = proxy_list(&client, workspace_id).await?; - let data_sources = data_source_list(&client, workspace_id).await?; + let proxies = client.proxy_list(workspace_id).await?; + let data_sources = client.data_source_list(workspace_id).await?; // Put all of the proxies in a map so we can easily look them up by ID and add the data source counts let mut proxies: BTreeMap = proxies @@ -273,7 +273,7 @@ async fn handle_get_command(args: GetArgs) -> Result<()> { ) .await?; - let proxy = proxy_get(&client, workspace_id, &proxy_name).await?; + let proxy = client.proxy_get(workspace_id, &proxy_name).await?; match args.output { ProxyOutput::Table => { @@ -287,7 +287,7 @@ async fn handle_get_command(args: GetArgs) -> Result<()> { async fn handle_data_sources_command(args: DataSourcesArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let data_sources = data_source_list(&client, workspace_id).await?; + let data_sources = client.data_source_list(workspace_id).await?; match args.output { ProxyOutput::Table => { @@ -310,7 +310,7 @@ async fn handle_delete_command(args: DeleteArgs) -> Result<()> { ) .await?; - proxy_delete(&client, workspace_id, &proxy_name).await?; + client.proxy_delete(workspace_id, &proxy_name).await?; info!("Deleted daemon"); Ok(()) diff --git a/src/data_sources.rs b/src/data_sources.rs index 3312ad4..611fb3e 100644 --- a/src/data_sources.rs +++ b/src/data_sources.rs @@ -5,9 +5,6 @@ use crate::workspaces; use anyhow::{anyhow, Result}; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::{ - data_source_create, data_source_delete, data_source_list, data_source_update, -}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::data_sources::{DataSource, NewDataSource, UpdateDataSource}; use fiberplane::models::names::Name; @@ -243,7 +240,7 @@ async fn handle_create(args: CreateArgs) -> Result<()> { .config(provider_config.0) .build(); data_source.description = description; - let data_source = data_source_create(&client, workspace_id, data_source).await?; + let data_source = client.data_source_create(workspace_id, data_source).await?; match args.output { DataSourceOutput::Table => output_details(GenericKeyValue::from_data_source(&data_source)), @@ -260,7 +257,9 @@ async fn handle_delete(args: DeleteArgs) -> Result<()> { let data_source = data_source_picker(&client, Some(workspace_id), args.name).await?; - data_source_delete(&client, workspace_id, &data_source.name).await?; + client + .data_source_delete(workspace_id, &data_source.name) + .await?; Ok(()) } @@ -290,7 +289,9 @@ async fn handle_update(args: UpdateArgs) -> Result<()> { update.description = args.description; update.config = args.provider_config.map(|c| c.0); - let data_source = data_source_update(&client, workspace_id, &data_source.name, update).await?; + let data_source = client + .data_source_update(workspace_id, &data_source.name, update) + .await?; match args.output { DataSourceOutput::Table => output_details(GenericKeyValue::from_data_source(&data_source)), @@ -305,7 +306,7 @@ async fn handle_list(args: ListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let data_sources = data_source_list(&client, workspace_id).await?; + let data_sources = client.data_source_list(workspace_id).await?; match args.output { DataSourceOutput::Table => { diff --git a/src/events.rs b/src/events.rs index 7f83a96..5846e5e 100644 --- a/src/events.rs +++ b/src/events.rs @@ -5,7 +5,6 @@ use crate::KeyValueArgument; use anyhow::Result; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::{event_create, event_delete, event_list}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::events::{Event, NewEvent}; use fiberplane::models::sorting::{EventSortFields, SortDirection}; @@ -154,7 +153,7 @@ async fn handle_event_create_command(args: CreateArguments) -> Result<()> { .labels(labels.unwrap_or_default()) .build(); new_event.time = args.time; - let event = event_create(&client, workspace_id, new_event).await?; + let event = client.event_create(workspace_id, new_event).await?; info!("Successfully created new event"); @@ -169,19 +168,19 @@ async fn handle_event_search_command(args: SearchArguments) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let events = event_list( - &client, - workspace_id, - args.start, - args.end, - args.labels - .map(|args| args.into_iter().map(|kv| (kv.key, kv.value)).collect()), - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - args.page, - args.limit, - ) - .await?; + let events = client + .event_list( + workspace_id, + args.start, + args.end, + args.labels + .map(|args| args.into_iter().map(|kv| (kv.key, kv.value)).collect()), + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + args.page, + args.limit, + ) + .await?; match args.output { EventOutput::Table => { @@ -210,7 +209,7 @@ pub struct DeleteArguments { async fn handle_event_delete_command(args: DeleteArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - event_delete(&client, args.id).await?; + client.event_delete(args.id).await?; info!("Successfully deleted event"); Ok(()) diff --git a/src/experiments.rs b/src/experiments.rs index e35706a..d27aaa7 100644 --- a/src/experiments.rs +++ b/src/experiments.rs @@ -6,7 +6,6 @@ use crate::templates::NOTEBOOK_ID_REGEX; use anyhow::{anyhow, Context, Result}; use clap::{Parser, ValueEnum}; use directories::ProjectDirs; -use fiberplane::api_client::{notebook_cells_append, notebook_get, profile_get}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::markdown::notebook_to_markdown; use fiberplane::models::formatting::{Annotation, AnnotationWithOffset, Mention}; @@ -149,6 +148,7 @@ pub async fn handle_command(args: Arguments) -> Result<()> { async fn handle_message_command(args: MessageArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; + let notebook_id = interactive::notebook_picker(&client, args.notebook_id, None).await?; let mut cache = Cache::load().await?; @@ -156,7 +156,8 @@ async fn handle_message_command(args: MessageArgs) -> Result<()> { let (user_id, name) = match (cache.user_id, cache.user_name) { (Some(user_id), Some(user_name)) => (Base64Uuid::from_str(&user_id)?, user_name), _ => { - let user = profile_get(&client) + let user = client + .profile_get() .await .with_context(|| "Error getting user profile")?; cache.user_name = Some(user.name.clone()); @@ -181,7 +182,8 @@ async fn handle_message_command(args: MessageArgs) -> Result<()> { )]) .build(), ); - let cell = notebook_cells_append(&client, notebook_id, None, None, vec![cell]) + let cell = client + .notebook_cells_append(notebook_id, None, None, vec![cell]) .await .with_context(|| "Error appending cell to notebook")? .pop() @@ -239,7 +241,7 @@ async fn handle_crawl_command(args: CrawlArgs) -> Result<()> { continue; } crawl_index += 1; - let notebook = match notebook_get(&client, notebook_id).await { + let notebook = match client.notebook_get(notebook_id).await { Ok(notebook) => notebook, Err(err) => { // TODO differentiate between 404 and other errors @@ -429,22 +431,22 @@ async fn handle_prometheus_redirect_command(args: PrometheusGraphToNotebookArgs) Some(query) => { // Append cell to notebook and return the URL let id = Base64Uuid::new().to_string(); - if let Err(err) = notebook_cells_append( - &client, - notebook_id, - None, - None, - vec![Cell::Provider( - ProviderCell::builder() - .id(id.clone()) - .intent("prometheus,timeseries") - .query_data(format!( - "application/x-www-form-urlencoded,query={query}" - )) - .build(), - )], - ) - .await + if let Err(err) = client + .notebook_cells_append( + notebook_id, + None, + None, + vec![Cell::Provider( + ProviderCell::builder() + .id(id.clone()) + .intent("prometheus,timeseries") + .query_data(format!( + "application/x-www-form-urlencoded,query={query}" + )) + .build(), + )], + ) + .await { error!("Error appending cell to notebook: {:?}", err); return Ok::<_, Error>( diff --git a/src/front_matter.rs b/src/front_matter.rs index 4bcb0cc..1bfafcf 100644 --- a/src/front_matter.rs +++ b/src/front_matter.rs @@ -1,8 +1,7 @@ +use crate::config::api_client_configuration; +use crate::interactive::{front_matter_collection_picker, workspace_picker}; use anyhow::{Context, Result}; use clap::{Parser, ValueHint}; -use fiberplane::api_client::{ - workspace_front_matter_schema_create, workspace_front_matter_schema_get_by_name, -}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::front_matter_schemas::FrontMatterSchema; use fiberplane::models::names::Name; @@ -10,11 +9,6 @@ use fiberplane::models::workspaces::NewWorkspaceFrontMatterSchema; use std::{io::stdin, path::PathBuf}; use url::Url; -use crate::{ - config::api_client_configuration, - interactive::{front_matter_collection_picker, workspace_picker}, -}; - #[derive(Parser)] pub struct Arguments { #[clap(subcommand)] @@ -155,15 +149,15 @@ pub async fn handle_set_command(args: SetArgs) -> Result<()> { serde_json::from_str(&content).with_context(|| "cannot parse content as schema")? }; - workspace_front_matter_schema_create( - &client, - workspace_id, - NewWorkspaceFrontMatterSchema::builder() - .name(fmc_name.to_string()) - .schema(front_matter_schema) - .build(), - ) - .await?; + client + .workspace_front_matter_schema_create( + workspace_id, + NewWorkspaceFrontMatterSchema::builder() + .name(fmc_name.to_string()) + .schema(front_matter_schema) + .build(), + ) + .await?; Ok(()) } @@ -185,15 +179,15 @@ pub async fn handle_create_command(args: CreateArgs) -> Result<()> { serde_json::from_str(&content).with_context(|| "cannot parse content as schema")? }; - workspace_front_matter_schema_create( - &client, - workspace_id, - NewWorkspaceFrontMatterSchema::builder() - .name(args.name.to_string()) - .schema(front_matter_schema) - .build(), - ) - .await?; + client + .workspace_front_matter_schema_create( + workspace_id, + NewWorkspaceFrontMatterSchema::builder() + .name(args.name.to_string()) + .schema(front_matter_schema) + .build(), + ) + .await?; Ok(()) } @@ -204,7 +198,9 @@ pub async fn handle_get_command(args: GetArgs) -> Result<()> { let (workspace_id, fmc_name) = front_matter_collection_picker(&client, args.workspace_id, args.name).await?; - let fmc = workspace_front_matter_schema_get_by_name(&client, workspace_id, &fmc_name).await?; + let fmc = client + .workspace_front_matter_schema_get_by_name(workspace_id, &fmc_name) + .await?; println!( "{}", diff --git a/src/integrations.rs b/src/integrations.rs index 589e014..b56b9b8 100644 --- a/src/integrations.rs +++ b/src/integrations.rs @@ -3,12 +3,13 @@ use crate::output::{output_json, output_list}; use anyhow::Result; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::integrations_get; use fiberplane::models::integrations::IntegrationSummary; use std::path::PathBuf; use time::format_description::well_known::Rfc3339; use url::Url; +mod pagerduty_receivers; + #[derive(Parser)] pub struct Arguments { #[clap(subcommand)] @@ -19,11 +20,17 @@ pub struct Arguments { enum SubCommand { /// List all integrations List(ListArgs), + + /// All commands related to the creation and management of PagerDuty receivers. + #[clap(name = "pagerduty-receivers", alias = "pagerduty-receiver")] + PagerDutyReceivers(pagerduty_receivers::Arguments), } pub async fn handle_command(args: Arguments) -> Result<()> { match args.sub_command { SubCommand::List(args) => handle_integrations_list(args).await, + + SubCommand::PagerDutyReceivers(args) => pagerduty_receivers::handle_command(args).await, } } @@ -54,7 +61,7 @@ enum IntegrationOutput { async fn handle_integrations_list(args: ListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - let integrations = integrations_get(&client).await?; + let integrations = client.integrations_get().await?; match args.output { IntegrationOutput::Table => { diff --git a/src/integrations/pagerduty_receivers.rs b/src/integrations/pagerduty_receivers.rs new file mode 100644 index 0000000..d2facf3 --- /dev/null +++ b/src/integrations/pagerduty_receivers.rs @@ -0,0 +1,393 @@ +use crate::config::api_client_configuration; +use crate::interactive::{name_req, workspace_picker}; +use crate::output::{output_details, output_json, output_list, GenericKeyValue}; +use crate::utils::clear_or_update; +use anyhow::Result; +use clap::{Parser, ValueEnum}; +use cli_table::Table; +use fiberplane::base64uuid::Base64Uuid; +use fiberplane::models::names::Name; +use fiberplane::models::pagerduty::{ + NewPagerDutyReceiver, PagerDutyReceiver, PagerDutyReceiverListSortFields, + UpdatePagerDutyReceiver, +}; +use fiberplane::models::sorting::SortDirection; +use petname::petname; +use std::path::PathBuf; +use time::format_description::well_known::Rfc3339; +use tracing::info; +use url::Url; + +/// PagerDuty receivers allow for customization for integration with PagerDuty's +/// webhooks. +#[derive(Parser)] +pub struct Arguments { + #[clap(subcommand)] + sub_command: SubCommand, +} + +#[derive(Parser)] +enum SubCommand { + /// Create a new PagerDuty receiver. + Create(CreateArgs), + + /// Retrieve a single PagerDuty receiver. + Get(GetArgs), + + /// Update a PagerDuty receiver. + Update(UpdateArgs), + + /// Delete a PagerDuty receiver. + Delete(DeleteArgs), + + /// List all PagerDuty receivers for a single workspace. + List(ListArgs), +} + +pub async fn handle_command(args: Arguments) -> Result<()> { + match args.sub_command { + SubCommand::Create(args) => handle_create(args).await, + SubCommand::Get(args) => handle_get(args).await, + SubCommand::Update(args) => handle_update(args).await, + SubCommand::Delete(args) => handle_delete(args).await, + SubCommand::List(args) => handle_list(args).await, + } +} + +/// A generic output for daemon related commands. +#[derive(ValueEnum, Clone)] +enum PagerDutyWebhookOutput { + /// Output the result as a table + Table, + + /// Output the result as a JSON encoded object + Json, +} + +#[derive(Parser)] +struct CreateArgs { + /// Workspace to use + #[clap(from_global)] + workspace_id: Option, + + /// PagerDuty webhook receiver name. Use this to refer back to this + #[clap(long, short)] + name: Option, + + /// An optional name referencing a Template. This template will be expanded + /// if a PagerDuty incident is created. + #[clap(long, short)] + incident_created_template: Option, + + /// Output of the webhooks + #[clap(long, short, default_value = "table", value_enum)] + output: PagerDutyWebhookOutput, + + #[clap(from_global)] + base_url: Url, + + #[clap(from_global)] + config: Option, + + #[clap(from_global)] + token: Option, +} + +async fn handle_create(args: CreateArgs) -> Result<()> { + let client = api_client_configuration(args.token, args.config, args.base_url).await?; + + let workspace_id = workspace_picker(&client, args.workspace_id).await?; + let default_name = Name::new(petname(2, "-")).expect("petname should be valid name"); + let name = name_req( + "PagerDuty webhook receiver name", + args.name, + Some(default_name), + )?; + + let new_pagerduty_receiver = NewPagerDutyReceiver::builder() + .incident_created_template_name(args.incident_created_template) + .build(); + let pagerduty_receiver = client + .pagerduty_receiver_create(workspace_id, &name, new_pagerduty_receiver) + .await?; + + match args.output { + PagerDutyWebhookOutput::Table => { + output_details(GenericKeyValue::from_pagerduty_receiver(pagerduty_receiver)) + } + PagerDutyWebhookOutput::Json => output_json(&pagerduty_receiver), + } +} + +#[derive(Parser)] +struct GetArgs { + /// Workspace to use + #[clap(from_global)] + workspace_id: Option, + + /// PagerDuty webhook receiver name. + #[clap(long, short)] + name: Option, + + /// Output of the webhooks + #[clap(long, short, default_value = "table", value_enum)] + output: PagerDutyWebhookOutput, + + #[clap(from_global)] + base_url: Url, + + #[clap(from_global)] + config: Option, + + #[clap(from_global)] + token: Option, +} + +async fn handle_get(args: GetArgs) -> Result<()> { + let client = api_client_configuration(args.token, args.config, args.base_url).await?; + + let workspace_id = workspace_picker(&client, args.workspace_id).await?; + let name = name_req("PagerDuty webhook receiver name", args.name, None)?; + + let pagerduty_receiver = client.pagerduty_receiver_get(workspace_id, &name).await?; + + match args.output { + PagerDutyWebhookOutput::Table => { + output_details(GenericKeyValue::from_pagerduty_receiver(pagerduty_receiver)) + } + PagerDutyWebhookOutput::Json => output_json(&pagerduty_receiver), + } +} + +#[derive(Parser)] +struct UpdateArgs { + /// Workspace to use + #[clap(from_global)] + workspace_id: Option, + + /// PagerDuty webhook receiver name. + #[clap(long, short)] + name: Option, + + /// An optional name referencing a Template. This template will be expanded + /// if a PagerDuty incident is created. + #[clap(long, env, conflicts_with = "clear_incident_creation_template")] + incident_creation_template: Option, + + /// Clear the incident creation template reference. + #[clap(long, env, conflicts_with = "incident_creation_template")] + clear_incident_creation_template: bool, + + #[clap(long, short)] + regenerate_security_key: bool, + + /// Output of the webhooks + #[clap(long, short, default_value = "table", value_enum)] + output: PagerDutyWebhookOutput, + + #[clap(from_global)] + base_url: Url, + + #[clap(from_global)] + config: Option, + + #[clap(from_global)] + token: Option, +} + +async fn handle_update(args: UpdateArgs) -> Result<()> { + let client = api_client_configuration(args.token, args.config, args.base_url).await?; + + let workspace_id = workspace_picker(&client, args.workspace_id).await?; + let default_name = Name::new(petname(2, "-")).expect("petname should be valid name"); + let name = name_req( + "PagerDuty webhook receiver name", + args.name, + Some(default_name), + )?; + + let incident_created_template_name = clear_or_update( + args.clear_incident_creation_template, + args.incident_creation_template, + ); + + let update_pagerduty_receiver = UpdatePagerDutyReceiver::builder() + .incident_created_template_name(incident_created_template_name) + .regenerate_security_key(args.regenerate_security_key) + .build(); + + let pagerduty_receiver = client + .pagerduty_receiver_update(workspace_id, &name, update_pagerduty_receiver) + .await?; + + match args.output { + PagerDutyWebhookOutput::Table => { + output_details(GenericKeyValue::from_pagerduty_receiver(pagerduty_receiver)) + } + PagerDutyWebhookOutput::Json => output_json(&pagerduty_receiver), + } +} + +#[derive(Parser)] +struct DeleteArgs { + /// Workspace to use + #[clap(from_global)] + workspace_id: Option, + + /// PagerDuty webhook receiver name. + #[clap(long, short)] + name: Option, + + #[clap(from_global)] + base_url: Url, + + #[clap(from_global)] + config: Option, + + #[clap(from_global)] + token: Option, +} + +async fn handle_delete(args: DeleteArgs) -> Result<()> { + let client = api_client_configuration(args.token, args.config, args.base_url).await?; + + let workspace_id = workspace_picker(&client, args.workspace_id).await?; + let name = name_req("PagerDuty webhook receiver name", args.name, None)?; + + client + .pagerduty_receiver_delete(workspace_id, &name) + .await?; + + info!("Deleted Pagerduty receiver"); + Ok(()) +} + +#[derive(Parser)] +struct ListArgs { + /// Workspace to use + #[clap(from_global)] + workspace_id: Option, + + /// Sort the result according to the following field + #[clap(long, value_enum)] + sort_by: Option, + + /// Sort the result in the following direction + #[clap(long, value_enum)] + sort_direction: Option, + + /// Page to display + #[clap(long)] + page: Option, + + /// Amount of integrations to display per page + #[clap(long)] + limit: Option, + + /// Output of the webhooks + #[clap(long, short, default_value = "table", value_enum)] + output: PagerDutyWebhookOutput, + + #[clap(from_global)] + base_url: Url, + + #[clap(from_global)] + config: Option, + + #[clap(from_global)] + token: Option, +} + +async fn handle_list(args: ListArgs) -> Result<()> { + let client = api_client_configuration(args.token, args.config, args.base_url).await?; + + let workspace_id = workspace_picker(&client, args.workspace_id).await?; + + let pagerduty_receivers = client + .pagerduty_receiver_list( + workspace_id, + args.page, + args.limit, + args.sort_by.map(Into::into), + args.sort_direction.map(Into::into), + ) + .await?; + + if pagerduty_receivers.has_more_results { + info!(total_results = pagerduty_receivers.total_results, "There are more results available. Please use the --page and --limit flags to paginate through the results.") + } + + match args.output { + PagerDutyWebhookOutput::Table => { + let pagerduty_receivers: Vec = + pagerduty_receivers.into_iter().map(Into::into).collect(); + output_list(pagerduty_receivers) + } + PagerDutyWebhookOutput::Json => output_json(&pagerduty_receivers), + } +} + +#[derive(Table)] +pub struct PagerDutyReceiverRow { + #[table(title = "Name")] + pub name: String, + + #[table(title = "Incident Created Template")] + pub incident_created_template: String, + + #[table(title = "Updated at")] + pub updated_at: String, + + #[table(title = "Created at")] + pub created_at: String, +} + +impl From for PagerDutyReceiverRow { + fn from(pagerduty_receiver: PagerDutyReceiver) -> Self { + Self { + name: pagerduty_receiver.name.to_string(), + incident_created_template: pagerduty_receiver + .incident_created_template_name + .map(|name| name.to_string()) + .unwrap_or_else(|| String::from("")), + updated_at: pagerduty_receiver + .updated_at + .format(&Rfc3339) + .unwrap_or_default(), + created_at: pagerduty_receiver + .created_at + .format(&Rfc3339) + .unwrap_or_default(), + } + } +} + +impl GenericKeyValue { + pub fn from_pagerduty_receiver(pagerduty_receiver: PagerDutyReceiver) -> Vec { + vec![ + GenericKeyValue::new("Name:", pagerduty_receiver.name), + GenericKeyValue::new( + "Incident created template:", + pagerduty_receiver + .incident_created_template_name + .map(|name| name.to_string()) + .unwrap_or_else(|| String::from("")), + ), + GenericKeyValue::new("Security key:", pagerduty_receiver.security_key), + GenericKeyValue::new( + "Created at:", + pagerduty_receiver + .created_at + .format(&Rfc3339) + .unwrap_or_default(), + ), + GenericKeyValue::new( + "Updated at:", + pagerduty_receiver + .updated_at + .format(&Rfc3339) + .unwrap_or_default(), + ), + ] + } +} diff --git a/src/interactive.rs b/src/interactive.rs index 02b1e7d..08f4e5f 100644 --- a/src/interactive.rs +++ b/src/interactive.rs @@ -1,16 +1,12 @@ use anyhow::{anyhow, bail, Context, Result}; use dialoguer::{theme, Confirm, FuzzySelect, Input, MultiSelect, Select}; -use fiberplane::api_client::clients::ApiClient; -use fiberplane::api_client::{ - data_source_get, data_source_list, notebook_search, proxy_list, snippet_list, template_list, - trigger_list, view_list, webhook_delivery_list, webhook_list, - workspace_front_matter_schema_get, workspace_list, workspace_user_list, -}; +use fiberplane::api_client::ApiClient; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::data_sources::DataSource; use fiberplane::models::names::Name; use fiberplane::models::notebooks::NotebookSearch; -use fiberplane::models::sorting::{NotebookSortFields, Pagination, SortDirection}; +use fiberplane::models::paging::Pagination; +use fiberplane::models::sorting::{NotebookSortFields, SortDirection}; use fiberplane::models::webhooks::{InvalidWebhookCategoryError, WebhookCategory}; use indicatif::ProgressBar; use std::convert::TryInto; @@ -240,14 +236,14 @@ pub async fn notebook_picker_with_prompt( pb.set_message("Fetching recent notebooks"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = notebook_search( - client, - workspace_id, - Some(NotebookSortFields::CreatedAt.into()), - Some(SortDirection::Descending.into()), // show notebooks which have been created most recently first - NotebookSearch::default(), - ) - .await?; + let results = client + .notebook_search( + workspace_id, + Some(NotebookSortFields::CreatedAt.into()), + Some(SortDirection::Descending.into()), // show notebooks which have been created most recently first + NotebookSearch::default(), + ) + .await?; pb.finish_and_clear(); @@ -303,8 +299,9 @@ pub async fn template_picker( pb.set_message("Fetching templates"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = - template_list(client, workspace_id, Some("updated_at"), Some("descending")).await?; + let results = client + .template_list(workspace_id, Some("updated_at"), Some("descending")) + .await?; pb.finish_and_clear(); @@ -347,7 +344,7 @@ pub async fn template_picker( /// interactive input will always be shown. This is a limitation that we /// currently not check if the invocation is interactive or not. pub async fn snippet_picker( - config: &ApiClient, + client: &ApiClient, snippet_name: Option, workspace_id: Option, ) -> Result<(Base64Uuid, Name)> { @@ -359,14 +356,15 @@ pub async fn snippet_picker( // No argument was provided, so we need to know the workspace ID in order to query // the snippet name. let workspace_id = - workspace_picker_with_prompt("Workspace of the snippet", config, workspace_id).await?; + workspace_picker_with_prompt("Workspace of the snippet", client, workspace_id).await?; let pb = ProgressBar::new_spinner(); pb.set_message("Fetching snippets"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = - snippet_list(config, workspace_id, Some("updated_at"), Some("descending")).await?; + let results = client + .snippet_list(workspace_id, Some("updated_at"), Some("descending")) + .await?; pb.finish_and_clear(); @@ -425,7 +423,7 @@ pub async fn trigger_picker( pb.set_message("Fetching triggers"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = trigger_list(client, workspace_id).await?; + let results = client.trigger_list(workspace_id).await?; pb.finish_and_clear(); @@ -478,7 +476,7 @@ pub async fn proxy_picker( pb.set_message("Fetching daemons"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = proxy_list(client, workspace_id).await?; + let results = client.proxy_list(workspace_id).await?; pb.finish_and_clear(); @@ -519,7 +517,7 @@ pub async fn data_source_picker( let workspace_id = workspace_picker(client, workspace_id).await?; if let Some(name) = argument { - let data_source = data_source_get(client, workspace_id, &name).await?; + let data_source = client.data_source_get(workspace_id, &name).await?; return Ok(data_source); } @@ -527,7 +525,7 @@ pub async fn data_source_picker( pb.set_message("Fetching data sources"); pb.enable_steady_tick(Duration::from_millis(100)); - let mut results = data_source_list(client, workspace_id).await?; + let mut results = client.data_source_list(workspace_id).await?; pb.finish_and_clear(); @@ -578,7 +576,9 @@ pub async fn view_picker( pb.set_message("Fetching views"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = view_list(client, workspace_id, None, None, None, None).await?; + let results = client + .view_list(workspace_id, None, None, None, None) + .await?; pb.finish_and_clear(); @@ -640,7 +640,9 @@ pub async fn workspace_picker_with_prompt( pb.set_message("Fetching workspaces"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = workspace_list(client, Some("name"), Some("ascending")).await?; + let results = client + .workspace_list(Some("name"), Some("ascending")) + .await?; pb.finish_and_clear(); @@ -678,7 +680,7 @@ pub async fn workspace_picker_with_prompt( /// currently not check if the invocation is interactive or not. pub async fn workspace_user_picker( client: &ApiClient, - workspace: &Base64Uuid, + workspace_id: &Base64Uuid, argument: Option, ) -> Result { // If the user provided an argument, use that. Otherwise show the picker. @@ -690,7 +692,9 @@ pub async fn workspace_user_picker( pb.set_message("Fetching workspace users"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = workspace_user_list(client, *workspace, Some("name"), Some("ascending")).await?; + let results = client + .workspace_user_list(*workspace_id, Some("name"), Some("ascending")) + .await?; pb.finish_and_clear(); @@ -727,7 +731,7 @@ pub async fn workspace_user_picker( /// currently not check if the invocation is interactive or not. pub async fn webhook_picker( client: &ApiClient, - workspace: Base64Uuid, + workspace_id: Base64Uuid, argument: Option, ) -> Result { // If the user provided an argument, use that. Otherwise, show the picker. @@ -740,13 +744,9 @@ pub async fn webhook_picker( pb.enable_steady_tick(Duration::from_millis(100)); let max = Pagination::max(); - let results = webhook_list( - client, - workspace, - Some(max.page as i32), - Some(max.limit as i32), - ) - .await?; + let results = client + .webhook_list(workspace_id, Some(max.page as i32), Some(max.limit as i32)) + .await?; pb.finish_and_clear(); @@ -783,8 +783,8 @@ pub async fn webhook_picker( /// currently not check if the invocation is interactive or not. pub async fn webhook_delivery_picker( client: &ApiClient, - workspace: Base64Uuid, - webhook: Base64Uuid, + workspace_id: Base64Uuid, + webhook_id: Base64Uuid, argument: Option, ) -> Result { // If the user provided an argument, use that. Otherwise, show the picker. @@ -797,14 +797,14 @@ pub async fn webhook_delivery_picker( pb.enable_steady_tick(Duration::from_millis(100)); let max = Pagination::max(); - let results = webhook_delivery_list( - client, - workspace, - webhook, - Some(max.page as i32), - Some(max.limit as i32), - ) - .await?; + let results = client + .webhook_delivery_list( + workspace_id, + webhook_id, + Some(max.page as i32), + Some(max.limit as i32), + ) + .await?; pb.finish_and_clear(); @@ -869,7 +869,7 @@ pub fn webhook_category_picker( /// interactive input will always be shown. This is a limitation that we /// currently not check if the invocation is interactive or not. pub async fn front_matter_collection_picker( - config: &ApiClient, + client: &ApiClient, workspace_id: Option, front_matter_collection_name: Option, ) -> Result<(Base64Uuid, Name)> { @@ -882,7 +882,7 @@ pub async fn front_matter_collection_picker( // the snippet name. let workspace_id = workspace_picker_with_prompt( "Workspace of the front matter collection", - config, + client, workspace_id, ) .await?; @@ -891,7 +891,9 @@ pub async fn front_matter_collection_picker( pb.set_message("Fetching front matter collections"); pb.enable_steady_tick(Duration::from_millis(100)); - let results = workspace_front_matter_schema_get(config, workspace_id).await?; + let results = client + .workspace_front_matter_schema_get(workspace_id) + .await?; pb.finish_and_clear(); diff --git a/src/labels.rs b/src/labels.rs index 2b3d148..c4810a8 100644 --- a/src/labels.rs +++ b/src/labels.rs @@ -3,7 +3,6 @@ use crate::interactive::{self, workspace_picker}; use crate::output::{output_json, output_string_list}; use anyhow::Result; use clap::{Parser, ValueEnum}; -use fiberplane::api_client::{label_keys_list, label_values_list}; use fiberplane::base64uuid::Base64Uuid; use std::path::PathBuf; use url::Url; @@ -70,7 +69,9 @@ async fn handle_list_keys_command(args: ListKeysArgs) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; let prefix = interactive::text_opt("Prefix", args.prefix, None); - let keys = label_keys_list(&client, workspace_id, prefix.as_deref()).await?; + let keys = client + .label_keys_list(workspace_id, prefix.as_deref()) + .await?; match args.output { ListKeysOutput::List => output_string_list(keys), @@ -119,7 +120,9 @@ async fn handle_list_values_command(args: ListValuesArgs) -> Result<()> { let label_key = interactive::text_req("Label key", args.label_key, None)?; let prefix = interactive::text_opt("Prefix", args.prefix, None); - let values = label_values_list(&client, workspace_id, &label_key, prefix.as_deref()).await?; + let values = client + .label_values_list(workspace_id, &label_key, prefix.as_deref()) + .await?; match args.output { ListValuesOutput::List => output_string_list(values), diff --git a/src/main.rs b/src/main.rs index ca4a7d3..07ead59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use clap::{CommandFactory, Parser, ValueHint}; use clap_complete::{generate, Shell}; use config::api_client_configuration; use directories::ProjectDirs; -use fiberplane::api_client::notebook_create; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::labels::Label; use fiberplane::models::notebooks::NewNotebook; @@ -581,7 +580,7 @@ async fn handle_new_command(args: NewArguments) -> Result<()> { .title(title) .time_range(NewTimeRange::Relative(RelativeTimeRange::from_minutes(60))) .build(); - let notebook = notebook_create(&client, workspace_id, new_notebook).await?; + let notebook = client.notebook_create(workspace_id, new_notebook).await?; let notebook_id = Base64Uuid::parse_str(¬ebook.id)?; diff --git a/src/notebooks.rs b/src/notebooks.rs index 4b48ee9..e7776e6 100644 --- a/src/notebooks.rs +++ b/src/notebooks.rs @@ -9,12 +9,6 @@ use anyhow::{anyhow, bail, Context, Result}; use clap::{Parser, ValueEnum, ValueHint}; use cli_table::Table; use dialoguer::FuzzySelect; -use fiberplane::api_client::{ - front_matter_add_keys, front_matter_delete, front_matter_delete_key, front_matter_update, - front_matter_update_key, notebook_cells_append, notebook_create, notebook_delete, - notebook_duplicate, notebook_get, notebook_list, notebook_search, notebook_snippet_insert, - workspace_front_matter_schema_get_by_name, -}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::markdown::{markdown_to_notebook, notebook_to_markdown}; use fiberplane::models::front_matter_schemas::{ @@ -219,7 +213,7 @@ async fn handle_create_command(args: CreateArgs) -> Result<()> { .front_matter(notebook.front_matter) .build(); - let notebook = notebook_create(&client, workspace_id, notebook).await?; + let notebook = client.notebook_create(workspace_id, notebook).await?; match args.output { NotebookOutput::Table => { @@ -275,7 +269,7 @@ async fn handle_duplicate_command(args: DuplicateArgs) -> Result<()> { ) .await?; - let source_notebook = notebook_get(&client, notebook_id).await?; + let source_notebook = client.notebook_get(notebook_id).await?; let workspace_id = interactive::workspace_picker_with_prompt("Target workspace", &client, args.workspace_id) @@ -293,15 +287,15 @@ async fn handle_duplicate_command(args: DuplicateArgs) -> Result<()> { let title = interactive::text_req("Title", args.title, Some(new_title))?; - let notebook = notebook_duplicate( - &client, - notebook_id, - NotebookCopyDestination::builder() - .title(title) - .workspace_id(workspace_id) - .build(), - ) - .await?; + let notebook = client + .notebook_duplicate( + notebook_id, + NotebookCopyDestination::builder() + .title(title) + .workspace_id(workspace_id) + .build(), + ) + .await?; match args.output { NotebookOutput::Table => { @@ -343,7 +337,7 @@ async fn handle_get_command(args: GetArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let notebook_id = notebook_picker(&client, args.notebook_id, args.workspace_id).await?; - let notebook = notebook_get(&client, notebook_id).await?; + let notebook = client.notebook_get(notebook_id).await?; match args.output { SingleNotebookOutput::Table => output_details(GenericKeyValue::from_notebook(notebook)?), @@ -382,7 +376,7 @@ async fn handle_list_command(args: ListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let notebooks = notebook_list(&client, workspace_id).await?; + let notebooks = client.notebook_list(workspace_id).await?; match args.output { NotebookOutput::Table => { @@ -454,14 +448,14 @@ async fn handle_search_command(args: SearchArgs) -> Result<()> { search_params.view = Some(view_picker(&client, Some(workspace_id), args.view).await?); } - let notebooks = notebook_search( - &client, - workspace_id, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - search_params, - ) - .await?; + let notebooks = client + .notebook_search( + workspace_id, + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + search_params, + ) + .await?; match args.output { NotebookOutput::Table => { @@ -532,7 +526,8 @@ async fn handle_delete_command(args: DeleteArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let notebook_id = notebook_picker(&client, args.notebook_id, args.workspace_id).await?; - notebook_delete(&client, notebook_id) + client + .notebook_delete(notebook_id) .await .with_context(|| format!("Error deleting notebook {notebook_id}"))?; @@ -590,7 +585,8 @@ async fn handle_append_cell_command(args: AppendCellArgs) -> Result<()> { unreachable!(); }; - let cell = notebook_cells_append(&client, notebook_id, None, None, vec![cell]) + let cell = client + .notebook_cells_append(notebook_id, None, None, vec![cell]) .await? .pop() .ok_or_else(|| anyhow!("Expected a single cell"))?; @@ -653,9 +649,9 @@ pub(crate) async fn handle_insert_snippet_command(args: InsertSnippetArgs) -> Re snippet_picker(&client, args.snippet_name, Some(workspace_id)).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - let cells = - notebook_snippet_insert(&client, notebook_id, &snippet_name, args.cell_id.as_deref()) - .await?; + let cells = client + .notebook_snippet_insert(notebook_id, &snippet_name, args.cell_id.as_deref()) + .await?; let url = NotebookUrlBuilder::new(workspace_id, notebook_id) .base_url(args.base_url) @@ -745,7 +741,9 @@ async fn handle_front_matter_update_command(args: FrontMatterUpdateArguments) -> let workspace_id = workspace_picker(&client, args.workspace_id).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - front_matter_update(&client, notebook_id, args.front_matter).await?; + client + .front_matter_update(notebook_id, args.front_matter) + .await?; info!("Successfully updated front matter"); Ok(()) @@ -777,7 +775,7 @@ async fn handle_front_matter_clear_command(args: FrontMatterClearArguments) -> R let workspace_id = workspace_picker(&client, args.workspace_id).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - front_matter_delete(&client, notebook_id).await?; + client.front_matter_delete(notebook_id).await?; info!("Successfully cleared front matter"); Ok(()) @@ -938,13 +936,13 @@ async fn handle_front_matter_append_command(args: FrontMatterAppendArguments) -> .value(args.value.map(Into::into)) .build(); - let notebook = notebook_get(&client, notebook_id).await?; + let notebook = client.notebook_get(notebook_id).await?; let additions = FrontMatterAddRows::builder() .to_index(notebook.front_matter_schema.len().try_into().unwrap()) .insertions(vec![new_row]) .build(); - front_matter_add_keys(&client, notebook_id, additions).await?; + client.front_matter_add_keys(notebook_id, additions).await?; info!("Successfully updated front matter"); Ok(()) @@ -955,7 +953,7 @@ async fn handle_front_matter_delete_command(args: FrontMatterDeleteArguments) -> let workspace_id = workspace_picker(&client, args.workspace_id).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - let notebook = notebook_get(&client, notebook_id).await?; + let notebook = client.notebook_get(notebook_id).await?; if args.all { for key in notebook @@ -963,7 +961,7 @@ async fn handle_front_matter_delete_command(args: FrontMatterDeleteArguments) -> .iter() .map(|schema| schema.key.clone()) { - front_matter_delete_key(&client, notebook_id, &key).await?; + client.front_matter_delete_key(notebook_id, &key).await?; } info!("Successfully updated front matter"); return Ok(()); @@ -971,7 +969,7 @@ async fn handle_front_matter_delete_command(args: FrontMatterDeleteArguments) -> match args.front_matter_key { Some(key) => { - front_matter_delete_key(&client, notebook_id, &key).await?; + client.front_matter_delete_key(notebook_id, &key).await?; } None => { let keys: Vec<_> = notebook @@ -1004,7 +1002,9 @@ async fn handle_front_matter_delete_command(args: FrontMatterDeleteArguments) -> match selection { Some(selection) => { - front_matter_delete_key(&client, notebook_id, &keys[selection].0).await?; + client + .front_matter_delete_key(notebook_id, &keys[selection].0) + .await?; } None => bail!("No key selected"), } @@ -1020,7 +1020,7 @@ async fn handle_front_matter_edit_command(args: FrontMatterEditArguments) -> Res let workspace_id = workspace_picker(&client, args.workspace_id).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - let notebook = notebook_get(&client, notebook_id).await?; + let notebook = client.notebook_get(notebook_id).await?; let payload = FrontMatterUpdateRow::builder() .new_value(Some(args.new_value)) @@ -1028,7 +1028,9 @@ async fn handle_front_matter_edit_command(args: FrontMatterEditArguments) -> Res match args.front_matter_key { Some(key) => { - front_matter_update_key(&client, notebook_id, &key, payload).await?; + client + .front_matter_update_key(notebook_id, &key, payload) + .await?; } None => { let keys: Vec<_> = notebook @@ -1061,7 +1063,8 @@ async fn handle_front_matter_edit_command(args: FrontMatterEditArguments) -> Res match selection { Some(selection) => { - front_matter_update_key(&client, notebook_id, &keys[selection].0, payload) + client + .front_matter_update_key(notebook_id, &keys[selection].0, payload) .await?; } None => bail!("No key selected"), @@ -1112,7 +1115,9 @@ async fn handle_front_matter_add_collection_command( front_matter_collection_picker(&client, args.workspace_id, args.name).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - let fmc = workspace_front_matter_schema_get_by_name(&client, workspace_id, &fmc_name).await?; + let fmc = client + .workspace_front_matter_schema_get_by_name(workspace_id, &fmc_name) + .await?; let insertions: Vec = fmc .iter() @@ -1127,7 +1132,7 @@ async fn handle_front_matter_add_collection_command( let to_index: u32 = match args.position { Some(index) => index, None => { - let notebook = notebook_get(&client, notebook_id).await?; + let notebook = client.notebook_get(notebook_id).await?; notebook.front_matter_schema.len().try_into()? } }; @@ -1137,7 +1142,7 @@ async fn handle_front_matter_add_collection_command( .insertions(insertions) .build(); - front_matter_add_keys(&client, notebook_id, payload).await?; + client.front_matter_add_keys(notebook_id, payload).await?; info!("Successfully added {fmc_name} collection to front matter"); Ok(()) diff --git a/src/run/cell_writer.rs b/src/run/cell_writer.rs index b8b4d2f..b277dc7 100644 --- a/src/run/cell_writer.rs +++ b/src/run/cell_writer.rs @@ -1,8 +1,7 @@ use super::parse_logs::{contains_logs, parse_logs}; use anyhow::{anyhow, Context, Result}; use bytes::Bytes; -use fiberplane::api_client::clients::ApiClient; -use fiberplane::api_client::notebook_cells_append; +use fiberplane::api_client::ApiClient; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::notebooks; use fiberplane::models::notebooks::{Cell, CodeCell, LogCell, TextCell}; @@ -128,7 +127,9 @@ impl CellWriter { } async fn append_cell(&self, cell: Cell) -> Result { - let cell = notebook_cells_append(&self.client, self.notebook_id, None, None, vec![cell]) + let cell = self + .client + .notebook_cells_append(self.notebook_id, None, None, vec![cell]) .await .with_context(|| "Error appending cell to notebook")? .pop() diff --git a/src/shell/notebook_writer.rs b/src/shell/notebook_writer.rs index 3651d1e..1e9e1f3 100644 --- a/src/shell/notebook_writer.rs +++ b/src/shell/notebook_writer.rs @@ -1,6 +1,5 @@ use anyhow::{anyhow, Result}; -use fiberplane::api_client::clients::ApiClient; -use fiberplane::api_client::{notebook_cell_append_text, notebook_cells_append, profile_get}; +use fiberplane::api_client::ApiClient; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::formatting::{Annotation, AnnotationWithOffset, Formatting, Mention}; use fiberplane::models::notebooks::operations::CellAppendText; @@ -9,15 +8,15 @@ use fiberplane::models::timestamps::Timestamp; use fiberplane::models::utils::char_count; pub struct NotebookWriter { - config: ApiClient, + client: ApiClient, notebook_id: Base64Uuid, code_cell_id: String, heading_cell_id: String, } impl NotebookWriter { - pub async fn new(config: ApiClient, notebook_id: Base64Uuid) -> Result { - let user = profile_get(&config).await?; + pub async fn new(client: ApiClient, notebook_id: Base64Uuid) -> Result { + let user = client.profile_get().await?; let now = Timestamp::now_utc(); let timestamp = now.to_string(); @@ -27,52 +26,52 @@ impl NotebookWriter { user.name, timestamp ); let timestamp_offset = char_count(&content) - char_count(×tamp); - let header_cell = notebook_cells_append( - &config, - notebook_id, - None, - None, - vec![Cell::Heading( - HeadingCell::builder() - .id(String::new()) - .heading_type(HeadingType::H3) - .content(content) - .formatting(vec![ - AnnotationWithOffset::new( - 0, - Annotation::Mention( - Mention::builder().name(user.name).user_id(user.id).build(), + let header_cell = client + .notebook_cells_append( + notebook_id, + None, + None, + vec![Cell::Heading( + HeadingCell::builder() + .id(String::new()) + .heading_type(HeadingType::H3) + .content(content) + .formatting(vec![ + AnnotationWithOffset::new( + 0, + Annotation::Mention( + Mention::builder().name(user.name).user_id(user.id).build(), + ), ), - ), - AnnotationWithOffset::new( - timestamp_offset, - Annotation::Timestamp { timestamp: now }, - ), - ]) - .read_only(true) - .build(), - )], - ) - .await? - .pop() - .ok_or_else(|| anyhow!("No cells returned"))?; + AnnotationWithOffset::new( + timestamp_offset, + Annotation::Timestamp { timestamp: now }, + ), + ]) + .read_only(true) + .build(), + )], + ) + .await? + .pop() + .ok_or_else(|| anyhow!("No cells returned"))?; - let code_cell = notebook_cells_append( - &config, - notebook_id, - None, - None, - vec![Cell::Code( - CodeCell::builder() - .id(String::new()) - .content(String::new()) - .read_only(true) - .build(), - )], - ) - .await? - .pop() - .ok_or_else(|| anyhow!("No cells returned"))?; + let code_cell = client + .notebook_cells_append( + notebook_id, + None, + None, + vec![Cell::Code( + CodeCell::builder() + .id(String::new()) + .content(String::new()) + .read_only(true) + .build(), + )], + ) + .await? + .pop() + .ok_or_else(|| anyhow!("No cells returned"))?; let code_cell_id = match code_cell { Cell::Code(CodeCell { id, .. }) => id, @@ -85,7 +84,7 @@ impl NotebookWriter { }; Ok(Self { - config, + client, notebook_id, code_cell_id, heading_cell_id, @@ -95,16 +94,16 @@ impl NotebookWriter { pub async fn write(&self, buffer: Vec) -> Result<()> { let content = String::from_utf8(buffer)?; - notebook_cell_append_text( - &self.config, - self.notebook_id, - &self.code_cell_id, - CellAppendText::builder() - .content(content) - .formatting(Formatting::new()) - .build(), - ) - .await?; + self.client + .notebook_cell_append_text( + self.notebook_id, + &self.code_cell_id, + CellAppendText::builder() + .content(content) + .formatting(Formatting::new()) + .build(), + ) + .await?; Ok(()) } @@ -113,19 +112,19 @@ impl NotebookWriter { let now = Timestamp::now_utc(); let content = format!("\n🔴 Ended at: \t{now}"); - notebook_cell_append_text( - &self.config, - self.notebook_id, - &self.heading_cell_id, - CellAppendText::builder() - .content(content) - .formatting(vec![AnnotationWithOffset::new( - 0, - Annotation::Timestamp { timestamp: now }, - )]) - .build(), - ) - .await?; + self.client + .notebook_cell_append_text( + self.notebook_id, + &self.heading_cell_id, + CellAppendText::builder() + .content(content) + .formatting(vec![AnnotationWithOffset::new( + 0, + Annotation::Timestamp { timestamp: now }, + )]) + .build(), + ) + .await?; Ok(()) } diff --git a/src/snippets.rs b/src/snippets.rs index 41cc141..24b7a8f 100644 --- a/src/snippets.rs +++ b/src/snippets.rs @@ -9,10 +9,6 @@ use crate::templates::crop_description; use anyhow::{anyhow, bail, Context, Result}; use clap::{Parser, ValueEnum, ValueHint}; use cli_table::Table; -use fiberplane::api_client::{ - notebook_convert_to_snippet, notebook_get, snippet_create, snippet_delete, snippet_get, - snippet_list, snippet_update, -}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::names::Name; use fiberplane::models::notebooks::Cell; @@ -306,7 +302,7 @@ async fn handle_convert(args: ConvertArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; let notebook_id = notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - let notebook = notebook_get(&client, notebook_id).await?; + let notebook = client.notebook_get(notebook_id).await?; let cells: Vec = serde_json::from_str(&serde_json::to_string(¬ebook.cells)?)?; let display_cells: Vec = cells @@ -361,9 +357,9 @@ async fn handle_convert(args: ConvertArguments) -> Result<()> { let start_cell_id = cells[start_cell_index].id(); let end_cell_id = cells[end_cell_index].id(); - let body = - notebook_convert_to_snippet(&client, notebook_id, Some(start_cell_id), Some(end_cell_id)) - .await?; + let body = client + .notebook_convert_to_snippet(notebook_id, Some(start_cell_id), Some(end_cell_id)) + .await?; // Now create the snippet record let default_name = sluggify_str(¬ebook.title); @@ -375,7 +371,7 @@ async fn handle_convert(args: ConvertArguments) -> Result<()> { .description(description) .body(body) .build(); - let snippet = snippet_create(&client, workspace_id, snippet).await?; + let snippet = client.snippet_create(workspace_id, snippet).await?; match args.output { SnippetOutput::Table => output_details(GenericKeyValue::from_snippet(snippet)), @@ -407,7 +403,7 @@ async fn handle_create(args: CreateArguments) -> Result<()> { .body(body) .build(); - let snippet = snippet_create(&client, workspace_id, snippet).await?; + let snippet = client.snippet_create(workspace_id, snippet).await?; match args.output { SnippetOutput::Table => output_details(GenericKeyValue::from_snippet(snippet)), @@ -424,7 +420,8 @@ async fn handle_delete(args: DeleteArguments) -> Result<()> { let (workspace_id, snippet_name) = snippet_picker(&client, args.snippet_name, None).await?; - snippet_delete(&client, workspace_id, &snippet_name) + client + .snippet_delete(workspace_id, &snippet_name) .await .with_context(|| format!("Error deleting snippet {snippet_name}"))?; @@ -437,13 +434,13 @@ async fn handle_list(args: ListArguments) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let snippets = snippet_list( - &client, - workspace_id, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - ) - .await?; + let snippets = client + .snippet_list( + workspace_id, + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + ) + .await?; match args.output { SnippetListOutput::Table => { @@ -458,7 +455,7 @@ async fn handle_get(args: GetArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let (workspace_id, snippet_name) = snippet_picker(&client, args.snippet_name, None).await?; - let snippet = snippet_get(&client, workspace_id, &snippet_name).await?; + let snippet = client.snippet_get(workspace_id, &snippet_name).await?; match args.output { SnippetOutput::Table => output_details(GenericKeyValue::from_snippet(snippet)), @@ -492,7 +489,8 @@ async fn handle_update(args: UpdateArguments) -> Result<()> { snippet.description = args.description; snippet.body = body; - let snippet = snippet_update(&client, workspace_id, &snippet_name, snippet) + let snippet = client + .snippet_update(workspace_id, &snippet_name, snippet) .await .with_context(|| format!("Error updating snippet {snippet_name}"))?; info!("Updated snippet"); diff --git a/src/templates.rs b/src/templates.rs index 143d5f9..001ceb1 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -4,11 +4,7 @@ use crate::{config::api_client_configuration, fp_urls::NotebookUrlBuilder}; use anyhow::{anyhow, bail, Context, Error, Result}; use clap::{Parser, ValueEnum, ValueHint}; use cli_table::Table; -use fiberplane::api_client::clients::ApiClient; -use fiberplane::api_client::{ - notebook_create, notebook_get, template_create, template_delete, template_expand, template_get, - template_list, template_update, trigger_create, -}; +use fiberplane::api_client::ApiClient; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::names::Name; use fiberplane::models::notebooks::{ @@ -508,16 +504,16 @@ async fn expand_template_api( template_name: Name, template_arguments: Option, ) -> Result { - let notebook = template_expand( - client, - workspace_id, - &template_name, - template_arguments.map_or_else(TemplateExpandPayload::new, |args| { - Map::from_iter(args.0.into_iter()) - }), - ) - .await - .with_context(|| format!("Error expanding template: {template_name}"))?; + let notebook = client + .template_expand( + workspace_id, + &template_name, + template_arguments.map_or_else(TemplateExpandPayload::new, |args| { + Map::from_iter(args.0.into_iter()) + }), + ) + .await + .with_context(|| format!("Error expanding template: {template_name}"))?; Ok(notebook) } @@ -540,7 +536,8 @@ async fn expand_template_file( let notebook = expand_template(template, template_args).context("expanding template")?; - let notebook = notebook_create(client, workspace_id, notebook) + let notebook = client + .notebook_create(workspace_id, notebook) .await .context("Error creating notebook")?; @@ -555,7 +552,8 @@ async fn handle_convert_command(args: ConvertArguments) -> Result<()> { let notebook_id = interactive::notebook_picker(&client, args.notebook_id, Some(workspace_id)).await?; - let mut notebook = notebook_get(&client, notebook_id) + let mut notebook = client + .notebook_get(notebook_id) .await .context("Error fetching notebook")?; @@ -593,14 +591,16 @@ async fn handle_convert_command(args: ConvertArguments) -> Result<()> { // Create or update the template let (template, trigger_url) = if let Some(template_name) = args.template_name { - if template_get(&client, workspace_id, &template_name) + if client + .template_get(workspace_id, &template_name) .await .is_ok() { let mut template = UpdateTemplate::builder().body(template).build(); template.description = description; - let template = template_update(&client, workspace_id, &template_name, template.clone()) + let template = client + .template_update(workspace_id, &template_name, template.clone()) .await .with_context(|| format!("Error updating template {template_name}"))?; @@ -687,7 +687,7 @@ async fn handle_get_command(args: GetArguments) -> Result<()> { let (workspace_id, template_name) = interactive::template_picker(&client, args.template_name, None).await?; - let template = template_get(&client, workspace_id, &template_name).await?; + let template = client.template_get(workspace_id, &template_name).await?; match args.output { TemplateOutput::Table => output_details(GenericKeyValue::from_template(template)), @@ -704,7 +704,8 @@ async fn handle_delete_command(args: DeleteArguments) -> Result<()> { let (workspace_id, template_name) = interactive::template_picker(&client, args.template_name, None).await?; - template_delete(&client, workspace_id, &template_name) + client + .template_delete(workspace_id, &template_name) .await .with_context(|| format!("Error deleting template {template_name}"))?; @@ -718,13 +719,13 @@ async fn handle_list_command(args: ListArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let templates = template_list( - &client, - workspace_id, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - ) - .await?; + let templates = client + .template_list( + workspace_id, + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + ) + .await?; match args.output { TemplateListOutput::Table => { @@ -756,7 +757,8 @@ async fn handle_update_command(args: UpdateArguments) -> Result<()> { template.description = args.description; template.body = body; - let template = template_update(&client, workspace_id, &template_name, template) + let template = client + .template_update(workspace_id, &template_name, template) .await .with_context(|| format!("Error updating template {template_name}"))?; info!("Updated template"); @@ -952,22 +954,23 @@ async fn create_template_and_trigger( false, ); - let template = template_create(client, workspace_id, template) + let template = client + .template_create(workspace_id, template) .await .with_context(|| "Error creating template")?; info!("Uploaded template"); let trigger_url = if create_trigger { - let trigger = trigger_create( - client, - workspace_id, - NewTrigger::builder() - .title(format!("{} Trigger", &template.name)) - .template_name(template.name.clone()) - .build(), - ) - .await - .context("Error creating trigger")?; + let trigger = client + .trigger_create( + workspace_id, + NewTrigger::builder() + .title(format!("{} Trigger", &template.name)) + .template_name(template.name.clone()) + .build(), + ) + .await + .context("Error creating trigger")?; let trigger_url = client.server.join(&format!( "api/triggers/{}/{}", diff --git a/src/tokens.rs b/src/tokens.rs index 4c80bfa..c78c9c0 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -3,7 +3,6 @@ use crate::output::{output_details, output_json, output_list, GenericKeyValue}; use anyhow::Result; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::{token_create, token_delete, token_list}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::sorting::{SortDirection, TokenListSortFields}; use fiberplane::models::tokens::{NewToken, Token, TokenSummary}; @@ -132,7 +131,7 @@ pub struct DeleteArguments { async fn handle_token_create_command(args: CreateArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - let token = token_create(&client, NewToken::new(args.name)).await?; + let token = client.token_create(NewToken::new(args.name)).await?; if !matches!(args.output, TokenCreateOutput::Token) { info!("Successfully created new token"); @@ -151,14 +150,14 @@ async fn handle_token_create_command(args: CreateArguments) -> Result<()> { async fn handle_token_list_command(args: ListArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - let tokens = token_list( - &client, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - args.page, - args.limit, - ) - .await?; + let tokens = client + .token_list( + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + args.page, + args.limit, + ) + .await?; match args.output { TokenListOutput::Table => { @@ -172,7 +171,7 @@ async fn handle_token_list_command(args: ListArguments) -> Result<()> { async fn handle_token_delete_command(args: DeleteArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - token_delete(&client, args.id).await?; + client.token_delete(args.id).await?; info!("Successfully deleted token"); Ok(()) diff --git a/src/triggers.rs b/src/triggers.rs index 1b4d0d0..de51fa6 100644 --- a/src/triggers.rs +++ b/src/triggers.rs @@ -5,10 +5,8 @@ use crate::templates::TemplateArguments; use anyhow::{Context, Result}; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::clients::{default_config, ApiClient}; -use fiberplane::api_client::{ - trigger_create, trigger_delete, trigger_get, trigger_invoke, trigger_list, -}; +use fiberplane::api_client::clients::default_config; +use fiberplane::api_client::ApiClient; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::names::Name; use fiberplane::models::notebooks::{ @@ -200,7 +198,8 @@ async fn handle_trigger_create_command(args: CreateArguments) -> Result<()> { .template_name(template_name) .default_arguments(default_arguments) .build(); - let trigger = trigger_create(&client, workspace_id, trigger) + let trigger = client + .trigger_create(workspace_id, trigger) .await .context("Error creating trigger")?; @@ -217,7 +216,8 @@ async fn handle_trigger_get_command(args: GetArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url.clone()).await?; let trigger_id = interactive::trigger_picker(&client, args.trigger_id, None).await?; - let trigger = trigger_get(&client, trigger_id) + let trigger = client + .trigger_get(trigger_id) .await .with_context(|| "Error getting trigger details")?; @@ -233,7 +233,8 @@ async fn handle_trigger_delete_command(args: DeleteArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let trigger_id = interactive::trigger_picker(&client, args.trigger_id, None).await?; - trigger_delete(&client, trigger_id) + client + .trigger_delete(trigger_id) .await .context("Error deleting trigger")?; @@ -245,7 +246,8 @@ async fn handle_trigger_delete_command(args: DeleteArguments) -> Result<()> { async fn handle_trigger_list_command(args: ListArguments) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = interactive::workspace_picker(&client, args.workspace_id).await?; - let mut triggers = trigger_list(&client, workspace_id) + let mut triggers = client + .trigger_list(workspace_id) .await .with_context(|| "Error getting triggers")?; @@ -271,17 +273,17 @@ async fn handle_trigger_invoke_command(args: InvokeArguments) -> Result<()> { server: args.base_url, }; - let response = trigger_invoke( - &anon_client, - trigger_id, - &secret_key, - args.template_arguments - .map_or_else(TemplateExpandPayload::new, |args| { - Map::from_iter(args.0.into_iter()) - }), - ) - .await - .context("Error invoking trigger")?; + let response = anon_client + .trigger_invoke( + trigger_id, + &secret_key, + args.template_arguments + .map_or_else(TemplateExpandPayload::new, |args| { + Map::from_iter(args.0.into_iter()) + }), + ) + .await + .context("Error invoking trigger")?; match args.output { TriggerOutput::Table => { diff --git a/src/users.rs b/src/users.rs index 79b526a..32a0845 100644 --- a/src/users.rs +++ b/src/users.rs @@ -2,7 +2,6 @@ use crate::config::api_client_configuration; use crate::output::{output_details, output_json, GenericKeyValue}; use anyhow::Result; use clap::{Parser, ValueEnum}; -use fiberplane::api_client::profile_get; use fiberplane::models::users::Profile; use std::path::PathBuf; use url::Url; @@ -52,7 +51,7 @@ pub async fn handle_command(args: Arguments) -> Result<()> { async fn handle_get_profile_command(args: GetArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - let profile = profile_get(&client).await?; + let profile = client.profile_get().await?; match args.output { ProfileOutput::Table => output_details(GenericKeyValue::from_profile(profile)), diff --git a/src/views.rs b/src/views.rs index cbbd258..8f9cf0e 100644 --- a/src/views.rs +++ b/src/views.rs @@ -6,7 +6,6 @@ use crate::KeyValueArgument; use anyhow::Result; use clap::{Parser, ValueEnum}; use cli_table::Table; -use fiberplane::api_client::{view_create, view_delete, view_list, view_update}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::labels::Label; use fiberplane::models::names::Name; @@ -133,7 +132,7 @@ async fn handle_create(args: CreateArguments) -> Result<()> { view.sort_by = args.sort_by; view.sort_direction = args.sort_direction; - let view = view_create(&client, workspace_id, view).await?; + let view = client.view_create(workspace_id, view).await?; info!("Successfully created new view"); @@ -184,15 +183,15 @@ async fn handle_list(args: ListArguments) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let views = view_list( - &client, - workspace_id, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - args.page, - args.limit, - ) - .await?; + let views = client + .view_list( + workspace_id, + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + args.page, + args.limit, + ) + .await?; match args.output { ViewOutput::Table => { @@ -229,7 +228,7 @@ async fn handle_delete(args: DeleteArguments) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; let view_name = view_picker(&client, args.workspace_id, args.view_name).await?; - view_delete(&client, workspace_id, &view_name).await?; + client.view_delete(workspace_id, &view_name).await?; info!("Successfully deleted view"); Ok(()) @@ -342,7 +341,7 @@ async fn handle_update(args: UpdateArguments) -> Result<()> { update.sort_by = clear_or_update(args.clear_sort_by, args.sort_by); update.sort_direction = clear_or_update(args.clear_sort_direction, args.sort_direction); - view_update(&client, workspace_id, &view_name, update).await?; + client.view_update(workspace_id, &view_name, update).await?; info!("Successfully updated view"); Ok(()) diff --git a/src/webhooks.rs b/src/webhooks.rs index 93e3417..cfb2e70 100644 --- a/src/webhooks.rs +++ b/src/webhooks.rs @@ -8,10 +8,6 @@ use anyhow::Result; use clap::Parser; use clap::ValueEnum; use cli_table::Table; -use fiberplane::api_client::{ - webhook_create, webhook_delete, webhook_delivery_get, webhook_delivery_list, - webhook_delivery_resend, webhook_list, webhook_update, -}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::webhooks::{ NewWebhook, UpdateWebhook, Webhook, WebhookCategory, WebhookDelivery, WebhookDeliverySummary, @@ -119,7 +115,7 @@ async fn handle_webhook_create(args: CreateArgs) -> Result<()> { .enabled(enabled) .build(); - let webhook = webhook_create(&client, workspace_id, payload).await?; + let webhook = client.webhook_create(workspace_id, payload).await?; if !webhook.successful { warn!("The webhook has been created in the disabled state because it failed to handle the \"ping\" event."); @@ -170,7 +166,9 @@ async fn handle_webhook_list(args: ListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let webhooks = webhook_list(&client, workspace_id, args.page, args.limit).await?; + let webhooks = client + .webhook_list(workspace_id, args.page, args.limit) + .await?; match args.output { WebhookOutput::Table => { @@ -214,7 +212,7 @@ async fn handle_webhook_delete(args: DeleteArgs) -> Result<()> { return Ok(()); } - webhook_delete(&client, workspace_id, webhook_id).await?; + client.webhook_delete(workspace_id, webhook_id).await?; info!("Successfully deleted webhook"); Ok(()) @@ -275,7 +273,9 @@ async fn handle_webhook_update(args: UpdateArgs) -> Result<()> { payload.events = args.categories; payload.enabled = args.enabled; - let webhook = webhook_update(&client, workspace_id, webhook_id, payload).await?; + let webhook = client + .webhook_update(workspace_id, webhook_id, payload) + .await?; if args.endpoint.is_some() && !webhook.successful { warn!("The webhook has been updated into the disabled state because it failed to handle the \"ping\" event."); @@ -336,8 +336,9 @@ async fn handle_webhook_delivery_list(args: WebhookDeliveryListArgs) -> Result<( let workspace_id = workspace_picker(&client, args.workspace_id).await?; let webhook_id = webhook_picker(&client, workspace_id, args.webhook_id).await?; - let deliveries = - webhook_delivery_list(&client, workspace_id, webhook_id, args.page, args.limit).await?; + let deliveries = client + .webhook_delivery_list(workspace_id, webhook_id, args.page, args.limit) + .await?; match args.output { WebhookOutput::Table => { @@ -385,7 +386,9 @@ async fn handle_webhook_delivery_info(args: WebhookDeliveryInfoArgs) -> Result<( let delivery_id = webhook_delivery_picker(&client, workspace_id, webhook_id, args.delivery_id).await?; - let delivery = webhook_delivery_get(&client, workspace_id, webhook_id, delivery_id).await?; + let delivery = client + .webhook_delivery_get(workspace_id, webhook_id, delivery_id) + .await?; match args.output { WebhookDeliveryOutput::Table => { @@ -442,7 +445,9 @@ async fn handle_webhook_delivery_resend(args: WebhookDeliveryResendArgs) -> Resu let delivery_id = webhook_delivery_picker(&client, workspace_id, webhook_id, args.delivery_id).await?; - webhook_delivery_resend(&client, workspace_id, webhook_id, delivery_id).await?; + client + .webhook_delivery_resend(workspace_id, webhook_id, delivery_id) + .await?; info!("Successfully triggered a resend on the delivery"); Ok(()) diff --git a/src/workspaces.rs b/src/workspaces.rs index 3a390ac..f07d8c8 100644 --- a/src/workspaces.rs +++ b/src/workspaces.rs @@ -8,11 +8,6 @@ use anyhow::{anyhow, bail, Result}; use clap::{Parser, ValueEnum}; use cli_table::Table; use dialoguer::FuzzySelect; -use fiberplane::api_client::{ - workspace_create, workspace_delete, workspace_get, workspace_invite_create, - workspace_invite_delete, workspace_invite_get, workspace_leave, workspace_list, - workspace_update, workspace_user_list, workspace_user_remove, workspace_user_update, -}; use fiberplane::base64uuid::Base64Uuid; use fiberplane::models::data_sources::{ProviderType, SelectedDataSource}; use fiberplane::models::names::Name; @@ -158,14 +153,14 @@ async fn handle_workspace_create(args: CreateArgs) -> Result<()> { .ok_or_else(|| anyhow!("Name is required"))?; let display_name = text_opt("Display Name", args.display_name, Some(name.to_string())); - let workspace = workspace_create( - &client, - NewWorkspace::builder() - .name(name) - .display_name(display_name.unwrap_or_default()) - .build(), - ) - .await?; + let workspace = client + .workspace_create( + NewWorkspace::builder() + .name(name) + .display_name(display_name.unwrap_or_default()) + .build(), + ) + .await?; info!("Successfully created new workspace"); @@ -195,7 +190,7 @@ async fn handle_workspace_delete(args: DeleteArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - workspace_delete(&client, workspace_id).await?; + client.workspace_delete(workspace_id).await?; info!("Successfully deleted workspace"); Ok(()) @@ -236,12 +231,12 @@ struct ListArgs { async fn handle_workspace_list(args: ListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - let list = workspace_list( - &client, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - ) - .await?; + let list = client + .workspace_list( + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + ) + .await?; match args.output { WorkspaceListOutput::Table => { @@ -272,7 +267,7 @@ async fn handle_workspace_leave(args: LeaveArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - workspace_leave(&client, workspace_id).await?; + client.workspace_leave(workspace_id).await?; info!("Successfully left workspace"); Ok(()) @@ -311,12 +306,9 @@ async fn handle_invite_create(args: InviteCreateArgs) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; let email = text_req("Email", args.email, None)?; - let invite = workspace_invite_create( - &client, - workspace_id, - NewWorkspaceInvite::new(email, args.role), - ) - .await?; + let invite = client + .workspace_invite_create(workspace_id, NewWorkspaceInvite::new(email, args.role)) + .await?; if !matches!(args.output, NewInviteOutput::InviteUrl) { info!("Successfully invited user to workspace"); @@ -372,15 +364,15 @@ async fn handle_invite_list(args: InviteListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let invites = workspace_invite_get( - &client, - workspace_id, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - args.page, - args.limit, - ) - .await?; + let invites = client + .workspace_invite_get( + workspace_id, + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + args.page, + args.limit, + ) + .await?; match args.output { PendingInvitesOutput::Table => { @@ -410,7 +402,7 @@ struct InviteDeleteArgs { async fn handle_invite_delete(args: InviteDeleteArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; - workspace_invite_delete(&client, args.invite_id).await?; + client.workspace_invite_delete(args.invite_id).await?; info!("Successfully deleted invitation from workspace"); Ok(()) @@ -456,13 +448,13 @@ async fn handle_user_list(args: UserListArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let users = workspace_user_list( - &client, - workspace_id, - args.sort_by.map(Into::<&str>::into), - args.sort_direction.map(Into::<&str>::into), - ) - .await?; + let users = client + .workspace_user_list( + workspace_id, + args.sort_by.map(Into::<&str>::into), + args.sort_direction.map(Into::<&str>::into), + ) + .await?; match args.output { UserListOutput::Table => { @@ -507,7 +499,9 @@ async fn handle_user_update(args: UserUpdateArgs) -> Result<()> { Some(role) => UpdateWorkspaceUser::builder().role(role).build(), None => UpdateWorkspaceUser::builder().build(), }; - workspace_user_update(&client, workspace_id, user, payload).await?; + client + .workspace_user_update(workspace_id, user, payload) + .await?; info!("Successfully updated user within workspace"); Ok(()) @@ -539,7 +533,7 @@ async fn handle_user_delete(args: UserDeleteArgs) -> Result<()> { let workspace_id = workspace_picker(&client, args.workspace_id).await?; let user = workspace_user_picker(&client, &workspace_id, args.user_id).await?; - workspace_user_remove(&client, workspace_id, user).await?; + client.workspace_user_remove(workspace_id, user).await?; info!("Successfully removed user from workspace"); Ok(()) @@ -675,12 +669,12 @@ async fn handle_move_owner(args: MoveOwnerArgs) -> Result<()> { let new_owner = workspace_user_picker(&client, &workspace_id, args.new_owner_id).await?; - workspace_update( - &client, - workspace_id, - UpdateWorkspace::builder().owner(new_owner).build(), - ) - .await?; + client + .workspace_update( + workspace_id, + UpdateWorkspace::builder().owner(new_owner).build(), + ) + .await?; info!("Successfully moved ownership of workspace"); Ok(()) @@ -690,14 +684,14 @@ async fn handle_change_name(args: ChangeNameArgs) -> Result<()> { let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - workspace_update( - &client, - workspace_id, - UpdateWorkspace::builder() - .display_name(args.new_name) - .build(), - ) - .await?; + client + .workspace_update( + workspace_id, + UpdateWorkspace::builder() + .display_name(args.new_name) + .build(), + ) + .await?; info!("Successfully changed name of workspace"); Ok(()) @@ -707,7 +701,8 @@ async fn handle_get_default_data_sources(args: GetDefaultDataSourcesArgs) -> Res let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let default_data_sources = workspace_get(&client, workspace_id) + let default_data_sources = client + .workspace_get(workspace_id) .await? .default_data_sources; @@ -728,7 +723,8 @@ async fn handle_set_default_data_source(args: SetDefaultDataSourcesArgs) -> Resu let data_source = data_source_picker(&client, Some(workspace_id), args.data_source_name).await?; - let mut default_data_sources = workspace_get(&client, workspace_id) + let mut default_data_sources = client + .workspace_get(workspace_id) .await? .default_data_sources; @@ -743,14 +739,14 @@ async fn handle_set_default_data_source(args: SetDefaultDataSourcesArgs) -> Resu }; default_data_sources.insert(data_source.provider_type.clone(), sds); - workspace_update( - &client, - workspace_id, - UpdateWorkspace::builder() - .default_data_sources(default_data_sources) - .build(), - ) - .await?; + client + .workspace_update( + workspace_id, + UpdateWorkspace::builder() + .default_data_sources(default_data_sources) + .build(), + ) + .await?; info!( "Successfully set {}{} to be the default data source for {} queries", @@ -769,7 +765,8 @@ async fn handle_unset_default_data_source(args: UnsetDefaultDataSourcesArgs) -> let client = api_client_configuration(args.token, args.config, args.base_url).await?; let workspace_id = workspace_picker(&client, args.workspace_id).await?; - let mut default_data_sources = workspace_get(&client, workspace_id) + let mut default_data_sources = client + .workspace_get(workspace_id) .await? .default_data_sources; @@ -788,14 +785,14 @@ async fn handle_unset_default_data_source(args: UnsetDefaultDataSourcesArgs) -> default_data_sources.remove(&provider_type); - workspace_update( - &client, - workspace_id, - UpdateWorkspace::builder() - .default_data_sources(default_data_sources) - .build(), - ) - .await?; + client + .workspace_update( + workspace_id, + UpdateWorkspace::builder() + .default_data_sources(default_data_sources) + .build(), + ) + .await?; info!("Successfully unset default data source for workspace"); Ok(())