pybliometrics-dev / pybliometrics

Python-based API-Wrapper to access Scopus
https://pybliometrics.readthedocs.io/en/stable/
Other
410 stars 128 forks source link

Setting API key from code still uses the API key in config.ini #231

Closed raffaem closed 2 years ago

raffaem commented 2 years ago

I'm setting my API key from code, by writing

from pybliometrics.scopus.utils import config as pybliometrics_config
pybliometrics_config['Authentication']['APIKey'] = "MYKEY"

both before

from pybliometrics.scopus import AuthorSearch
from pybliometrics.scopus.exception import Scopus401Error
import pybliometrics

and after it.

But pybliometrics is still using the API key from config.ini.

I know because I get a "Scopus401Error: Invalid API Key" error, and I have modified pybliometrics/scopus/utils/get_content.py by adding

print(f"KEYS={KEYS}")

after

from pybliometrics.scopus.utils.startup import _throttling_params, KEYS

in the get_content function, and it is printing the API key in the config file.

Michael-E-Rose commented 2 years ago

That's correct, module startup.py reads the configuration file and stores the keys in KEYS. It's not intended to set the keys on runtime.

Why don't you just change the keys in the configuration file?

raffaem commented 2 years ago

Why don't you just change the keys in the configuration file?

Because it's a shared computer and the documentation says I can change them from code

Michael-E-Rose commented 2 years ago

You probably refer to https://pybliometrics.readthedocs.io/en/stable/configuration.html#runtime, right?

It used to be possible to assign keys at runtime, but since I implemented throttling it was too difficult to maintain this feature (especially while maintaining a smooth compilation on rtfd.io).

But what should work is to change the location of the configuration at runtime (but before importing pybliometrics) as in https://pybliometrics.readthedocs.io/en/stable/configuration.html#custom-location This should give you the possibility to have distinct keys on the same installation. The only downside is that you would have to write the same directories in different configuration files.

raffaem commented 2 years ago

You probably refer to https://pybliometrics.readthedocs.io/en/stable/configuration.html#runtime, right?

Yes

It used to be possible to assign keys at runtime, but since I implemented throttling it was too difficult to maintain this feature (especially while maintaining a smooth compilation on rtfd.io).

Let's change the docs if that is no longer the case :)

But what should work is to change the location of the configuration at runtime (but before importing pybliometrics) as in https://pybliometrics.readthedocs.io/en/stable/configuration.html#custom-location This should give you the possibility to have distinct keys on the same installation.

ok, I'll try that. For the moment I achieved what I had to do.

The only downside is that you would have to write the same directories in different configuration files.

Why, if I want to keep the cache separate?

Michael-E-Rose commented 2 years ago

For now I the documentation make clear that one cannot set keys at runtime. Maybe in future this will be possible.

claell commented 1 year ago

Seems like duplicate (or related to) #161