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)); }