private static DataSet GetKeyPairValueFromMemory(Guid connectionKey, string action,
                                                         string accountId, string eventId,
                                                         Dictionary <string, string> keypairs = null)
        {
            DataSet returnData = null;
            var     dsKey      = $"{connectionKey}-{action}";
            ConcurrentBag <string> bag;
            //We need to build our query
            StringBuilder sb = new StringBuilder();

            foreach (var keypair in keypairs)
            {
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append($"{keypair.Key}='{keypair.Value}'");
            }

            if (DatasetKeys.TryGetValue(dsKey, out bag))
            {
                foreach (var key in bag.AsEnumerable())
                {
                    var ds = ConnectorCache.GetCachedData <DataSet>(key);
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        var table        = ds.Tables["ResultSet"];
                        var filteredRows = table.Select(sb.ToString());
                        if (filteredRows != null && filteredRows.Any())
                        {
                            if (returnData == null)
                            {
                                //Quick and dirty way to add the table if it doesn't exist
                                returnData = new DataSet();
                                returnData.copyDataTable(ds);
                                returnData.Tables[0].Rows.Clear();
                            }
                            foreach (var r in filteredRows)
                            {
                                returnData.Tables[0].CopyRow(ds.Tables[0], r);
                            }
                        }
                    }
                }
            }

            return(returnData);
        }
        public static DataSet GetDataset(ScribeConnection connection, Extensions.Actions action,
                                         string eventId = null, DateTime?modifiedAfter = null, DateTime?modifiedBefore = null, string additionCondition = null,
                                         Dictionary <string, string> keypairs = null)
        {
            var     strAction  = action.Name();
            var     isKeyPairs = keypairs != null;
            var     key        = ConnectorCache.Generatekey(connection.ConnectionKey, strAction, connection.AccountId, connection.EventId, null, keypairs);
            DataSet ds         = ConnectorCache.GetCachedData <DataSet>(key);

            if (ds != null)
            {
                return(ds);
            }


            if (isKeyPairs)
            {
                ds = GetKeyPairValueFromMemory(connection.ConnectionKey, strAction, connection.AccountId, connection.EventId, keypairs);
                if (ds != null)
                {
                    return(ds);
                }
            }

            //If we do not have key pairs, will load all pages for the dataset
            ds = GetCompleteDatasetIteratively(connection, strAction, connection.AccountId, eventId, modifiedAfter, modifiedBefore, additionCondition, keypairs);

            if (ds != null)
            {
                ConnectorCache.StoreData(key, ds, connection.TTL);
            }

            if (!isKeyPairs)
            {
                StoreGeneratedKey(connection.ConnectionKey, strAction, key);
            }

            return(ds);
        }
        public static JObject GetJObject(ScribeConnection connection, Extensions.Actions action, string accountId = null,
                                         string eventId = null)
        {
            var     strAction = action.Name();
            var     key       = ConnectorCache.Generatekey(connection.ConnectionKey, strAction, connection.AccountId, connection.EventId, null, null);
            JObject json      = ConnectorCache.GetCachedData <JObject>(key);

            if (json != null)
            {
                Logger.WriteDebug($"The action {strAction} successfully returned in GetJobject from cache.");
                return(json);
            }
            HttpResponse result = DoHttpGetInternal(connection.BaseUrl, strAction, connection.AccessToken, accountId, eventId);

            if (!String.IsNullOrEmpty(result.RawText) && result.RawText.StartsWith("{\"status\":\"error\",\"msg\":\"Not authorized to access account"))
            {
                connection.ReConnnect();
                if (connection.IsConnected)
                {
                    result = DoHttpGetInternal(connection.BaseUrl, strAction, connection.AccessToken, accountId, eventId);
                }
            }
            if (result == null)
            {
                Logger.WriteError("Result of get was null in GetJObject");
                throw new ApplicationException("Result of get was null");
            }
            var res = result.RawText;

            json = JObject.Parse(res);

            if (json != null)
            {
                ConnectorCache.StoreData(key, json, connection.TTL);
                Logger.WriteDebug($"The action {strAction} successfully returned in GetJobject");
            }
            return(json);
        }