Skip to content

Commit

Permalink
make bulk functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
datapumpernickel committed Feb 14, 2024
1 parent cc73975 commit 87eaf35
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
20 changes: 15 additions & 5 deletions R/ct_check_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,23 @@ rlang::abort("Please provide a start and end date for the period of interest.")
if (sd_year && ed_year) {
# If start_date and end_date are both years ("yyyy") and are identical,
# return the single year as the date range.
if (identical(start_date, end_date)) {
start_date <- convert_to_date(start_date)
date_range <- seq.Date(start_date, by = "month", length.out = 12) |>
format(format = "%Y%m")
start_date <- convert_to_date(start_date)
end_date <- convert_to_date(end_date)
if (!bulk) {

if (identical(start_date, end_date)) {
date_range <-
seq.Date(start_date, by = "month", length.out = 12) |>
format(format = "%Y%m")
} else {
rlang::abort("Cannot get more than a single year's worth of monthly data in a single query.") # nolint
}
} else {
rlang::abort("Cannot get more than a single year's worth of monthly data in a single query.") # nolint
date_range <-
seq.Date(from = start_date, to = end_date, by = "month") |>
format(format = "%Y%m")
}

} else if (!sd_year && !ed_year) {
# If neither start_date nor end_date are years, get date range by month.
start_date <- convert_to_date(start_date)
Expand Down
24 changes: 17 additions & 7 deletions R/ct_get_bulk.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ ct_get_bulk <- function(type = "goods",
update = FALSE,
requests_per_second = 10 / 60,
extra_params = NULL,
cache = FALSE) {
cache = FALSE,
download_files = TRUE) {

bulk <- TRUE
## compile codes
Expand Down Expand Up @@ -123,13 +124,22 @@ ct_get_bulk <- function(type = "goods",
reporter_code <- parsed_response |>
poorman::pull(reporterCode)

reqs <- purrr::pmap(list(reporter_code,filehash,
primary_token),
ct_build_bulk_file_request, verbose = verbose)
if(download_files){


reqs <- purrr::pmap(list(reporter_code,
file_hash,
primary_token),
ct_build_bulk_file_request, verbose = verbose)

resps <- purrr::map(reqs,~ct_perform_request(.x,
requests_per_second = 60/10,
verbose = verbose))

} else {
return(parsed_response)
}

resps <- purrr::map(reqs,~ct_perform_request(.x,
requests_per_second = 60/10,
verbose = T))



Expand Down
4 changes: 2 additions & 2 deletions R/ct_perform_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ comtrade_error_body <- function(resp) {
} else if (stringr::str_detect(
body,
"The resource you are looking for has been removed"
)) { # nolint
)) {
message <-
c(
"The original message is: ",
body,
"But most likely you have exceeded the character value for the api."
"But most likely you have exceeded the character value for the api." # nolint
)
return(message)
}
Expand Down
2 changes: 2 additions & 0 deletions R/ct_process_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ ct_process_response <-

processed <- readr::read_delim(file.path(rappdirs::user_cache_dir("comtradr_bulk"),
filename), delim = "\t")
file.remove(file.path(rappdirs::user_cache_dir("comtradr_bulk"),
filename))
} else {
result <- resp |>
httr2::resp_body_json(simplifyVector = TRUE)
Expand Down

0 comments on commit 87eaf35

Please sign in to comment.