PermanentOrg / node-sdk

Node.js SDK for Permanent.org
GNU Affero General Public License v3.0
4 stars 2 forks source link

Add folder and record #15

Closed cecilia-donnelly closed 3 years ago

cecilia-donnelly commented 3 years ago
cecilia-donnelly commented 3 years ago

Okay, @jasonaowen , this is no longer WIP! I think it is ready to go. To test it, I used a script that @andrewatwood left us, which is also available in our Google Doc. I would like to include it in this repository or somewhere else accessible to anyone who wants to use the SDK, but I'm not sure the right way to do that. What do you think? It has a lot in common with the snippets in the README, naturally, but I found it easier to be able to run an existing script.

In the meantime, you might also like to use it. I have included some inline comments about some things that tripped me up:

// I had to add this so that I could run the script against my local copy,
// which has an invalid cert.  Naturally you would not want to do this in
// real life.
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

// I used [npm link](https://docs.npmjs.com/cli/v7/commands/npm-link) to 
// make our in-progress package available locally, which was super easy
const permSdk = require('@permanentorg/node-sdk');
const Permanent = permSdk.Permanent;

// Pull these out of developer tools in any request in the web client.  Note
// that this is `archiveNbr`, not `archiveId`!  I was very confused by why
// this failed until I figured that out.  You don't have to use your local version
// (the SDK defaults to prod) but I found it easier to debug locally.
const permanent = new Permanent({
    'sessionToken': '',
    'mfaToken': '',
    'archiveNbr': '',
    'apiKey': '',
    'baseUrl': 'https://local.permanent.org/api',
});

// this is a very cute puppy, thank you Andrew.  Note that the
// SDK only accepts files that are already online somewhere.
const imageUrl = 'https://post.greatist.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg';
const imageName = 'image.jpg';

run();

async function run() {
  const attempts = 1;

  await permanent.init();
  console.log('session valid!');

  const start = new Date();
  const folderForSet = await permanent.folder.create(`simultaneous test @ ${new Date().toISOString()}`);

  console.log('folder created');

  const logTime = () => {
    const end = new Date();
    const hours = (end.getTime() - start.getTime()) / 1000 / 60 / 60;
    console.log(`Time elapsed: ${hours} hours`);
  }

  try {
    for (let i = 0; i < attempts; i++) {
      await Promise.all([
        permanent.record.uploadFromUrl({displayName: 'image', uploadUri: imageUrl, uploadFileName: imageName}, folderForSet),
        permanent.record.uploadFromUrl({displayName: 'image', uploadUri: imageUrl, uploadFileName: imageName}, folderForSet),
      ]);
    }
    logTime();
    console.log('all success!');
  } catch (err) {
    console.dir(error);
    logTime();
    console.error('failure :(');
  }
}
cecilia-donnelly commented 3 years ago

@jasonaowen Okay, changes made! Thanks for the thorough review.