Skip to content

Commit

Permalink
documentation and test update
Browse files Browse the repository at this point in the history
  • Loading branch information
datapumpernickel committed May 9, 2024
1 parent 8a8192a commit 19c4a29
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 1 deletion.
3 changes: 2 additions & 1 deletion man/ct_get_bulk.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
structure(list(method = "GET", url = "https://comtradeapi.un.org/bulk/v1/file/32/b07f98ee4cfb477c5b86923bbf159d48ca519ccb35c35865b3578b075fc918fb?format=json",
status_code = 200L, headers = structure(list(`Content-Length` = "187742",
`Content-Type` = "application/TSV", `Request-Context` = "appId=cid-v1:9b6e1d5a-3728-46ff-b743-6d33d23e54a6",
`Content-Disposition` = "attachment; filename=\"C/A/32/C_A_32_196201_S1_O.txt.gz\"; size=187742",
`x-frame-options` = "deny", `X-Content-Type-Options` = "nosniff",
`X-XSS-Protection` = "1;mode=block", `strict-transport-security` = "max-age=31536000;includeSubDomains",
`content-security-policy` = "frame-src 'self'", `x-permitted-cross-domain-policies` = "none",
`Referrer-Policy` = "no-referrer-when-downgrade", `Permissions-Policy` = "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()",
Date = "Thu, 09 May 2024 22:26:42 GMT"), class = "httr2_headers"),
body = as.raw(c(0x1f, 0x8b, 0x08, 0x08, 0x56, 0xd3, 0x22,
0x5f, 0x02, 0xff, 0x43, 0x5f, 0x41, 0x5f, 0x33, 0x32, 0x5f,
0x31, 0x39, 0x36, 0x32, 0x30, 0x31, 0x5f, 0x53, 0x31, 0x2e,
0x74, 0x78, 0x74, 0x00, 0xcc, 0xbd, 0x59, 0xaf, 0x26, 0xc9,
0x91, 0x1d, 0xf8, 0x9c, 0x03, 0xcc, 0x4f, 0x99, 0x86, 0xef,
0xcb, 0x23, 0x45, 0x4c, 0x0f, 0x04, 0xb4, 0xc4, 0xd1, 0x88,
0x33, 0x92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x1d, 0x8f, 0x3f, 0x86, 0x64, 0xb2, 0x3d, 0x90, 0x2c, 0x91,
0x04, 0xe5, 0x6d, 0x90, 0x9c, 0x3c, 0x88, 0xfb, 0xac, 0x5a,
0x93, 0xc3, 0x34, 0x1c, 0xbf, 0xe9, 0xe3, 0xd7, 0xc3, 0x6b,
0x6f, 0xb5, 0x99, 0x73, 0x8f, 0x55, 0xaa, 0x4e, 0x3d, 0xed,
0x27, 0xcb, 0xbe, 0x11, 0xf6, 0x6d, 0xf0, 0xaa, 0xcf, 0x29,
0x0d, 0x80, 0x2b, 0xf7, 0x5f, 0xcd, 0xf9, 0xd7, 0xea, 0xd8,
0x66, 0xbf, 0x21, 0xb0, 0x30, 0x48, 0x08, 0x31, 0xa5, 0x60,
0x25, 0x04, 0xab, 0xc2, 0xba, 0x09, 0x36, 0xa8, 0x0e, 0xa1,
0x0b, 0x24, 0x08, 0x41, 0xde, 0x06, 0xed, 0x73, 0xb0, 0x30,
0x03, 0x9b, 0x18, 0x55, 0x9e, 0x0f, 0x22, 0xe6, 0x36, 0x13,
0x0c, 0x81, 0x85, 0x41, 0x0b, 0x0c, 0x65, 0x89, 0xf3, 0x59,
0xde, 0x06, 0x15, 0xd2, 0x9a, 0x00)), request = structure(list(
url = "https://comtradeapi.un.org/bulk/v1/file/32/b07f98ee4cfb477c5b86923bbf159d48ca519ccb35c35865b3578b075fc918fb?format=json",
method = NULL, headers = structure(list(`Ocp-Apim-Subscription-Key` = "1556dc28c565437e92119755dd47eb86"), redact = character(0)),
body = NULL, fields = list(), options = list(), policies = list(
error_body = function (resp)
{
if (!is.null(httr2::resp_header(resp, "Content-Type"))) {
if (stringr::str_detect(httr2::resp_header(resp,
"Content-Type"), "json")) {
body <- httr2::resp_body_json(resp, simplifyVector = TRUE)
message <- body$errorObject$errorMessage
if (!is.null(message)) {
message <- c(message)
}
return(message)
}
else if (stringr::str_detect(httr2::resp_header(resp,
"Content-Type"), "text")) {
body <- httr2::resp_body_string(resp)
if (stringr::str_detect(body, "Request URL Too Long")) {
message <- c("You might have provided too many parameters and the URL got too long.")
return(message)
}
else if (stringr::str_detect(body, "The resource you are looking for has been removed")) {
message <- c("The original message is: ",
body, "But most likely you have exceeded the character value for the api.")
return(message)
}
}
}
}, throttle_delay = function (req)
{
realm <- realm %||% url_parse(req$url)$hostname
last <- the$throttle[[realm]]
if (is.null(last)) {
wait <- 0
}
else {
wait <- delay - (unix_time() - last)
}
sys_sleep(wait, "for throttling delay")
throttle_touch(realm)
wait
}, retry_max_tries = 2, retry_is_transient = function (resp)
{
(httr2::resp_status(resp) == 403 && httr2::resp_header(resp,
"Retry-After") != "0") || (httr2::resp_status(resp) ==
429 && httr2::resp_header(resp, "Retry-After") !=
"0")
}, retry_after = function (resp)
{
time <- as.numeric(httr2::resp_header(resp, "Retry-After"))
time
})), class = "httr2_request"), cache = new.env(parent = emptyenv())), class = "httr2_response")
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"elapsedTime": "0.1 secs",
"count": 1,
"data": [
{
"fileUrl": "https://comtradeapi.un.org/bulk/v1/file/32/b07f98ee4cfb477c5b86923bbf159d48ca519ccb35c35865b3578b075fc918fb",
"fileSize": "183.34 KB",
"isOriginalClassification": true,
"reporterCode": 32,
"freqCode": "A",
"typeCode": "C",
"classificationCode": "S1",
"refPeriodId": 196201,
"period": 1962,
"publicationDate": "2003-04-29T11:24:10Z",
"partitionKey": "32",
"rowKey": "b07f98ee4cfb477c5b86923bbf159d48ca519ccb35c35865b3578b075fc918fb",
"timestamp": "2024-03-10T04:54:11.8900761+00:00",
"eTag": "W/\"datetime'2024-03-10T04%3A54%3A11.8900761Z'\""
}
],
"error": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"elapsedTime": "0.09 secs",
"count": 0,
"data": [

],
"error": ""
}
55 changes: 55 additions & 0 deletions tests/testthat/test-ct_get_bulk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

