|
1 | 1 | # NYPL Data API Client |
2 | 2 |
|
3 | | -Helper lib for interacting with the (internal) NYPL Data API |
| 3 | +Node module for interacting with the NYPL Platform API |
4 | 4 |
|
5 | 5 | ## Installation |
6 | 6 |
|
7 | | -Install it via npm for use inside your project: |
8 | | - |
9 | 7 | ```js |
10 | 8 | npm i @nypl/nypl-data-api-client --save |
11 | 9 | ``` |
@@ -35,106 +33,98 @@ See [usage.md](usage.md) for complete documentation of Client methods and use. |
35 | 33 | To authenticate and fetch a bib (all GETs authenticate first, by default): |
36 | 34 |
|
37 | 35 | ```js |
38 | | -client.get('bibs/sierra-nypl/17746307').then((resp) => { |
39 | | - let bib = resp.data |
40 | | - console.log('Got bib: ' + bib.title) |
41 | | -}).catch((e) => console.error('Error authenticating or fetching bib: ', e)) |
| 36 | + try { |
| 37 | + const resp = await client.get('bibs/sierra-nypl/17746307') |
| 38 | + const bib = resp.data |
| 39 | + console.log('Got bib: ' + bib.title) |
| 40 | + } catch (e) => { |
| 41 | + console.error('Error authenticating or fetching bib: ', e) |
| 42 | + } |
42 | 43 | ``` |
43 | 44 |
|
44 | 45 | To get the "Item" stream schema, which doesn't require authentication: |
45 | 46 | ```js |
46 | | -client.get('current-schemas/Item', { authenticate: false }).then((resp) => { |
47 | | - let schema = resp.data |
| 47 | + const resp = await client.get('current-schemas/Item', { authenticate: false }) |
| 48 | + const schema = resp.data |
48 | 49 | // Now we can build an avro encoder by parsing the escaped "schema" prop: |
49 | | - var avroType = require('avsc').parse(JSON.parse(schema.schema)) |
50 | | -}) |
| 50 | + const avroType = require('avsc').parse(JSON.parse(schema.schema)) |
51 | 51 | ``` |
52 | 52 |
|
53 | 53 | To get patron id `12345678` (note you must auth with an account that has the 'read:patron' role): |
54 | 54 | ```js |
55 | | -client.get('patrons/12345678').then((resp) => { |
56 | | - let patron = resp.data |
57 | | - if (!patron) console.error('Could not find patron') |
58 | | - else { |
59 | | - var pType = Object.keys(patron.fixedFields).map((key) => patron.fixedFields[key]) |
60 | | - .filter((fixed) => fixed.label === 'Patron Type')[0] |
61 | | - .value |
62 | | - var name = patron.names[0] |
63 | | - var barcode = patron.barCodes[0] |
64 | | - |
65 | | - console.log('Patron:') |
66 | | - console.log(' Name: ' + name) |
67 | | - console.log(' Barcode: ' + barcode) |
68 | | - console.log(' Patron Type: ' + pType) |
69 | | - } |
70 | | -}) |
| 55 | + const resp = await client.get('patrons/12345678') |
| 56 | + const patron = resp.data |
| 57 | + if (!patron) throw new Error('Could not find patron') |
| 58 | + |
| 59 | + const pType = Object.keys(patron.fixedFields).map((key) => patron.fixedFields[key]) |
| 60 | + .filter((fixed) => fixed.label === 'Patron Type')[0] |
| 61 | + .value |
| 62 | + var name = patron.names[0] |
| 63 | + var barcode = patron.barCodes[0] |
| 64 | + |
| 65 | + console.log('Patron:') |
| 66 | + console.log(' Name: ' + name) |
| 67 | + console.log(' Barcode: ' + barcode) |
| 68 | + console.log(' Patron Type: ' + pType) |
71 | 69 | ``` |
72 | 70 |
|
73 | 71 | To POST a new "TestSchema" schema: |
74 | 72 | ```js |
75 | | -client.post('schemas/TestSchema', { name: "TestSchema", type: "record", fields: [ ... ] }) |
76 | | - .then((resp) => { |
77 | | - if (JSON.parse(resp).data.stream !== 'TestSchema') throw Error('Error creating schema...') |
78 | | - }) |
| 73 | + const resp = await client.post('schemas/TestSchema', { name: "TestSchema", type: "record", fields: [ ... ] }) |
| 74 | + if (resp.data.stream !== 'TestSchema') throw Error('Error creating schema...') |
79 | 75 | ``` |
80 | 76 |
|
81 | 77 | ## CLI |
82 | 78 |
|
83 | 79 | A small CLI exists for common tasks. |
84 | 80 |
|
85 | | -If installed globally (i.e. `npm i -g @nypl/nypl-data-api-client`), it can be run as follows: |
| 81 | +Set up: |
86 | 82 |
|
87 | | -```js |
88 | | -nypl-data-api |
| 83 | +``` |
| 84 | +git clone git@github.com:NYPL/node-nypl-data-api-client.git |
| 85 | +cd node-nypl-data-api-client |
| 86 | +nvm use; npm i |
| 87 | +
|
| 88 | +cp .env-example .env |
| 89 | +# Fill in missing details in .env |
89 | 90 | ``` |
90 | 91 |
|
91 | | -For local installs, it can be run via local `node_modules`: |
| 92 | +To fetch the first 25 bibs: |
92 | 93 |
|
93 | 94 | ```js |
94 | | -./node_modules/.bin/nypl-data-api |
| 95 | +./bin/nypl-data-api.js get bibs |
95 | 96 | ``` |
96 | 97 |
|
97 | | -To get help with any command run: |
| 98 | +To fetch a specific bib: |
98 | 99 |
|
99 | 100 | ```js |
100 | | -nypl-data-api help [command] |
| 101 | +./bin/nypl-data-api.js get bibs/sierra-nypl/11040445 |
101 | 102 | ``` |
102 | 103 |
|
103 | | -Note that the cli uses the following environment variables, read by default from `.env`: |
| 104 | +To create a hold-request: |
104 | 105 |
|
105 | | - - NYPL_API_BASE_URL |
106 | | - - NYPL_OAUTH_KEY |
107 | | - - NYPL_OAUTH_SECRET |
108 | | - - NYPL_OAUTH_URL |
109 | | - |
110 | | -See `.env.example` for a sample `.env` file. To specify a different `.env`, use the `--envfile` param (e.g. `--envfile config/qa.env`) |
| 106 | +```js |
| 107 | +./bin/nypl-data-api.js post hold-requests '{ "record": "1234", "patron": ... }' |
| 108 | +``` |
111 | 109 |
|
112 | | -### Schema post |
| 110 | +To specify a different `.env`, use the `--envfile` param (e.g. `--envfile .env-qa`) |
113 | 111 |
|
114 | | -Run the following to upload the content of the given jsonfile to `schemas/[name]` |
| 112 | +A special `schema` command is provided for updating schemas. To post a new schema, run the following to upload the content of the given jsonfile to `schemas/[name]` |
115 | 113 |
|
116 | 114 | ```js |
117 | | -nypl-data-api schema post [name] [jsonfile] |
| 115 | +./bin/nypl-data-api.js schema post [name] [jsonfile] |
118 | 116 | ``` |
119 | 117 |
|
120 | 118 | ## Contributing |
121 | 119 |
|
122 | | -1. Cut feature branch from `master` |
123 | | -2. After review, merge to `master` |
| 120 | +1. Cut feature branchs from `main` |
| 121 | +2. After review, merge to `main` |
124 | 122 | 3. Bump version in `package.json` & note changes in `CHANGELOG.md` |
125 | 123 | 4. `git tag -a v[version]` |
126 | 124 | 5. `npm publish` |
127 | 125 |
|
128 | 126 | ## Testing |
129 | 127 |
|
130 | | -All tests work offline with `request` and `oauth` stubs: |
131 | | - |
132 | 128 | ```js |
133 | 129 | npm test |
134 | 130 | ``` |
135 | | - |
136 | | -If you want to run the test suite against real infrastructure, you can do so as follows: |
137 | | - |
138 | | -```js |
139 | | -USE_CREDENTIALS=[credentials file, e.g. '.env'] npm test |
140 | | -``` |
0 commit comments