Integrating a CRM with external systems is quite common. For example, a business may integrate their CRM with an ERP back-office system that manages inventory and customer orders. Integration with a document management system is also common when businesses need to store all kinds of documents for multiple systems, including documents from the CRM system.
In these situations, we first identify the API interfaces needed to integrate with these external applications. Then, we get to work building code that can read and write to these systems. However, one key piece of information is the authentication to these systems that must be initiated and then maintained for a duration of time until the provided authorization expires. Instead of forcing users to build this ability, (fortunately) Creatio has a caching mechanism we can use to simplify this process.
Creatio has different types of caches, and one of them is the Application cache. The Application cache accepts some data and maintains it within the application so a future request to the API can just retrieve the needed information from this cache instead of trying to create new API keys each time.
When working inside of a C# service in Creatio, the service provides the current user connection. As an example, let’s assume we have a back office document management system and a service named BODocumentManagement written to handle all the work of pushing and pulling documents.
First, we need to initialize the Application cache contents inside the constructor. To read from the current user’s application cache, we first must get a reference to the current connection object (“userConnection”). Then, we need to use this to reference the associated Application cache while providing the name/tag that identifies the particular information we want from the cache.
In this the code below, you will see “BODocumentAPIAuthToken” cache key being provided to the application cache and then the currently stored value for that item gets returned as the “existingToken” variable.
To write/push data to the application cache, a similar approach lets you write to the same Application cache item so that it can be retrieved later.
Now that we know how to read and write to the Application cache, there is one more item that we must take care of – the expiration date of the data that is stored in the cache. The API of the external system will most likely provide some sort of duration or date that indicates when the current API key of the external system will expire. Since there could be multiple attributes returned by the external API, we should convert them to a JSON object and then we can save this object to the Creatio application cache as indicated below.
Now that we have the API token stored in the cache, we must make sure we can retrieve it and process it correctly.
So, the first step is to pull the JSON object that contains the API information and de-serialize it into its various attributes. Next, we should check to see whether it has a value in it. If not, then this is the first time it is being accessed. If we find that the value retrieved is not empty, then we actually have a previously stored Application cache value, and we should go ahead and process it.
Notice how the API key information is being used to check if the date and time in the token is past the current date and time and therefore expired. If not expired, then the API key is still valid and we can just reuse it for all our integration work. If it has in fact expired, then all we have to do is repeat the same call as indicated in the previous step of getting a fresh API key and storing it in the cache for the first time.
Hopefully this provides a good understanding of how the application cache can be leveraged to store API keys and other information that need to be cached and reused at the Creatio application level.