Code Monkey home page Code Monkey logo

Comments (4)

wreesman avatar wreesman commented on June 15, 2024

I believe I am having this exact issue: I have credentials for a premium account and thus a token, which I obtained from https://comtrade.un.org/api/swagger/ui/index#!/Auth/Auth_Authorize, and I have verified the validity of the token via https://comtrade.un.org/ws/CheckRights.aspx (which says that I have web service access using the token to the Comtrade database), but when I pass the token in R via ct_register_token() it does not seem to be verifying, as when I then run ct_get_remaining_hourly_queries() the value has not changed from prior to passing the token (i.e. still 100, not 10,000). It appears that subsequent data pull requests are subject to guest user limits, as opposed to premium user limits that the token should provide me with.

from comtradr.

amannj avatar amannj commented on June 15, 2024

Hi all,

@wreesman, I'm not sure if these issues are necessarily related:

  1. What seems to be happening is that as long as you provide any token, the package will tell you that you have premium access rights. I'll demonstrate this below.

  2. Another question is if the package actually uses your valid premium access credentials when you run ct_search(). I have no answer to that at the moment.

For now, I want to demonstrate issue (1):

devtools::install_github("ropensci/comtradr")
library(comtradr)

See what happens when we provide no token:

# Check behaviour: no token
ct_register_token(NULL) 
getOption("comtradr")
## $comtrade
## $comtrade$token
## NULL
## 
## $comtrade$account_type
## [1] "standard"
## 
## $comtrade$per_hour_limit
## [1] 100
## 
## $comtrade$per_second_limit
## [1] 1
## 
## 
## attr(,"class")
## [1] "comtradr_credentials"
ct_get_remaining_hourly_queries()
## [1] 100

So far, so good. Now, what if we add some random/invalid token instead?

# Check behaviour: some random/invalid token
ct_register_token("badtoken")
ct_get_remaining_hourly_queries()
## [1] 10000
getOption("comtradr")
## $comtrade
## $comtrade$token
## [1] "badtoken"
## 
## $comtrade$account_type
## [1] "premium"
## 
## $comtrade$per_hour_limit
## [1] 10000
## 
## $comtrade$per_second_limit
## [1] 1
## 
## 
## attr(,"class")
## [1] "comtradr_credentials"

Here things go wrong. This is the initial issue #34 I reported.

Potential solution

I just pushed a fix to branch token-validation. Now, ct_register_token() checks if the token you provide is recognised by the API. If not, you keep your “guest” rights; if it is, you get upgraded to “premium”.

Can you try installing from that new branch? Can you please tell me if you now observe a similar behaviour to what I show below?

Start with a fresh R session and add your authentication token to R; mine is stored in variable goodtoken. Next, install comtradr from branch token-validation:

require(remotes)
remotes::install_github("amannj/comtradr", ref = "token-validation")

See what happens when we provide no token:

# Check behaviour: no token
ct_register_token(NULL) 
getOption("comtradr")
## $comtrade
## $comtrade$token
## NULL
## 
## $comtrade$account_type
## [1] "standard"
## 
## $comtrade$per_hour_limit
## [1] 100
## 
## $comtrade$per_second_limit
## [1] 1
## 
## 
## attr(,"class")
## [1] "comtradr_credentials"
ct_get_remaining_hourly_queries()
## [1] 100

Same as before. Now with a rubbish token:

# Check behaviour: some random/invalid token
ct_register_token("badtoken")
ct_get_remaining_hourly_queries()
## [1] 100
getOption("comtradr")
## $comtrade
## $comtrade$token
## NULL
## 
## $comtrade$account_type
## [1] "standard"
## 
## $comtrade$per_hour_limit
## [1] 100
## 
## $comtrade$per_second_limit
## [1] 1
## 
## 
## attr(,"class")
## [1] "comtradr_credentials"

Good. Finally, let's try a legit one:

ct_register_token(token = goodtoken)
getOption("comtradr")
## $comtrade
## $comtrade$token
## [1] "....your legit token should show up here...."
## 
## $comtrade$account_type
## [1] "premium"
## 
## $comtrade$per_hour_limit
## [1] 10000
## 
## $comtrade$per_second_limit
## [1] 1
## 
## 
## attr(,"class")
## [1] "comtradr_credentials"
ct_get_remaining_hourly_queries()
## [1] 10000

Looks good. The counter seems to be working as well:

ex_1 <- ct_search(reporters = "China",
                  partners = c("Rep. of Korea", "USA", "Mexico"),
                  trade_direction = "exports")

ct_get_remaining_hourly_queries()
## [1] 9999

As I said before, I don't know if this also eliminates issue (2). For this, I think, we'd have to check if we still run into guest-user restrictions when sending out your queries. If we do, we should probably create a new issue.

@ChrisMuir: In my fix, I also had to change test-ct_register_token, because token = "some_token_str" now does not “upgrade” your account_type to premium, and devtools::check() would have thrown an error without the correction. I hope you don't mind.

from comtradr.

wreesman avatar wreesman commented on June 15, 2024

@amannj, your potential solution worked on my end; the hourly queries command is now updating correctly when I pass my token. I have no insight into issue (2), however. If I run into hitches related to that as I am making my queries, I will let you know.

from comtradr.

ChrisMuir avatar ChrisMuir commented on June 15, 2024

Resolved in PR-37.

from comtradr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.