ternandsparrow / wild-orchid-watch-pwa

Progressive Web App for the Wild Orchid Watch citizen science project
https://app.wildorchidwatch.org
4 stars 4 forks source link

Create a facade for the iNat API #33

Closed tomsaleeba closed 4 years ago

tomsaleeba commented 4 years ago

In https://github.com/ternandsparrow/wild-orchid-watch-pwa/issues/32 we talk about how clients can access the obs. Getting access to the private coords requires the user to be a curator in our project and that's not ideal because that role also gives almost admin access to the whole project.

We can solve this problem in two ways:

The second option is easier to implement as we don't need any inat buy-in but it also means there's an extra piece of infrastructure we have to maintain. Having our own "server" may be a good thing though as it means we have a layer of abstraction that we can use to insulate the gov agency clients from inat changes.

Running a serverless API is the smart choice. AWS API Gateway and Lambda or Google cloud functions are both solid choices. AWS API Gateway certainly allows you to maintain your own list of api keys and I'm sure that GCP has the same offering.

tomsaleeba commented 4 years ago

GCP Functions seem to be able to keep running after returning a response to the user but the timing is out. I put a setTimeout for 5 seconds but it runs 15 seconds later.

Here's the code used to test:

const https = require('https')

// sync version
// exports.doFacade = (req, res) => {
//   let message = process.env.INAT_API_PREFIX
//   let accum = ''
//   https
//     .get('https://worker.techotom.com/', res2 => {
//       res2.on('data', d => {
//         accum += d
//       })
//       res2.on('end', () => {
//         res.status(200).send({
//           msg: message,
//           fromCall: accum,
//         });
//       })
//     })
//     .on('error', err => {
//       res.status(500).send({
//         msg: err.message
//       });
//     })
// };

// async version
exports.doFacade = (req, res) => {
  let message = process.env.INAT_API_PREFIX
  res.status(200).send({
    msg: message,
    fromCall: 'will be called in 5 sec',
  });
  setTimeout(function(){
    let accum = ''
    https
      .get('https://worker.techotom.com/', res2 => {
        res2.on('data', d => {
          accum += d
        })
        res2.on('end', () => {
          console.log(accum)
        })
      })
      .on('error', err => {
        console.error(err)
      })
  }, 5000)
};
tomsaleeba commented 4 years ago

That comment above is actually talking about an API facade for observations on the way into the system. It was testing if we could return to the user before we've finished uploading to iNat, but we don't need any of that stuff now as we have a working method of uploading direct to iNat.

tomsaleeba commented 4 years ago

Done, it's here https://github.com/ternandsparrow/wild-orchid-watch-api-facade