@@ -6,9 +6,14 @@ const jsdiff = require('diff')
66const avsc = require ( 'avsc' )
77require ( 'colors' )
88
9- const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) )
9+ const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) , {
10+ default : {
11+ envfile : '.env'
12+ }
13+ } )
14+
1015const DataApiClient = require ( '../' )
11- require ( 'dotenv' ) . config ( )
16+ require ( 'dotenv' ) . config ( { path : argv . envfile } )
1217
1318const log_level = argv . log_level || 'error'
1419
@@ -28,6 +33,20 @@ var doPost = function (path, content) {
2833 } )
2934}
3035
36+ var doPatch = function ( path , content ) {
37+ if ( ( typeof content ) !== 'object' ) {
38+ content = JSON . parse ( content )
39+ }
40+
41+ client . patch ( path , content )
42+ . then ( ( resp ) => {
43+ console . log ( `Done PATCHing ${ path } ` )
44+ console . log ( `Response: ${ JSON . stringify ( resp , null , 2 ) } ` )
45+ } ) . catch ( ( e ) => {
46+ console . error ( 'Error: ' , e )
47+ } )
48+ }
49+
3150var doGet = function ( path ) {
3251 if ( ! path ) throw new Error ( 'Path required' )
3352
@@ -62,25 +81,27 @@ var showDiff = function (one, two) {
6281 console . log ( )
6382}
6483
65- function promptToPost ( path , content ) {
66- if ( ! content ) throw new Error ( 'Posting requires content to post' )
84+ function promptTo ( method , path , content , cb ) {
85+ if ( argv . content ) {
86+ content = fs . readFileSync ( argv . content , 'utf8' )
87+ }
88+ if ( ! content ) throw new Error ( `${ method } ing requires content to ${ method } ` )
6789
6890 try {
69- // Assume all posted bodies are json
70- console . log ( 'content: ' , content )
91+ // Assume all posted/patched bodies are json
7192 content = JSON . parse ( content )
7293 } catch ( e ) {
73- throw new Error ( ' Content to POST does not appear to be JSON. Only JSON supported currently.' )
94+ throw new Error ( ` Content to ${ method } does not appear to be JSON. Only JSON supported currently.` )
7495 }
7596
76- console . log ( 'POSTING' )
97+ console . log ( ` ${ method } ing` )
7798 console . log ( ` To endpoint: ${ process . env . NYPL_API_BASE_URL } ${ path } :` )
7899 console . log ( ` Using credentials: ${ process . env . NYPL_OAUTH_KEY } @${ process . env . NYPL_OAUTH_URL } ` )
79100 console . log ( `${ JSON . stringify ( content , null , 2 ) } ` )
80101 console . log ( 'Proceed?' )
81102 prompt . start ( )
82103 prompt . get ( 'y/n' , ( e , result ) => {
83- if ( result [ 'y/n' ] === 'y' ) doPost ( path , content )
104+ if ( result [ 'y/n' ] === 'y' ) cb ( path , content )
84105 else console . log ( 'Aborting.' )
85106 } )
86107}
@@ -97,10 +118,11 @@ function schemaPost () {
97118 if ( previous && next ) {
98119 showDiff ( previous , next )
99120 }
100- console . log ( 'Really upload?' )
121+ const path = `schemas/${ name } `
122+ console . log ( `Really upload to ${ process . env . NYPL_API_BASE_URL } ${ path } ?` )
101123 prompt . start ( )
102124 prompt . get ( 'y/n' , ( e , result ) => {
103- if ( result [ 'y/n' ] === 'y' ) doPost ( `schemas/ ${ name } ` , next )
125+ if ( result [ 'y/n' ] === 'y' ) doPost ( path , next )
104126 else console . log ( 'Aborting.' )
105127 } )
106128 }
@@ -140,7 +162,13 @@ const commandHash = {
140162 description : 'Post JSON to an arbitrary endpoint. Will prepare request and confirm before proceeding.' ,
141163 usage : 'nypl-data-api post [path] [inlinejson]' ,
142164 examples : [ 'nypl-data-api post recap/checkin-requests \'{ "foo": "bar" }\'' ] ,
143- exec : ( argv . y ? doPost : promptToPost )
165+ exec : ( argv . y ? doPost : ( path , content ) => promptTo ( 'POST' , path , content , doPost ) )
166+ } ,
167+ patch : {
168+ description : 'Patch JSON to an arbitrary endpoint. Will prepare request and confirm before proceeding.' ,
169+ usage : 'nypl-data-api patch [path] [inlinejson]' ,
170+ examples : [ 'nypl-data-api patch hold-requests \'{ "success": false }\'' ] ,
171+ exec : ( argv . y ? doPatch : ( path , content ) => promptTo ( 'PATCH' , path , content , doPatch ) )
144172 } ,
145173 get : {
146174 description : 'Get arbitrary endpoint.' ,
0 commit comments