Skip to content

Establishing an API interface for your program

ale-de-vries edited this page Jan 17, 2017 · 2 revisions

Initializing the client

At the basis of the elsapy module lies the ElsClient class. This class establishes the interface through which objects in the document/author/affiliation model communicate with api.elsevier.com to get their data, and with your local file system to store data. Whatever your program is supposed to do, you probably want to start it with instantiating that interface:

myCl = ElsClient('[my_api_key]')

In this, [my_api_key] is the APIkey that you received when you registered your application on https://dev.elsevier.com.

Alternatively, you can leverage the config.json provided with elsapy to store your apikey in a configuration file, and have your program read it from there:

## Load configuration
conFile = open("config.json")
config = json.load(conFile)
conFile.close()

## Initialize client
myCl = ElsClient(config['apikey'])
myCl.inst_token = config['insttoken']

Using the client to read data from the API

Once you have established the ElsClient interface, you can pass it on to various elsapy objects as an argument when you tell them to 'read' their own data, e.g.:

myAuth.read(myCl)

When you do that, the object not only reads its data from the API; it also remembers which ElsClient instance it used for that. This means that next time you tell the object to read its data from the API, you don't have to pass on a ElsClient instance as an argument (although you still can, e.g. if you want to change the client the object uses). For example:

myAuth.read()

Using the client to write data to disk

An ElsClient instance is not only used by objects for reading its data from the API - it is also used by objects to write their data to disk. The way this works:

  • Each ElsClient has a .local_dir attribute, which by default points to a sub-directory called 'data' inside whichever folder elsapy lives in
  • Every elsapy object that has read its data (see above) is associated with an ElsClient object
  • Consequently, when the .write() method of such an elsapy object is invoked, the object writes its data to disk using the local_dir of the ElsClient object it is associated with.

You can change the local directory used by your client by setting .local_dir explicitly:

myCl.local_dir = "C:/elsapy"

Make sure the directory is valid before using it - elsapy won't create it for you if it doesn't exist.