Skip to main content
Raymond Camden
Community Manager
Community Manager
August 15, 2023
Question

Node.js wrapper for REST APIs

  • August 15, 2023
  • 0 replies
  • 252 views

If folks are interested, I've got a Node.js *opinionated* wrapper for the REST APIs. It does a few things I particularly like:

 

* Combines the upload process into one call 

* Can poll a job (currently hard coded at two seconds)

* Can download when a job is done (ie, poll until done, then save)

 

You can find the source here: https://github.com/cfjedimaster/document-services-demos/tree/main/serviceswrapper

 

I'm only "officially" supporting Extract, but I've got a few other endpoints in there as well that I want to change the method signature on a bit. 

 

Here's a full example where I call Extract on a PDF and read the JSON:

 

require('dotenv').config();
const ServicesWrapper = require('./ServicesWrapper');

let sw = new ServicesWrapper(process.env.CLIENTID, process.env.CLIENTSECRET);

(async () => {
	let filePath = '../source_pdfs/schoolcalendar.pdf';
	let mediaType = 'application/pdf';

	let asset = await sw.upload(filePath, mediaType);
	console.log('PDF uploaded');

	let job = await sw.createExtractJob(asset);
	console.log('Extract job started.');

	let result = await sw.pollJob(job);
	console.log('Job done');

	/* 
	One example, download the json, or you can download the zip at result.resource.downloadUri...
	await sw.downloadFile(result.content.downloadUri, './extract.json');
	
	But instead, let's just fetch the json direct.
	*/
	let jsonReq = await fetch(result.content.downloadUri);
	let json = await jsonReq.json();
	console.log(JSON.stringify(json.extended_metadata, null, '\t'));


})();
This topic has been closed for replies.