Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @nypl/nypl-data-api-client with all npm packages installed. Try it out:

var nyplDataApiClient = require("@nypl/nypl-data-api-client")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@nypl/nypl-data-api-client v1.0.0

Simple client for interacting with NYPL's internal data api

NYPL Data API Client

Helper lib for interacting with the (internal) NYPL Data API

Installation

Install it via npm for use inside your project:

npm i @nypl/nypl-data-api-client --save

Usage

Initialization

const NyplClient = require('@nypl/nypl-data-api-client')
var client = new NyplClient({ base_url: 'http://[FQDN].com/api/v0.1/' })

Docs

See usage.md for complete documentation of Client methods and use.

(Usage doc is generated via ./node_modules/.bin/jsdoc2md lib/client.js > usage.md.)

Examples

To authenticate and fetch a bib (all GETs authenticate first, by default):

client.get('bibs/sierra-nypl/17746307').then((resp) => {
  let bib = resp.data
  console.log('Got bib: ' + bib.title)
}).catch((e) => console.error('Error authenticating or fetching bib: ', e))

To get the "Item" stream schema, which doesn't require authentication:

client.get('current-schemas/Item', { authenticate: false }).then((resp) => {
  let schema = resp.data
  // Now we can build an avro encoder by parsing the escaped "schema" prop:
  var avroType = require('avsc').parse(JSON.parse(schema.schema))
})

To get patron id 12345678 (note you must auth with an account that has the 'read:patron' role):

client.get('patrons/12345678').then((resp) => {
  let patron = resp.data
  if (!patron) console.error('Could not find patron')
  else {
    var pType = Object.keys(patron.fixedFields).map((key) => patron.fixedFields[key])
      .filter((fixed) => fixed.label === 'Patron Type')[0]
      .value
    var name = patron.names[0]
    var barcode = patron.barCodes[0]

    console.log('Patron:')
    console.log('  Name: ' + name)
    console.log('  Barcode: ' + barcode)
    console.log('  Patron Type: ' + pType)
  }
})

To POST a new "TestSchema" schema:

client.post('schemas/TestSchema', '{ "name": "TestSchema", "type": "record", "fields": [ ... ] }')
  .then((resp) => {
    if (JSON.parse(resp).data.stream !== 'TestSchema') throw Error('Error creating schema...')
  })

CLI

A small CLI exists for common tasks. Run the following for a list of commands:

nypl-data-api

To get help with any command run:

nypl-data-api help [command]

Note that the lib draws from the following environment variables, which you can place in .env:

  • NYPL_API_BASE_URL
  • NYPL_OAUTH_KEY
  • NYPL_OAUTH_SECRET
  • NYPL_OAUTH_URL

Schema post

Run the following to upload the content of the given jsonfile to schemas/[name]

nypl-data-api schema post [name] [jsonfile]

Testing

All tests work offline with request and oauth stubs:

npm test

If you want to run the test suite against real infrastructure, you can do so as follows:

USE_CREDENTIALS=[credentials file, e.g. '.env'] npm test
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free