From a4f9e923a8a00b7c48ff069c32f0dcaba22b8d31 Mon Sep 17 00:00:00 2001 From: tuhinmallick Date: Sat, 17 Dec 2022 19:59:22 +0100 Subject: [PATCH 1/2] added get dashboard --- R/ref-class-client.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/ref-class-client.R b/R/ref-class-client.R index 813e355..83cade7 100644 --- a/R/ref-class-client.R +++ b/R/ref-class-client.R @@ -137,3 +137,15 @@ MetabaseClient$methods( ) } ) +MetabaseClient$methods( + get_dashboards = function() { + dashboards <- .self$authenticated_get("/dashboard/") + print(dashboards) + do.call( + dplyr::bind_rows, + lapply(dashboards, function(data) { + list(dashboard_id = data$id, dashboard_name = data$name, collection_id = data$collection_id) + }) + ) + } +) \ No newline at end of file From 8440fffac90a9acf684a6d9a525f788581c11214 Mon Sep 17 00:00:00 2001 From: tuhinmallick Date: Mon, 19 Dec 2022 21:13:00 +0100 Subject: [PATCH 2/2] added the get dashboard functionality --- R/ref-class-client.R | 62 ++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/R/ref-class-client.R b/R/ref-class-client.R index 83cade7..22c663c 100644 --- a/R/ref-class-client.R +++ b/R/ref-class-client.R @@ -1,3 +1,17 @@ +validate_env_var <- function(env_var_name, assignment_name) { + value <- Sys.getenv(env_var_name) + if (value == "") { + stop(paste0( + "Either the MetabaseClient parameter '", + assignment_name, + "' has to be set or the env variable '", + env_var_name, + "' needs to be defined." + )) + } + return(value) +} + MetabaseClient <- setRefClass( # nolint "MetabaseClient", fields = list( @@ -15,17 +29,17 @@ MetabaseClient$methods( session <<- "" api_uri_prefix <<- "/api" if (is.null(user)) { - user <<- Sys.getenv("METABASE_USER") + user <<- validate_env_var("METABASE_USER", "user") } else { user <<- user } if (is.null(password)) { - password <<- Sys.getenv("METABASE_PWD") + password <<- validate_env_var("METABASE_PWD", "password") } else { password <<- password } if (is.null(metabase_url)) { - metabase_url <<- Sys.getenv("METABASE_URL") + metabase_url <<- validate_env_var("METABASE_URL", "metabase_url") } else { metabase_url <<- metabase_url } @@ -34,7 +48,8 @@ MetabaseClient$methods( MetabaseClient$methods( authenticate = function() { - response <- httr::POST(paste0(.self$metabase_url, .self$api_uri_prefix, "/session"), + response <- httr::POST( + paste0(.self$metabase_url, .self$api_uri_prefix, "/session"), body = list(username = .self$user, password = .self$password), encode = "json" ) @@ -102,7 +117,9 @@ MetabaseClient$methods( MetabaseClient$methods( get_items = function(collection_id) { - items <- .self$authenticated_get(paste0("/collection/", collection_id, "/items")) + items <- .self$authenticated_get( + paste0("/collection/", collection_id, "/items") + ) do.call( dplyr::bind_rows, lapply(items$data, function(item) { @@ -119,33 +136,46 @@ MetabaseClient$methods( } ) -MetabaseClient$methods( - create_collection = function(collection_name,parent_collection_id ) { - params <- list( "name"=collection_name,'parent_id'=parent_collection_id, 'color'='#509EE3') - authenticated_post (endpoint = "/collection/", payload = params) - } -) - MetabaseClient$methods( get_tables = function() { tables <- .self$authenticated_get("/table/") do.call( dplyr::bind_rows, lapply(tables, function(data) { - list(table_id = data$id, table_name = data$name, db_id = data$db_id, db_name = data$db$name) + list( + table_id = data$id, + table_name = data$name, + db_id = data$db_id, + db_name = data$db$name + ) }) ) } ) + MetabaseClient$methods( - get_dashboards = function() { + get_dashboards = function() { dashboards <- .self$authenticated_get("/dashboard/") - print(dashboards) do.call( dplyr::bind_rows, lapply(dashboards, function(data) { - list(dashboard_id = data$id, dashboard_name = data$name, collection_id = data$collection_id) + list( + dashboard_id = data$id, + dashboard_name = data$name, + collection_id = data$collection_id + ) }) ) } +) + +MetabaseClient$methods( + create_collection = function(collection_name, parent_collection_id) { + params <- list( + "name" = collection_name, + "parent_id" = parent_collection_id, + "color" = "#509EE3" + ) + authenticated_post(endpoint = "/collection/", payload = params) + } ) \ No newline at end of file