diff --git a/DESCRIPTION b/DESCRIPTION index 6d4b7b3..2c765b1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,12 +1,12 @@ Package: econdatar Title: Automation of Data Tasks to and from Codera Analytics' Econometric Data Services -Version: 3.0.2 +Version: 3.1.0 Date: 2024-04-24 Authors@R: c(person("Byron", "Botha", role = c("aut", "cre"), email = "byron@codera.co.za"), person("Sebastian", "Krantz", role = "ctb")) Description: Automation of data tasks to and from econometric data services. Using this package users can download data from directly into R (in tidy format) after signing up for a free account. hosts a comprehensive database of South African macroeconomic data. Depends: R (>= 4.2.0) -Imports: httr, jsonlite, readODS, collapse, data.table +Imports: httr, jose, jsonlite, readODS, collapse, data.table Suggests: tcltk URL: https://www.econdata.co.za/ BugReports: https://github.com/coderaanalytics/econdatar/issues diff --git a/NAMESPACE b/NAMESPACE index cf36fc1..9efab94 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,8 +2,9 @@ importFrom("collapse", "add_vars<-", "ckmatch", "dapply", "fmutate", "fnobs", "frename", "get_vars<-", "qDT", "qF", "ss", "unlist2d", "vlabels", "vlabels<-") importFrom("data.table", "dcast", "rbindlist", "setcolorder") -importFrom("httr", "POST", "GET", "PUT", "accept", "accept_json", "authenticate", - "content", "content_type", "cookies", "set_cookies", "upload_file") +importFrom("httr", "POST", "GET", "PUT", "accept", "add_headers", "accept_json", + "authenticate", "content", "content_type", "upload_file") +importFrom("jose", "jwt_split") importFrom("jsonlite", "fromJSON", "toJSON", "unbox") importFrom("readODS", "write_ods", "read_ods") importFrom("stats", "na.omit") diff --git a/R/econdata_credentials.R b/R/econdata_credentials.R index 88567c0..adff19f 100644 --- a/R/econdata_credentials.R +++ b/R/econdata_credentials.R @@ -3,39 +3,30 @@ econdata_credentials <- function() { stop("Package \"tcltk\" needed for this function to work.", call. = FALSE) } - user <- pswd <- NULL # Need to add global bindings for variables - username <- tcltk::tclVar(Sys.info()["user"]) - password <- tcltk::tclVar("") + tkn <- NULL # Need to add global bindings for variables + token <- tcltk::tclVar("") tt <- tcltk::tktoplevel() - tcltk::tkwm.title(tt, "www.econdata.co.za credentials") - user.entry <- tcltk::tkentry(tt, textvariable = username) - pswd.entry <- tcltk::tkentry(tt, textvariable = password, show = "*") + tcltk::tkwm.title(tt, "econdata.co.za credentials") + tkn.entry <- tcltk::tkentry(tt, textvariable = token) - reset <- function() { - tcltk::tclvalue(username) <- Sys.info()["user"] - tcltk::tclvalue(password) <- "" - } + reset <- function() tcltk::tclvalue(token) <- "" reset.but <- tcltk::tkbutton(tt, text = "Reset", command = reset) submit <- function() { - user <- tcltk::tclvalue(username) - pswd <- tcltk::tclvalue(password) + tkn <- tcltk::tclvalue(token) e <- parent.env(environment()) - e$user <- user - e$pswd <- pswd + e$tkn <- tkn tcltk::tkdestroy(tt) } - submit.but <- tcltk::tkbutton(tt, text = "submit", command = submit) + submit.but <- tcltk::tkbutton(tt, text = "Submit", command = submit) - tcltk::tkgrid(tcltk::tklabel(tt, text = "Enter User Details"), + tcltk::tkgrid(tcltk::tklabel(tt, text = "Enter Token Details"), columnspan = 2) - tcltk::tkgrid(tcltk::tklabel(tt, text = "Username"), - user.entry, pady = 10, padx = 10) - tcltk::tkgrid(tcltk::tklabel(tt, text = "Password"), - pswd.entry, pady = 10, padx = 10) + tcltk::tkgrid(tcltk::tklabel(tt, text = "API Token"), + tkn.entry, pady = 10, padx = 10) tcltk::tkgrid(submit.but, reset.but, pady = 10, padx = 50) tcltk::tkwait.window(tt) - return(c(user, pswd)) + return(tkn) } diff --git a/R/get_metadata.R b/R/get_metadata.R index 31d9deb..41e9fb6 100644 --- a/R/get_metadata.R +++ b/R/get_metadata.R @@ -1,11 +1,5 @@ -get_metadata <- function(x, ...) { - params <- list(...) - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] +get_metadata <- function(x) { + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Fetch data structure definition (metadata) ---- @@ -19,8 +13,8 @@ get_metadata <- function(x, ...) { path = paste(c(env$registry$path, "provisionagreements", provision_agreement_ref), collapse = "/"), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { stop(content(response, type = "application/json", encoding = "UTF-8")) @@ -38,8 +32,8 @@ get_metadata <- function(x, ...) { path = paste(c(env$registry$path, "dataflows", dataflow_ref), collapse = "/"), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { stop(content(response, type = "application/json", encoding = "UTF-8")) @@ -57,8 +51,8 @@ get_metadata <- function(x, ...) { "datastructures", data_structure_ref), collapse = "/"), query = list(relations = "references"), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { stop(content(response, type = "application/json", encoding = "UTF-8")) diff --git a/R/login_helper.R b/R/login_helper.R index 07512ef..efaa7bc 100644 --- a/R/login_helper.R +++ b/R/login_helper.R @@ -1,27 +1,20 @@ .pkgenv <- new.env(parent = emptyenv()) -login_helper <- function(credentials, login_url) { - if (!is.null(credentials)) { - creds <- unlist(strsplit(credentials, ";")) - } else if (Sys.getenv("ECONDATA_CREDENTIALS") != "") { +login_helper <- function(auth) { + if (Sys.getenv("ECONDATA_CREDENTIALS") != "") { creds <- unlist(strsplit(Sys.getenv("ECONDATA_CREDENTIALS"), ";")) + response <- POST(auth$url, + path = auth$path, + body = list(grant_type = "client_credentials", + client_id = creds[1], + client_secret = creds[2]), + encode = "form", + accept_json()) + if (response$status_code != 200) + stop(content(response)) + token <- content(response)$access_token } else { - creds <- econdata_credentials() + token <- econdata_credentials() } - - response <- POST(login_url, - path = "/signin", - body = list(username = creds[1], - password = creds[2]), - encode = "form") - - if (response$status_code != 200) - stop(content(response, encoding = "UTF-8")) - - cookie_jar <- cookies(response) - domain <- substr(login_url, 9, nchar(login_url)) - session <- cookie_jar[which(cookie_jar[, 1] == paste0("#HttpOnly_", domain) & - cookie_jar[, 6] == "ring-session"), ] - assign("econdata_session", as.character(session), envir = .pkgenv) - lockBinding("econdata_session", .pkgenv) + assign("econdata_token", paste("Bearer", token), envir = .pkgenv) } diff --git a/R/read_database.R b/R/read_database.R index d57dc59..c72c4ee 100644 --- a/R/read_database.R +++ b/R/read_database.R @@ -4,11 +4,6 @@ read_database <- function(id, include_series = FALSE, tidy = FALSE, ...) { # Parameters ---- params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } if (!is.null(params$agencyid)) { agencyid <- params$agencyid } else { @@ -19,12 +14,7 @@ read_database <- function(id, include_series = FALSE, tidy = FALSE, ...) { } else { version <- "latest" } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Fetch data set(s) ---- @@ -34,8 +24,16 @@ read_database <- function(id, include_series = FALSE, tidy = FALSE, ...) { data_message <- fromJSON(params$file, simplifyVector = FALSE) message("Data set(s) successfully retrieved from local storage.\n") } else { - if (!exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (is.null(params$file)) { + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) + } } query_params <- list() query_params$agencyids <- paste(agencyid, collapse = ",") @@ -44,15 +42,13 @@ read_database <- function(id, include_series = FALSE, tidy = FALSE, ...) { response <- GET(env$repository$url, path = c(env$repository$path, "/datasets"), query = query_params, - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") } @@ -77,17 +73,15 @@ read_database <- function(id, include_series = FALSE, tidy = FALSE, ...) { data_set_ref, "series", sep = "/"), query = query_params, - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code == 200) { message("Processing data set: ", data_set_ref, "\n") } else { - stop(content(response, type = "application/json", encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") tmp_data_set <- data_message[[2]][["data-sets"]][[1]][[2]] } else { tmp_data_set <- raw_data_set[[2]] diff --git a/R/read_dataset.R b/R/read_dataset.R index 3316ceb..3ffca8f 100644 --- a/R/read_dataset.R +++ b/R/read_dataset.R @@ -4,10 +4,8 @@ read_dataset <- function(id, tidy = FALSE, ...) { # Parameters ---- params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL + if (is.null(params$debug)) { + params$debug <- FALSE } if (!is.null(params$agencyid)) { agencyid <- params$agencyid @@ -19,12 +17,7 @@ read_dataset <- function(id, tidy = FALSE, ...) { } else { version <- "latest" } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Fetch data set(s) ---- @@ -34,8 +27,14 @@ read_dataset <- function(id, tidy = FALSE, ...) { data_message <- fromJSON(params$file, simplifyVector = FALSE) message("Data set(s) successfully retrieved from local storage.\n") } else { - if (!exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) } query_params <- list() query_params$agencyids <- paste(agencyid, collapse = ",") @@ -44,15 +43,16 @@ read_dataset <- function(id, tidy = FALSE, ...) { response <- GET(env$repository$url, path = c(env$repository$path, "/datasets"), query = query_params, - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) + if (params$debug == TRUE) { + message("Request URL: ", response$request$url, "\n") + } if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") } @@ -67,11 +67,11 @@ read_dataset <- function(id, tidy = FALSE, ...) { raw_data_set[[2]]$version, sep = "-") query_params <- list() - query_params$release <- get_release(env, data_set_ref, params$release) + query_params$release <- get_release(env, data_set_ref, params$release, params$debug) if (!is.null(params$series_key)) { query_params[["series-key"]] <- params$series_key } - tmp_data_set <- get_data(env, data_set_ref, query_params) + tmp_data_set <- get_data(env, data_set_ref, query_params, debug = params$debug) } series_names <- sapply(tmp_data_set$series, function(raw_series) { return(raw_series[["series-key"]]) @@ -128,28 +128,35 @@ read_econdata <- function(id, tidy = FALSE, ...) { read_dataset(id = id, tidy = tidy, ...) } -get_release <- function(env, ref, candidate_release) { +get_release <- function(env, ref, candidate_release, debug = FALSE) { if (is.null(candidate_release)) { candidate_release <- "latest" } if (candidate_release != "unreleased") { final_release <- tryCatch({ - strftime(candidate_release, "%Y-%m-%dT%H:%M:%S") + if (grepl("^\\d{4}-\\d{1,2}-\\d{1,2}(T\\d{1,2}:\\d{1,2}:\\d{1,2})?$", + candidate_release, + perl = TRUE)) { + strftime(candidate_release, "%Y-%m-%dT%H:%M:%S") + } else { + stop("Unacceptable proposed time/date format") + } }, error = function(e) { response <- GET(env$repository$url, path = paste(env$repository$path, "datasets", ref, "release", sep = "/"), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept_json()) + if (debug == TRUE) { + message("Request URL: ", response$request$url, "\n") + } if (response$status_code != 200) { - stop(content(response, type = "application/json", encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") if (length(data_message$releases) != 0) { if (candidate_release == "latest") { release <- head(data_message$releases, n = 1)[[1]]$release |> @@ -191,30 +198,31 @@ get_release <- function(env, ref, candidate_release) { return(final_release) } -get_data <- function(env, ref, params, links = NULL, data_set = NULL) { +get_data <- function(env, ref, params, links = NULL, data_set = NULL, debug = FALSE) { if (is.null(links)) { response <- GET(env$repository$url, path = paste(env$repository$path, "datasets", ref, sep = "/"), query = params, - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) + if (debug == TRUE) { + message("Request URL: ", response$request$url, "\n") + } if (response$status_code == 200) { message("Processing data set: ", ref, "\n") } else { - stop(content(response, type = "application/json", encoding = "UTF-8")) + stop(content(response, type = "application/json")) } links <- unlist(strsplit(response$headers$link, ",")) - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") data_set <- data_message[[2]][["data-sets"]][[1]][[2]] if (!any(grepl("rel=next", links))) { return(data_set) } else { - return(get_data(env, ref, params, links, data_set)) + return(get_data(env, ref, params, links, data_set, debug)) } } else { link_next <- links[grepl("rel=next", links)] @@ -233,21 +241,22 @@ get_data <- function(env, ref, params, links = NULL, data_set = NULL) { return(y) }) |> unlist(recursive = FALSE), - set_cookies(.cookies = get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) + if (debug == TRUE) { + message("Request URL: ", response$request$url, "\n") + } if (response$status_code != 200) { - stop(content(response, type = "application/json", encoding = "UTF-8")) + stop(content(response, type = "application/json")) } links <- unlist(strsplit(response$headers$link, ",")) - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") data_set$series <- c(data_set$series, data_message[[2]][["data-sets"]][[1]][[2]]$series) if (!any(grepl("rel=next", links))) { return(data_set) } else { - return(get_data(env, ref, params, links, data_set)) + return(get_data(env, ref, params, links, data_set, debug)) } } } diff --git a/R/read_registry.R b/R/read_registry.R index 965998c..6ff9065 100644 --- a/R/read_registry.R +++ b/R/read_registry.R @@ -7,11 +7,6 @@ read_registry <- function(structure, tidy = FALSE, ...) { if (is.null(params$id) && is.null(params$file)) { stop("At least one of either: 'id' or 'file' parameter required.") } - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } if (!is.null(params$agencyid)) { agencyid <- params$agencyid } else { @@ -27,19 +22,22 @@ read_registry <- function(structure, tidy = FALSE, ...) { } else { version <- "latest" } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) params$env <- env # Fetch structure(s) ---- - if (!exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (is.null(params$file)) { + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) + } } agencyids <- paste(agencyid, collapse = ",") ids <- paste(id, collapse = ",") @@ -101,15 +99,13 @@ read_category_schemes <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") category_schemes <- data_message[[2]][["structures"]][["category-schemes"]] return(category_schemes) } else { @@ -187,16 +183,14 @@ read_codelists <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") codelists <- data_message[[2]][["structures"]][["codelists"]] return(codelists) } else { @@ -268,15 +262,13 @@ read_concept_schemes <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") concept_schemes <- data_message[[2]][["structures"]][["concept-schemes"]] return(concept_schemes) } else { @@ -361,16 +353,14 @@ read_dataflow <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") dataflows <- data_message[[2]][["structures"]][["dataflows"]] return(dataflows) } else { @@ -434,15 +424,13 @@ read_data_structures <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") data_structures <- data_message[[2]][["structures"]][["data-structures"]] return(data_structures) } else { @@ -610,15 +598,13 @@ read_memberlist <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - data_message <- content(response, - type = "application/json", - encoding = "UTF-8") + data_message <- content(response, type = "application/json") memberlists <- data_message[[2]][["structures"]][["memberlists"]] return(memberlists) } else { @@ -708,14 +694,14 @@ read_cons_agreement <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } data_message <- - content(response, type = "application/json", encoding = "UTF-8") + content(response, type = "application/json") cons_agreements <- data_message[[2]][["structures"]][["consumption-agreements"]] return(cons_agreements) @@ -793,14 +779,14 @@ read_prov_agreement <- function(agencyids, ids, versions, params) { query = list(agencyids = agencyids, ids = ids, versions = versions), - set_cookies(.cookies = get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code != 200) { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } data_message <- - content(response, type = "application/json", encoding = "UTF-8") + content(response, type = "application/json") prov_agreements <- data_message[[2]][["structures"]][["provision-agreements"]] return(prov_agreements) diff --git a/R/read_release.R b/R/read_release.R index 5689cf7..caeadec 100644 --- a/R/read_release.R +++ b/R/read_release.R @@ -4,11 +4,6 @@ read_release <- function(id, tidy = FALSE, ...) { # Parameters ---- params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } if (!is.null(params$agencyid)) { agencyid <- params$agencyid } else { @@ -29,30 +24,31 @@ read_release <- function(id, tidy = FALSE, ...) { if (!is.null(params$description)) { query_params$description <- params$description } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Fetch release ---- - if (!exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) } response <- GET(env$repository$url, path = c(env$repository$path, "/datasets"), query = list(agencyids = paste(agencyid, collapse = ","), ids = paste(id, collapse = ","), versions = paste(version, collapse = ",")), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept_json()) if (response$status_code != 200) - stop(content(response, encoding = "UTF-8")) - data_message <- content(response, encoding = "UTF-8") + stop(content(response, type = "application/json")) + data_message <- content(response, type = "application/json") releases <- lapply(data_message[["data-sets"]], function(dataset) { dataset_ref <- paste(dataset$agencyid, dataset$id, @@ -62,15 +58,15 @@ read_release <- function(id, tidy = FALSE, ...) { "datasets", dataset_ref, "release", sep = "/"), query = query_params, - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept("application/vnd.sdmx-codera.data+json")) if (response$status_code == 200) { message("Fetching releases for: ", dataset_ref, "\n") } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } - release <- content(response, type = "application/json", encoding = "UTF-8") + release <- content(response, type = "application/json") release$releases <- lapply(release$releases, function(r) { list("release" = strptime(r$release, "%Y-%m-%dT%H:%M:%S%z"), "start-period" = strptime(r[["start-period"]], "%Y-%m-%d"), diff --git a/R/tidy_data.R b/R/tidy_data.R index ca61d75..2c7d2b6 100644 --- a/R/tidy_data.R +++ b/R/tidy_data.R @@ -38,7 +38,7 @@ tidy_wide <- function(x, codelabel = FALSE, prettymeta = TRUE, ...) { tidy_wide, codelabel, prettymeta)) - metadata <- if (prettymeta) get_metadata(x, ...) else NULL + metadata <- if (prettymeta) get_metadata(x) else NULL d <- unlist2d(x, "series_key", row.names = "time_period", DT = TRUE) |> dcast(time_period ~ series_key, value.var = "OBS_VALUE") |> fmutate(time_period = as.Date(time_period)) @@ -86,7 +86,7 @@ tidy_long <- function(x, combine = FALSE, allmeta = FALSE, origmeta = FALSE, pre prettymeta) return(if (combine) rbindlist(res, use.names = TRUE, fill = TRUE) else res) } - metadata <- if (prettymeta) get_metadata(x, ...) else NULL + metadata <- if (prettymeta) get_metadata(x) else NULL d <- unlist2d(x, "series_key", row.names = "time_period", DT = TRUE) |> fmutate(time_period = as.Date(time_period), series_key = qF(series_key)) |> @@ -122,11 +122,20 @@ tidy_long <- function(x, combine = FALSE, allmeta = FALSE, origmeta = FALSE, pre meta$data_provider_ref <- rep(metadata$data_provider_ref, nseries) meta$series_key <- names(x)[names(x) %in% levels(d$series_key)] } - setcolorder(meta, - c("data_set_name", - "data_set_ref", - "data_provider_ref", - "series_key")) + if (prettymeta) { + setcolorder(meta, + c("data_set_name", + "data_set_ref", + "data_provider_ref", + "series_key")) + } else { + setcolorder(meta, + c("data_set_name", + "data_set_ref", + "series_key")) + meta[["name"]] <- NULL + meta[["provision-agreement"]] <- NULL + } if (!allmeta) get_vars(meta, fnobs(meta) == 0L) <- NULL if (combine) { meta_fct <- dapply(meta, qF, drop = FALSE) # Factors for efficient storage diff --git a/R/write_database.R b/R/write_database.R index ddc9ad3..075cbc4 100644 --- a/R/write_database.R +++ b/R/write_database.R @@ -4,25 +4,23 @@ write_database <- function(x, method = "update", ...) { # Parameters ---- params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } stopifnot(length(method) == 1) stopifnot(method %in% c("create", "update")) - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Push data message ---- - if (is.null(params$file) && !exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (is.null(params$file)) { + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) + } } header <- list() header$id <- unbox("ECONDATAR") @@ -68,14 +66,14 @@ write_database <- function(x, method = "update", ...) { "datasets", sep = "/"), body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - error <- content(response, encoding = "UTF-8") + error <- content(response, type = "application/json") if (response$status_code == 400) { if (error$message == "Validation error") { stop(toJSON(error, pretty = TRUE)) @@ -94,14 +92,14 @@ write_database <- function(x, method = "update", ...) { dataset_ref, sep = "/"), body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - error <- content(response, encoding = "UTF-8") + error <- content(response, type = "application/json") if (response$status_code == 400) { if (error$message == "Validation error") { stop(toJSON(error, pretty = TRUE)) diff --git a/R/write_dataset.R b/R/write_dataset.R index d3f6905..5a050a6 100644 --- a/R/write_dataset.R +++ b/R/write_dataset.R @@ -5,25 +5,23 @@ write_dataset <- function(x, method = "stage", ...) { env <- fromJSON(system.file("settings.json", package = "econdatar")) params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } stopifnot(length(method) == 1) stopifnot(method %in% c("stage", "validate")) - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Push data message ---- - if (is.null(params$file) && !exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (is.null(params$file)) { + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) + } } header <- list() header$id <- unbox("ECONDATAR") @@ -66,14 +64,14 @@ write_dataset <- function(x, method = "stage", ...) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - error <- content(response, encoding = "UTF-8") + error <- content(response, type = "application/json") if (response$status_code == 400) { if (error$message == "Validation error") { stop(toJSON(error, pretty = TRUE)) @@ -94,14 +92,14 @@ write_dataset <- function(x, method = "stage", ...) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - error <- content(response, encoding = "UTF-8") + error <- content(response, type = "application/json") if (response$status_code == 400) { if (error$message == "Validation error") { stop(toJSON(error, pretty = TRUE)) diff --git a/R/write_registry.R b/R/write_registry.R index ec4122f..eb22685 100644 --- a/R/write_registry.R +++ b/R/write_registry.R @@ -4,26 +4,24 @@ write_registry <- function(structure, x, method = "update", ...) { # Parameters ---- params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } stopifnot(length(method) == 1) stopifnot(method %in% c("create", "update")) - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) params$env <- env # Fetch structure(s) ---- - if (!exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (is.null(params$file)) { + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) + } } header <- list() if (is.null(params$file)) { @@ -107,14 +105,14 @@ write_category_scheme <- function(category_scheme, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating category scheme: ", category_scheme_ref, "\n") @@ -125,14 +123,14 @@ write_category_scheme <- function(category_scheme, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") @@ -196,14 +194,14 @@ write_codelist <- function(codelist, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating codelist: ", codelist_ref, "\n") @@ -214,14 +212,14 @@ write_codelist <- function(codelist, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") @@ -289,14 +287,14 @@ write_concept_scheme <- function(concept_scheme, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating concept scheme: ", concept_scheme_ref, "\n") @@ -307,14 +305,14 @@ write_concept_scheme <- function(concept_scheme, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") @@ -369,14 +367,14 @@ write_dataflow <- function(dataflow, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating dataflow: ", dataflow_ref, "\n") @@ -386,14 +384,14 @@ write_dataflow <- function(dataflow, method, params) { dataflow_ref, sep = "/"), body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") @@ -556,14 +554,14 @@ write_data_structure <- function(data_structure, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating data structure: ", data_structure_ref, "\n") @@ -574,14 +572,14 @@ write_data_structure <- function(data_structure, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") @@ -682,14 +680,14 @@ write_memberlist <- function(memberlist, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating memberlist: ", memberlist_ref, "\n") @@ -700,14 +698,14 @@ write_memberlist <- function(memberlist, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } } else { @@ -766,14 +764,14 @@ write_cons_agreement <- function(cons_agreement, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating consumption agreement: ", cons_agreement_ref, "\n") @@ -784,14 +782,14 @@ write_cons_agreement <- function(cons_agreement, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") @@ -860,14 +858,14 @@ write_prov_agreement <- function(prov_agreement, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 201) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "update") { message("Updating provision agreement: ", prov_agreement_ref, "\n") @@ -878,14 +876,14 @@ write_prov_agreement <- function(prov_agreement, method, params) { body = toJSON(data_message, na = "null", always_decimal = TRUE), - set_cookies(.cookies = get("econdata_session", - envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), content_type("application/vnd.sdmx-codera.data+json"), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") diff --git a/R/write_release.R b/R/write_release.R index b995548..0b23a58 100644 --- a/R/write_release.R +++ b/R/write_release.R @@ -4,11 +4,6 @@ write_release <- function(id, version, providerid, description, method = "releas # Parameters ---- params <- list(...) - if (!is.null(params$username) && !is.null(params$password)) { - credentials <- paste(params$username, params$password, sep = ";") - } else { - credentials <- NULL - } if (!is.null(params$agencyid)) { agencyid <- params$agencyid } else { @@ -23,20 +18,21 @@ write_release <- function(id, version, providerid, description, method = "releas "%Y-%m-%dT%H:%M:%S", tz = "Africa/Johannesburg") } - if (!is.null(params$portal)) { - portal <- params$portal - } else { - portal <- "econdata" - } stopifnot(length(method) == 1) stopifnot(method %in% c("release", "reset", "rollback")) - env <- fromJSON(system.file("settings.json", package = "econdatar"))[[portal]] + env <- fromJSON(system.file("settings.json", package = "econdatar")) # Commit data set release ---- - if (!exists("econdata_session", envir = .pkgenv)) { - login_helper(credentials, env$repository$url) + if (exists("econdata_token", envir = .pkgenv)) { + token <- unlist(strsplit(get("econdata_token", envir = .pkgenv), " "))[2] + payload <- jwt_split(token)$payload + if (Sys.time() > as.POSIXct(payload$exp, origin="1970-01-01")) { + login_helper(env$auth) + } + } else { + login_helper(env$auth) } dataset_ref <- paste(agencyid, id, version, sep = "-") if (method == "release") { @@ -47,13 +43,13 @@ write_release <- function(id, version, providerid, description, method = "releas dataset_ref, "commit", sep = "/"), query = query_params, - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "reset") { message("Resetting release: ", dataset_ref, "\n") @@ -62,13 +58,13 @@ write_release <- function(id, version, providerid, description, method = "releas "datasets", dataset_ref, "reset", sep = "/"), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else if (method == "rollback") { message("Rolling back release: ", dataset_ref, "\n") @@ -77,13 +73,13 @@ write_release <- function(id, version, providerid, description, method = "releas "datasets", dataset_ref, "rollback", sep = "/"), - set_cookies(.cookies = - get("econdata_session", envir = .pkgenv)), + add_headers(authorization = get("econdata_token", + envir = .pkgenv)), accept_json()) if (response$status_code == 200) { - message(content(response, encoding = "UTF-8")$success) + message(content(response, type = "application/json")$success) } else { - stop(content(response, encoding = "UTF-8")) + stop(content(response, type = "application/json")) } } else { stop("Method not implemented.") diff --git a/inst/installEconDataR.ps1 b/inst/installEconDataR.ps1 deleted file mode 100644 index 2dfe2ac..0000000 --- a/inst/installEconDataR.ps1 +++ /dev/null @@ -1,82 +0,0 @@ -Write-Output "Checking R installation" -Write-Output "" - -If (Test-Path -Path "C:\Program Files\R") { - Write-Output "Found R installation." - Write-Output "" -} Else { - Write-Output "No R installation found." - Write-Output "Please download and install R from www.r-project.org before continuing." - Write-Output "" - Write-Output "Press any key to exit..." - - [Console]::ReadKey() - - Exit -} - -Write-Output "Downloading package from GitHub.com" -Write-Output "" - -$zipFilePath = $home + "\Documents\econdatar.zip" - -$WebClient = New-Object System.Net.WebClient -$WebClient.DownloadFile("https://github.com/coderaanalytics/econdatar/archive/refs/heads/master.zip", $zipFilePath) - -Write-Output "Unzipping source code" -Write-Output "" - -$unzipDir = $home + "\Documents" - -Expand-Archive $zipFilePath -DestinationPath $unzipDir - -$packageFolder0 = $unzipDir + "\econdatar-master" -$packageFolder1 = $unzipDir + "\econdatar" - -Rename-Item $packageFolder0 $packageFolder1 - -Write-Output "Setting up directories..." -Write-Output "" - -$rDir = @(Get-ChildItem -Path "C:\Program Files\R" | Sort-Object -Descending) -$rVersion = [regex]::match($rDir[0].name,"\d\.\d").Groups[0].Value -$libPath0 = $home + "\Documents\R\win-library\" + $rVersion -$libPath1 = $libPath0.replace("\","/") -$rProfilePath = $unzipDir + "\.Rprofile" - -If (Test-Path -Path $libPath0) { - @("R_LIBS_USER=""" + $libPath1 + """") | Out-File -FilePath $rProfilePath -Append -Encoding ascii - "options(repos=structure(c(CRAN='https://cran.mirror.ac.za')))" | Out-File -FilePath $rProfilePath -Append -Encoding ascii -} Else { - New-Item -Path $libPath0 -ItemType Directory - @(".libPaths('" + $libPath1 + "')") | Out-File -FilePath $rProfilePath -Append -Encoding ascii - "options(repos=structure(c(CRAN='https://cran.mirror.ac.za')))" | Out-File -FilePath $rProfilePath -Append -Encoding ascii -} - -Write-Output "" -Write-Output "Installing EconDataR package..." -Write-Output "" - -$rBinDir = $rDir[0].Fullname + "\bin\Rscript.exe" -$rscriptDir = @(Get-Item -Path $rBinDir) -$packageFolder2 = $packageFolder1.replace("\","/") - -$installCommand = "install.packages('" + $packageFolder2 + "',repos=NULL,type='source')" - -Start-Process $rscriptDir[0].Fullname -ArgumentList "-e", "install.packages('httr')" -NoNewWindow -Wait -Start-Process $rscriptDir[0].Fullname -ArgumentList "-e", "install.packages('jsonline')" -NoNewWindow -Wait -Start-Process $rscriptDir[0].Fullname -ArgumentList "-e", "install.packages('xml2')" -NoNewWindow -Wait -Start-Process $rscriptDir[0].Fullname -ArgumentList "-e", $installCommand -NoNewWindow -Wait - -Write-Output "" -Write-Output "Cleaning up" -Write-Output "" - -Remove-Item $zipFilePath -Remove-Item -Recurse -Force $packageFolder1 - -Write-Output "Done!" -Write-Output "" -Write-Output "Press any key to exit..." - -[Console]::ReadKey() diff --git a/inst/settings.json b/inst/settings.json index e5737db..830b937 100644 --- a/inst/settings.json +++ b/inst/settings.json @@ -1,22 +1,14 @@ { - "econdata" : { - "repository" : { - "url" : "https://www.econdata.co.za", - "path" : "sdmx-codera/v1" - }, - "registry" : { - "url" : "https://www.econdata.co.za", - "path" : "sdmx-codera/v1" - } + "auth" : { + "url" : "https://codera-firstrand.auth.af-south-1.amazoncognito.com", + "path" : "oauth2/token" }, - "firstrand" : { - "repository" : { - "url" : "https://firstrand.econdata.co.za", - "path" : "sdmx-codera/v1" - }, - "registry" : { - "url" : "https://firstrand.econdata.co.za", - "path" : "sdmx-codera/v1" - } + "repository" : { + "url" : "https://firstrand.econdata.co.za", + "path" : "sdmx-codera/v1" + }, + "registry" : { + "url" : "https://firstrand.econdata.co.za", + "path" : "sdmx-codera/v1" } }