Articles in this section
Category / Section

Avoid unnecessary generation of access token in embedded application

Published:

Implementing Token Caching for Bold BI Embedding in ASP. NET Core

When embedding Bold BI dashboards using JavaScript embedding, we use BoldBI.create() method to embed the dashboard which triggers the “Authorization Server API” to get access token and dashboard details. To optimize this process and avoid unnecessary API calls, a caching mechanism can be implemented to reuse the access token and dashboard details until the expiration time is reached, as provided in the JavaScript API expirationTime. This article outlines the steps to cache the API response from the Authorization Server API in an ASP.NET Core application. By using the methods outlined in this article, you can achieve the same in any platform.

Prerequisites

Steps to Implement Caching

Step 1: Configure In-Memory Cache

In the Program.cs file, include the following line to add in-memory caching services:

builder.Services.AddMemoryCache();

Step 2: Utilize In-Memory Cache in Controller

In the HomeController.cs file, include the necessary namespace and code to utilize the in-memory cache:

using Microsoft.Extensions.Caching.Memory;

public class HomeController : Controller
{
    private readonly IMemoryCache _memoryCache;

    public HomeController(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    [HttpPost]
    [Route("AuthorizationServer")]
    public string AuthorizationServer([FromBody] object embedQueryString)
    {
        string resultContent = "";
        if (!_memoryCache.TryGetValue("API_response", out string cachedData))
        {
            // Code to handle initial state or when the cache is expired
            // ... (API call and caching logic)
        }
        else
        {
            // When the cache is alive, use the cached data
            resultContent = cachedData;
        }

        return resultContent;
    }
}

Step 3: Implement Caching Logic

Within the AuthorizationServer method, implement the logic to cache the API response:

// Deserialize the embedQueryString to an EmbedClass object
var embedClass = Newtonsoft.Json.JsonConvert.DeserializeObject<EmbedClass>(embedQueryString.ToString());

// Construct the query for the Authorization Server API
var embedQuery = embedClass.embedQueryString;
// ... (additional query parameters)

// Make an API call if the cache is expired or not set
using (var client = new HttpClient())
{
    // ... (HTTP client setup and API call)

    // Parse the query string to get the expiration time
    NameValueCollection queryParams = HttpUtility.ParseQueryString(embedQuery);
    var expsTimeInSecs = int.Parse(queryParams["embed_expirationtime"].Trim('\"'));
    double minutes = (double)expsTimeInSecs / 60;

    // Set the cache with the API response and expiration time
    _memoryCache.Set("API_response", resultContent, TimeSpan.FromMinutes(minutes));
}

Now, when the expiration time is included, the API response will be utilized until it expires. If it expires, the Authorization Server API will send a usual API request to the Bold BI server for a new access token along with the dashboard details for embedding purposes.

Conclusion

By implementing a caching mechanism for the Authorization Server API Response, the application can reduce the number of API calls to the Bold BI server, thereby improving performance and efficiency. The above steps provide a guideline for caching the token and dashboard details in an ASP.NET Core application when embedding Bold BI dashboards.

Note: JavaScript API expirationTime is an optional parameter. We can customize the value with a maximum value of 7 days and a minimum of 1 day. Its default value is 1 day.

Additional References

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
SM
Written by Soundarya Mani Meharan
Updated:
Comments (0)
Please  to leave a comment
Access denied
Access denied