Posts

PST to UTC Date

I had a requirement to return customer usage between two days, but database saved all usage records with end of the day PST time in UTC dates. Since API accepts date only, database has records with UTC, API need to convert date into end of the day and convert same into UTC to retrive right DB records. Eg - Given date - 4/30/2022 Database has record saved as- 5/1/2022 6:59:59 AM +00:00 (4/30/2022 23:59:59 PST) Since DB saved usage dated 4/30 as 5/1 in UTC, API would miss last record if no date conversion implemented. The below code converts given date only string into end of the day for the intended timezone with UTC format to compare records and include last record with given date ranges. //Get Pacific Time from the machine. var pacificTimeZone = "America/Los_Angeles"; //for linux env if (Environment.OSVersion.VersionString.Contains("Windows")) pacificTimeZone = "Pacific Standard Time"; var pstZone = TimeZoneInfo.FindSystemTimeZoneById(pacific

BFS - Breadth First Search

The below is sample code to iterate through graph nodes using breadth first search - //build graph nodes var tree = new Dictionary<int, List<int>>(); tree.Add(1, new List<int> { 2, 3, 4 }); tree.Add(2, new List<int> { 5 }); tree.Add(3, new List<int> { 6, 7}); tree.Add(4, new List<int> { 8 }); tree.Add(5, new List<int> { 9 }); tree.Add(6, new List<int> { 10 }); //get root element of graph Queue q = new Queue(); q.Enqueue(tree.ElementAt(0).Key); HashSet<int> visitiedNode = new HashSet<int>(); //iterate queue to visit nodes while(q.Count > 0) { //dequeue 1st element var item = Convert.ToInt32(q.Dequeue()); //check if element already visited if (visitiedNode.Contains(item)) continue; //add element to visited list visitiedNode.Add(item); Console.WriteLine(item); //get next adjecent nodes tree.TryGetValue(item, out List<int> adjcentNodes); //enqueue new nodes to the queue if (adjcentNodes != null)

Generate Google, Outlook Calendar events links

We had a requirement to save calendar events with out downloading of ICS file and save events into the customer's email client calendar directly. This requirement seems hard, but the email clients had calendar API and can have links targeted to call the right API to make this happen. The below APP provides links and can be included in an email to the customer to save events into the calendar.   https://www.labnol.org/apps/calendar.html

Initialize postman variables using Pre-request script

Image
 Often times we need to set postman variables before call API and API requests may use other API responses. Postman offers a pre-request script section that can be used to initialize postman variables. In the below request, I require to send a mfa-token as part of the header to consume API, but mfa-token is not a static one to hardcode in postman global variables. I had get this for each request to get updated mfa-token from other API. This can be achieved through pre-request script to set dynamic values. Set mfa-token variable through pre-request script by consuming another API response -  Text out of Image - const   reqObject   =  {   url:  'http://internalUrl/authentication/mfa-token' ,   method:  'GET' ,   header: { 'channel' : 'customer' } }; pm .sendRequest( reqObject , ( err ,  res )  =>  {      token   =   res .json()      console . log ( `fetched token ${ token . tokenValue }` )      pm . environment . set ( "mfa-token" ,  token . t

In-memory & Distributed (Redis) Caching in ASP.NET Core

https://medium.com/net-core/in-memory-distributed-redis-caching-in-asp-net-core-62fb33925818

Call API gateway endpoint using jQuery (Ajax)

The below shows one of the samples get calls to retrieve data from AWS API gateway - Click!