11-13-2022 03:41 AM
Hi,
I’m developing a custom snap with basic auth ( user/pass ) account.
The snap includes several suggestion properties that need to use the access token from the account in order to display relevant values received through an API call.
I can connect and authorize the account through the account.connect() method, but this will generate new access token each time I click on the suggestion property ( and it will do the same for each suggestion property ).
Is there a way to save the access token from the account and reuse it on each suggestion property instead of generating new access token constantly ?
I am really not sure how to achieve this in the defineProperties() method, if it is possible at all ?
I also have the ValidatableAccount interface implemented on the account, which in return successfully connects to the API ( through the user/pass combination ), I was also wondering if it is possible to reuse the same token when the validate button is clicked ?
Connect() method code
@Override
public String connect() throws ExecutionException {
try {
// Will return the access token - meaning the user is authenticated
return authUser(param1, param2);
} catch (Exception e) {
// Will throw an error message if the authUser() throws an exception
throw new ExecutionException(e.getMessage());
}
}
What I have within the defineProperties method
// propertyBuilder
.withSuggestions(new Suggestions() {
@Override
public void suggest(SuggestionBuilder suggestionBuilder, PropertyValues propertyValues) {
String accessToken = myAccount.connect(); // will return the access token
// return String[] with values based on the parameters
suggestionBuilder.node("MYNODE").suggestions(callAPIEndpoint(accessToken, parameeters));
}
})
What I want to achieve:
// propertyBulder
.withSuggestions(new Suggestions() {
@Override
public void suggest(SuggestionBuilder suggestionBuilder, PropertyValues propertyValues) {
// do not generate a new access token, instead use the one that was already generated
// return String[] with values based on the parameters
suggestionBuilder.node("MYNODE").suggestions(callAPIEndpoint(accessToken, parameeters));
}
})
My issue here is ( even though this approach works ) that I am generating access tokens constantly on each click on the suggestion property which is really not something I like to do.
@ptaylor Apologies for tagging you but I would appreciate your advice on this one.
Solved! Go to Solution.
11-17-2022 12:30 PM
That’s tricky. You might want to try using a static cache in the connect method that maps the user key to a token value. The connect method would return the cached token if there is one. If it fails, you’d need a way to invalid that cache entry and then obtain a new one to retry with.
11-17-2022 12:30 PM
That’s tricky. You might want to try using a static cache in the connect method that maps the user key to a token value. The connect method would return the cached token if there is one. If it fails, you’d need a way to invalid that cache entry and then obtain a new one to retry with.
11-18-2022 01:21 AM
Thank you so much @ptaylor .
This was the solution I was looking for.