In the new Genesys Cloud Functions integration, the Credentials configuration is quite similar to the "User Defined" type in other Data Actions. It is very flexible, but there is a case where I think it could be improved: caching credentials. Let me explain...
For Genesys Cloud Data Actions and Web Services Data Actions Integrations, there is the "User Defined (OAuth)" credential type, which creates an Auth Action that is executed before the normal Actions. But Genesys Cloud is smart enough to run it only when it has no cached token/when it is expired. So, it minimizes the token creation. Executing the same Data Action several times in a short span entails only 1 auth request.
However, as Genesys Cloud Functions has no caching features, a function that needs to use an auth-required service would need to generate a token for each execution.
So I was wondering if this is something that you have considered and/or planned to add in the future (to create an Idea if not).
The expected way to handle this is to have your function cache the auth token the first time it is invoked. The function will continue to run for a while after it is invoked, so that it is ready for the next invocation.
If you are running your function occasionally then it may be starting a new instance every time, which will have to get a new token. If there is a steady stream of requests to your function I would expect that most invocations would not need to get an auth token.
If that doesn't work for you then posting a new idea is the way to go.
Oh, so let me see if I understand. Let's suppose I have this simplified code:
var myToken;
export.handler = async (event, context, callback) {
if (myToken === undefined) {
let clientCredentialsBase64 = context.clientContext.Credentials;
myToken = getTokenSomehow(clientCredentialsBase64);
};
doAuthenticatedStuff(myToken);
}
If I ran the Function several times in a short time, the first invocation would set the mytoken variable so that the subsequent calls would reuse it without having to call getTokenSomehow again. Am I right?
The only other case you need to handle is if the the doAuthenticatedStuff method fails because of an invalid token, you will need to refresh myToken and doAuthenticatedStuff again.
If your token expires in 24 hours then this would probably never come up, if it expires in 5 minutes it would almost certainly be an issue.
Thank you Jason. I´ve seen this topic discussed in Stack Overflow, and there they say that although is a very simple caching mechanism, it is a bit unreliable, as there is no way to control how long the containers live. And also, parallel executions wouldn't share memory.
I think I will open a new Idea to have Auth Actions in this type of integration too.