public static async Task <DataOfEntries> GetDataAsyncInRange(int intFromUT, int intToUT) { DataOfEntries doeRet = new DataOfEntries(); doeRet.count = 0; TableQuery <Entry> entryQuery = new TableQuery <Entry>().Where( TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.GreaterThanOrEqual, intFromUT.ToString()), TableOperators.And, TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThanOrEqual, intToUT.ToString()) ) ); TableContinuationToken tctContinuationToken = null; do { var result = await ctEntity.ExecuteQuerySegmentedAsync(entryQuery, tctContinuationToken); tctContinuationToken = result.ContinuationToken; if (result.Results != null) { foreach (Entry entity in result.Results) { doeRet.count++; doeRet.entries.Add(entity); } } } while (tctContinuationToken != null); return(doeRet); }
private static async Task <int> SaveDataAsync(DataOfEntries doeData, CloudTable ctTable) { if (doeData.count == 0) { return(0); } Int32 currUnixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; for (int i = 0; i < doeData.entries.Count; i++) { try { // Create the InsertOrReplace table operation doeData.entries[i].setAzureFields(i, currUnixTimestamp); TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(doeData.entries[i]); // Execute the operation TableResult result = await ctTable.ExecuteAsync(insertOrMergeOperation); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); return(-1); } } return(1); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string strId = req.Query["id"]; int intId = -1; try { intId = int.Parse(strId); } catch { intId = 1; } #region Query dataBase DataFetch dfDataFetch = new DataFetch(); Task <Entry> te = DataFetch.getSingleRecord(intId); te.Wait(); DataOfEntries doeResults = new DataOfEntries(); Entry e = (Entry)te.Result; if (string.IsNullOrEmpty(e.PartitionKey)) { doeResults.count = 0; } else { doeResults.count = 1; doeResults.entries.Add(e); } #endregion #region Generate result string DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(DataOfEntries)); MemoryStream msObj = new MemoryStream(); js.WriteObject(msObj, doeResults); msObj.Position = 0; StreamReader srStreamReader = new StreamReader(msObj); string strJson = srStreamReader.ReadToEnd(); srStreamReader.Close(); msObj.Close(); #endregion string strResponseMessage = null; if (intId == -1) { strResponseMessage = "Query parameter not in valid format!"; } else { strResponseMessage = strJson; } return(new OkObjectResult(strResponseMessage)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); #region Set query variables string strFrom = req.Query["from"]; string strTo = req.Query["to"]; intUnixTimeStampFrom = 0; intUnixTimeStampTo = 0; try { intUnixTimeStampFrom = int.Parse(strFrom); } catch { intUnixTimeStampFrom = 0; } try { intUnixTimeStampTo = int.Parse(strTo); } catch { intUnixTimeStampTo = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; } #endregion #region Query dataBase DataFetch dfDataFetch = new DataFetch(); Task <DataOfEntries> tdoe = DataFetch.GetDataAsyncInRange(intUnixTimeStampFrom, intUnixTimeStampTo); tdoe.Wait(); DataOfEntries doeResults = (DataOfEntries)tdoe.Result; #endregion DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(DataOfEntries)); MemoryStream msObj = new MemoryStream(); js.WriteObject(msObj, doeResults); msObj.Position = 0; StreamReader srStreamReader = new StreamReader(msObj); string strJson = srStreamReader.ReadToEnd(); srStreamReader.Close(); msObj.Close(); string responseMessage = !(string.IsNullOrEmpty(strFrom) && string.IsNullOrEmpty(strTo)) ? strJson : "Missing query params!"; return(new OkObjectResult(responseMessage)); }
public static async void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log) { //Task<string> tsGetData = getData(); //tsGetData.Wait(); //var strData = tsGetData.Result; string strData = "{\"count\":1,\"entries\":[{\"API\":\"Russian Calendar\",\"Description\":\"Check if a date is a Russian holiday or not\",\"Auth\":\"\",\"HTTPS\":true,\"Cors\":\"no\",\"Link\":\"links\",\"Category\":\"Calendar\"}]}"; DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(DataOfEntries)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strData.ToString())); ms.Position = 0; DataOfEntries doeData = (DataOfEntries)dcjs.ReadObject(ms); Task <int> sdaTask = SaveDataAsync(doeData, DataFetch.ctEntity); sdaTask.Wait(); int res = sdaTask.Result; }