httptest2::with_mock_dir("goods_bulk_error", {
test_that("test that error works for params that have no values", {
expect_error(
ct_get_bulk(
reporter = 'ARG',
start_date = '1920',
end_date = "1920"
),
"Probably no data for this combination of parameters"
)
})
})

without_internet({
test_that('URL query is correctly constructed with goods',
{
expect_GET(
comtradr::ct_get_bulk(reporter = 'ARG',
start_date = '1962',
end_date = "1962",
verbose = T)
,'https://comtradeapi.un.org/bulk/v1/get/C/A/HS?reporterCode=32&period=1962') # nolint
})
})

httptest2::with_mock_dir("goods_bulk",{
test_that("We can get goods data", {
expect_s3_class(comtradr::ct_get_bulk(reporter = 'ARG',
start_date = '1962',
end_date = "1962",
verbose = T), 'data.frame')
})
})

httptest2::with_mock_dir("goods_bulk", {
test_that("test informative messages", {
expect_message(comtradr::ct_get_bulk(reporter = 'ARG',
start_date = '1962',
end_date = "1962",
verbose = T),
'Processing bulk file, this writes to your cache directory') # nolint
expect_message(comtradr::ct_get_bulk(reporter = 'ARG',
start_date = '1962',
end_date = "1962",
verbose = T),
'Performing request, which can take a few seconds, depend') # nolint

expect_message(comtradr::ct_get_bulk(reporter = 'ARG',
start_date = '1962',
end_date = "1962",
verbose = T),
'Will download files size of: 183.3 KB') # nolint
})
})

0 comments on commit 19c4a29

Please sign in to comment.