Skip to content

Commit

Permalink
Merge pull request #74 from ropensci/dev
Browse files Browse the repository at this point in the history
merge changes from review into main
  • Loading branch information
datapumpernickel authored Dec 22, 2023
2 parents c44b45c + bbf6a83 commit 4525b8d
Show file tree
Hide file tree
Showing 60 changed files with 2,374 additions and 814 deletions.
17 changes: 13 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ Authors@R: c(
role = "rev",
comment = c(ORCID = "0000-0002-3915-0618",
"Alicia reviewed the package for rOpenSci,
see https://github.com/ropensci/onboarding/issues/141")),
see https://github.com/ropensci/onboarding/issues/141")),
person("Rafael", "Hellwig",
role = "rev",
comment = c(ORCID = "0000-0002-3092-3493",
"Rafael reviewed the package for rOpenSci,
see https://github.com/ropensci/onboarding/issues/141")),
see https://github.com/ropensci/onboarding/issues/141")),
person("Ernest", "Guevarra",
role = "rev",
comment = c(ORCID = "0000-0002-4887-4415",
"Ernest reviewed the package for rOpenSci,
see https://github.com/ropensci/software-review/issues/613")),
person("Nicholas", "Potter",
role = "rev",
comment = c(ORCID = "0000-0002-3410-3732",
"Nicholas reviewed the package for rOpenSci,
see https://github.com/ropensci/software-review/issues/613")),
person("Juergen", "Amann", role=c("ctb")))
Description: Interface with and extract data from the United Nations Comtrade
API <https://comtradeplus.un.org/>. Comtrade provides country level shipping
Expand All @@ -49,8 +59,7 @@ Suggests:
knitr,
rmarkdown,
spelling,
testthat (>= 3.0.0),
vcr
testthat (>= 3.0.0)
License: GPL-3
Encoding: UTF-8
LazyData: true
Expand Down
59 changes: 34 additions & 25 deletions R/ct_build_request.R
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
#' Build a valid request object from the checked parameters
#'
#' This is an internal function takes the necessary parameters from `ct_check_params()`
#' and creates a httr2 request to be performed. This request can then be used in a second function, `ct_perform_request()` to actually return the data.
#' It is called internally ct `ct_get_data()`
#' This is an internal function takes the necessary parameters
#' from `ct_check_params()` and creates a httr2 request to be performed.
#' This request can then be used in a second function, `ct_perform_request()`
#' to actually return the data. It is called internally ct `ct_get_data()`
#'
#' @param params a named vector of parameters for the comtrade request, result from `ct_check_params()`.
#' @param params a named vector of parameters for the comtrade request,
#' result from `ct_check_params()`.
#'
#' @examplesIf interactive()
#' # Build request from checked parameters
#' comtradr:::ct_build_request(comtradr:::ct_check_params(type = 'goods',
#' frequency = 'A',
#' commodity_classification = 'HS',
#' commodity_code = 'TOTAL',
#' flow_direction = 'all',
#' reporter = 'all',
#' partner = 'World',
#' start_date = 2020,
#' end_date = 2022,
#' process = TRUE,
#' tidy_cols = TRUE,
#' verbose = FALSE,
#' primary_token = 'xxxx',
#' mode_of_transport = '0',
#' partner_2 = 'World',
#' customs_code ='C00',
#' update = FALSE ))
#' comtradr:::ct_build_request(comtradr:::ct_check_params(
#' type = "goods",
#' frequency = "A",
#' commodity_classification = "HS",
#' commodity_code = "TOTAL",
#' flow_direction = "all",
#' reporter = "all",
#' partner = "World",
#' start_date = 2020,
#' end_date = 2022,
#' process = TRUE,
#' tidy_cols = TRUE,
#' verbose = FALSE,
#' primary_token = "xxxx",
#' mode_of_transport = "0",
#' partner_2 = "World",
#' customs_code = "C00",
#' update = FALSE
#' ))
#'
#' @returns a httr2 request object
#' @inheritParams ct_get_data
ct_build_request <- function(params,
primary_token = NULL,
verbose = FALSE) {
primary_token = NULL,
verbose = FALSE) {
query_params <- params$query_params

extra_params <- params$extra_params |>
purrr::map(unlist) |>
purrr::pluck(1) |>
as.list()
query_params <- c(query_params, extra_params)
type <- params$url_params$type

freq <- params$url_params$freq
Expand All @@ -47,12 +56,12 @@ ct_build_request <- function(params,
httr2::req_headers(`Ocp-Apim-Subscription-Key` = primary_token) |>
httr2::req_url_query(!!!query_params)

if(stringr::str_length(res$url)>4095){
if (stringr::str_length(res$url) > 4095) {
rlang::abort("Your request exceeds 4KB or 4096 characters, which is the upper limit of the Comtrade API.") # nolint
}

if (verbose) {
cli::cli_inform(c("i" = paste0("URL that will be queried: ",res$url)))
cli::cli_inform(c("i" = paste0("URL that will be queried: ", res$url)))
}

return(res)
Expand Down
Loading

0 comments on commit 4525b8d

Please sign in to comment.