Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error code [400] when downloading dataset from comtrade #24

Closed
hansronald opened this issue May 5, 2020 · 2 comments
Closed

Error code [400] when downloading dataset from comtrade #24

hansronald opened this issue May 5, 2020 · 2 comments

Comments

@hansronald
Copy link

I have used the example code from

https://rpubs.com/matt_smith/ITNr

library(comtradr)

#Find all the codes associated with "motor vechicle"
auto_codes <- ct_commodity_lookup("motor vehicle",
                                  return_code = FALSE,
                                  return_char = TRUE)

##Download the data for trade in these codes (motor vechicles)
auto_trade<- ct_search(reporters = "All",
                       partners = "All",
                       trade_direction = "imports",
                       start_date = "2016-01-01",
                       end_date = "2016-12-31",
                       commod_codes = auto_codes)

When I run this I get the error code

Error: Comtrade API request failed, with status code [400]

Any idea why this doesn't work?

@ChrisMuir
Copy link
Member

Hey, so there's a few things going on here. The API call is returning 400 because the vector auto_codes contains the long description of each commodity. Here's the first element of auto_codes:

ct_commodity_lookup("motor vehicle", return_code = FALSE, return_char = TRUE)[1]
[1] "271012 - Petroleum spirit for motor vehicles **LEGACY NON-WCO CODE**"

If you're passing commodity codes along to API calls, you'll want to use return_code = TRUE in the call to ct_commodity_lookup():

ct_commodity_lookup("motor vehicle", return_code = TRUE, return_char = TRUE)
 [1] "271012" "830120" "830140" "830230" "841520" "8512"   "851220" "851230" "851240" "851290" "8703"   "8705"   "8706"   "870600" "8707"  
[16] "870710" "870790" "8708"   "940120"

Doing this will alleviate the 400 code response:

#Find all the codes associated with "motor vechicle"
auto_codes <- ct_commodity_lookup("motor vehicle",
                                  return_code = TRUE,
                                  return_char = TRUE)

##Download the data for trade in these codes (motor vechicles)
auto_trade<- ct_search(reporters = "All",
                       partners = "All",
                       trade_direction = "imports",
                       start_date = "2016-01-01",
                       end_date = "2016-12-31",
                       commod_codes = auto_codes)
 Error: API request failed. Err msg from Comtrade:
  Both 'all' reporters and 'all' partners may not be selected. Select a different reporter or partner. 

The issue here is that the catch-all input "All" cannot be used for both reporters and partners. For more info on this, check out the section API rate limits in this vignette.

I appreciate you bringing this to my attention though, I think I'm going to add a check to ct_search() that ensures the elements of param commod_codes can be cast as ints. Thanks!

@ChrisMuir
Copy link
Member

ChrisMuir commented May 16, 2020

Pushed a commit last night (137ff09) that should handle this a little more gracefully. An error is thrown prior to an api call being made to Comtrade. Thank you again for bringing this up!

Here is the output of the code now:

library(comtradr)

#Find all the codes associated with "motor vechicle"
auto_codes <- ct_commodity_lookup("motor vehicle",
                                  return_code = FALSE,
                                  return_char = TRUE)

##Download the data for trade in these codes (motor vechicles)
auto_trade<- ct_search(reporters = "All",
                       partners = "All",
                       trade_direction = "imports",
                       start_date = "2016-01-01",
                       end_date = "2016-12-31",
                       commod_codes = auto_codes)
Error: arg 'commod_codes' must be either 'TOTAL', or 'ALL', or a char vector of codes that can be cast as integers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants