Asynchronous Call to Amazon API Gateway

Hi all,

I have been trying (for a couple of days now) to correctly set up a way to receive the results of an asynchronous Amazon API Gateway call using a custom-built (by me) server-side action plugin. For a brief synopsis of what exactly I mean (with respect to Amazon’s asynchronous call), see this link.

I have a file that I’m uploading from a FileUploader element that I then send to an Amazon API that I’ve built. This has worked flawlessly for months for the synchronous version, but I cannot figure out how to get the asynchronous version to return the data back to me, and I really need to make the asynchronous version work. I’ve spent hours upon hours reading all sorts of comments from some of the more sophisticated Bubblers on the Plugin Development board (@sudsy, @keith, @vini_brito), but nothing I’ve tried has worked.

More specifically, I have been able to set up a context.request() call that is able to successfully hit my API, but it doesn’t wait for the results. That code looks like this:

var options = {
      uri: 'https://<someAmazonAWSCall>.amazonaws.com/beta/<someAPIEndpoint>',
      method: 'POST',
      headers: {'X-Api-Key': '<Some API Key>'},
	  body: properties.fileLocation
	};  
  const objects = context.request(options);
  return { 
    "values": objects
  }

But as soon as I try to use the context.async() function with a callback, I don’t even see anything hitting my gateway. At this point I’ve exhausted the literature in the forum, and I’m far beyond my depth (as somebody who isn’t a serious programmer, but who can get around some relatively hairy concepts as needed).

For the asynchronous side of things, I’ve tried a couple of things:

  return context.async( async callback => {
  try {
      let parsed = await https.request("<someAWSAPI>.amazonaws.com/beta/<someAPIEndpoint>", 
                  {
                      method: "POST",
		      		  body: properties.fileLocation,
                      headers: {"X-Api-Key": "<some API Key>"}
                    }
               	 );
        callback( undefined, {
            subject : parsed.values
        });
    }
    catch ( err ) {
        callback( err );
    }
	});

as well as (in this case, requiring ‘node-fetch’; copy/pasted, so apologies if parens don’t line up/match perfectly):

return context.async( async callback => {
      try {               
            let parsed = await fetch('https://<someAWSAPI>.amazonaws.com/beta/<someAPIEndpoint>', {
    			method: 'POST',
			    body: {"fileLocation":properties.fileLocation},
			    headers: { 'X-Api-Key': '<some API Key>' }
					}
                )
            ;
       callback( undefined, {
                subject : parsed.values
            });
        }
        catch ( err ) {
            callback( err );
        }
    	});

If someone can point me in the right direction here, I would be exceptionally grateful.

Update:

I’ve gotten something to actually hit the API, but I still can’t get the data back. That code looks as follows:

function(properties, context) {
   const options = {
      uri: 'https://<someAWSCall>.com/beta/<SomeAPIEndpoint>',
      method: 'POST',
      headers: {'X-Api-Key': '<Some API Key>'},
	  body: properties.fileLocation
  };  
  let object = context.async( async callback => {
        try {               
            let parsed = await context.request(options);
            callback( undefined, {
                  "values" : parsed.values
            });
        }
        catch ( err ) {
            callback( err );
        }
    });
    return {
        "values": object
    }  
}

Now that the problem is simpler, hopefully this is less of a lift to help me get through the issue.

1 Like

You can’t return an object as a value like that.
I mean, you’re nesting the object, try ‘un-nesting’ it:
return object

Nice catch. You’d
Be returning

{“object”:{
       “values”:data
        }
}

Thank you for the pointers guys! I still don’t seem to see my function waiting for the results, though. Everything is processing fine on the AWS lambda and API gateway side, but I don’t see the workflow I’ve created ever getting the data (trawling through the Bubble logs tab), nor do I see any indicator that the call is blocking. Maybe I’m doing something really dumb here, but I’m quite confused.

I’m thinking part of the problem might be whether an asynchronous API via API Gateway actually returns the data ever, or just returns some info that I then need to use elsewhere to look that data up (in S3?). I’ve tried to validate this by looking at the response in Postman:

I haven’t found anything to really help me go in the right direction here, though.