Skip to content

Commit

Permalink
Refactor to ApiClient functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hatchan committed Feb 27, 2024
1 parent 3a182a7 commit 4dedff6
Show file tree
Hide file tree
Showing 21 changed files with 460 additions and 453 deletions.
9 changes: 4 additions & 5 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -110,19 +109,19 @@ 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 {
let mut config = Config::load(args.config).await?;

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?;
Expand Down
14 changes: 7 additions & 7 deletions src/daemons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:?}"))?;

Expand Down Expand Up @@ -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<String, ProxySummaryWithConnectedDataSources> = proxies
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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(())
Expand Down
15 changes: 8 additions & 7 deletions src/data_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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)),
Expand All @@ -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 => {
Expand Down
31 changes: 15 additions & 16 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");

Expand All @@ -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 => {
Expand Down Expand Up @@ -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(())
Expand Down
42 changes: 22 additions & 20 deletions src/experiments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -149,14 +148,16 @@ 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?;

// If we don't already know the user name, load it from the API and save it
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());
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>(
Expand Down
50 changes: 23 additions & 27 deletions src/front_matter.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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;
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)]
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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!(
"{}",
Expand Down
3 changes: 1 addition & 2 deletions src/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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;
Expand Down Expand Up @@ -62,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 => {
Expand Down
Loading

0 comments on commit 4dedff6

Please sign in to comment.