rnv-data-client
v1.0.3
Published
A simple node.js client for the RNV Data Hub API
Downloads
3
Maintainers
Readme
RNV Open Data Client
A simple client in node.js to access the RNV Open Data API.
Install client from npm
$ npm install rnv-data-client
Usage example
The usage example contains two ways of using the RNV Open Data Client.
- Dynamically obtains an access token, when needed
- Always obtains an access token
For obtaining credentials for RNV Open Data API in environment variables, see the section 'preparation-steps' below.
const dotenv = require('dotenv');
const client = require('rnv-data-client').client;
const exampleQueries = require('rnv-data-client').examples;
dotenv.config();
// Run prepare_env_file.sh to create an env file
CLIENT_API_URL = process.env.CLIENT_API_URL;
OAUTH_URL = process.env.OAUTH_URL;
CLIENT_ID = process.env.CLIENT_ID;
CLIENT_SECRET = process.env.CLIENT_SECRET;
RESOURCE_ID = process.env.RESOURCE_ID;
// Modify this path when you copy this solution
ACCESS_TOKEN_CACHE_PATH = "./at_cache.json";
// Checking that the .env file was loaded
console.log("CLIENT API URL: " + CLIENT_API_URL);
// Simulate an async context
(async () => {
try {
let DYN = true;
if (DYN){
// Dynamically obtain an access token (only if token not cached and cache not expired)
const at = await client.obtainAccessToken();
// Change the key to use other queries here
query = exampleQueries["stations"]
console.log("Doing a query: " + query)
const qres = await client.doQuery(query, at);
console.log("Response to query: " + JSON.stringify(qres).substring(0, 200))
}else{
// Directly obtain access token
const at_info = await client.requestAccessToken();
query = exampleQueries["stations"]
console.log("Doing a query: " + query)
const qres = await client.doQuery(query, at_info.access_token);
console.log("Response to query: " + JSON.stringify(qres).substring(0, 200))
}
} catch (error) {
console.error("Error while running the app: " + error)
}
})();
Debug
$ DEBUG=dataclient node \<your main\>.js
Or set the DEBUG variable in your runtime environment.
Run Tests
$ npm run test
Preparation steps
Obtain Credentials
To get access to our Open Data Hub API with your solutions, it is necessary to have an oauth2 access token. You need to go through several steps to generate such a token
- You register with your name and email for "Graphql" access with the reason "Upper Hackathon 24" here. Open RNV Formular
- In around 10 minutes you should receive a mail with your basic credentials, it's recommended to save the mail
- From these credentials you can generate an access token for your coded clients, there are several ways to obtain such a token, some of them are described here.
Prepare credentials
The client for node.js is in this repo in the node/ folder.
- Download the graphql.http file to this repo and place it in the root path.
- Download the preparation script from gist (see below)
- Run the shell script ./prepare_env_file.sh this will generate a .env file which is read by the node.js application
On Unix/Mac/Linux
You can copy the shell-script from node_modules if using npm.
$ curl https://gist.github.com/rnv-opendata/8365b1317505a80359491c2124a05e94 > prepare_env_file.sh
$ sudo chmod u+rwx prepare_env_file.sh
$ ./prepare_env_file.sh > .envs
On Windows
$ curl https://gist.github.com/rnv-opendata/900d43affca063caed7918f91d9531b5 > prepare_env_file.cmd
$ pwsh prepare_env_file.cmd