Ejemplo n.º 1
0
        public async Task<HttpResponseData> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "page/{pageid:Guid}")]
            HttpRequestData req,
            FunctionContext executionContext,
            string pageid)
        {
            var logger = executionContext.GetLogger(nameof(PageFunction));
            logger.LogInformation($"C# HTTP trigger function GET Page '{pageid}'");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "application/json");

            if (!Guid.TryParse(pageid, out var pageGuid))
            {
                logger.LogInformation($"Invalid GUID to page, exiting with bad request");
                response.WriteString(JsonSerializer.Serialize(new
                {
                    Error = "BadId",
                    Message = $"Page ID not in proper format: {pageid}"
                }, JsonOptions));
                response.StatusCode = HttpStatusCode.BadRequest;
                return response;
            }

            // Once here, query the Schools service to do further work
            try
            {
                logger.LogInformation($"Getting page {pageGuid}");
                var page = await PagesService.GetById(pageGuid);
                var stringBody = JsonSerializer.Serialize(TranslatePage(page), JsonOptions);
                response.WriteString(stringBody);
            }
            catch (Exception ex)
            {
                logger.LogInformation($"Exception attempting to retrieve and serialize page: {ex.Message} STACK: {ex.StackTrace}");
                ResponseJsonHandler.SetExceptionToHttpResponse(ex, response, JsonOptions);
            }

            logger.LogInformation($"Sending response to client");

            return response;
        }