public async System.Threading.Tasks.Task <HttpResponseMessage> ExecuteAsync(ExcelExtractHandlerFunctionArgs args) { // Get request body string requestJson = await _request.Content.ReadAsStringAsync(); var js = new JavaScriptSerializer(); try { var request = js.Deserialize <PostBody>(requestJson); if (request == null) { throw new InvalidOperationException("No merge data"); } if (request.Workbook == string.Empty && request.WorkbookUrl == string.Empty) { throw new InvalidOperationException("Document template is missing"); } byte[] workbook; if (request.Workbook != string.Empty) { workbook = Convert.FromBase64String(request.Workbook); } else { workbook = await(new WebClient()).DownloadDataTaskAsync(request.WorkbookUrl); } Log("Got workbook."); var extractedData = Extractor.Extract(workbook, request); var result = (new JavaScriptSerializer()).Serialize(extractedData); var columnCount = extractedData.Count > 0 ? extractedData[0].Keys.Count : 0; Log($"Got data from {extractedData.Count} rows and {columnCount} columns."); _response.Content = new StringContent(result); _response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); _response.StatusCode = HttpStatusCode.OK; return(_response); } catch (Exception ex) { Log(ex.ToString()); _response.StatusCode = HttpStatusCode.BadRequest; _response.Content = new StringContent(GetErrorPage()); _response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return(_response); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "ExcelExtract")] HttpRequestMessage req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); Log(log, $"C# HTTP trigger function processed a request! RequestUri={req.RequestUri}"); var func = new ExcelExtractHandler(req); func.FunctionNotify += (sender, args) => Log(log, args.Message); var functionArgs = new ExcelExtractHandlerFunctionArgs { StorageAccount = ConfigurationManager.AppSettings["ConfigurationStorageAccount"], StorageAccountKey = ConfigurationManager.AppSettings["ConfigurationStorageAccountKey"] }; return(await Task.Run(() => func.ExecuteAsync(functionArgs))); }