コード例 #1
0
        public async Task <HttpResponseData> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "school/{schoolid:Guid}")]
            HttpRequestData req,
            FunctionContext executionContext,
            string schoolid)
        {
            var logger = executionContext.GetLogger(nameof(SchoolFunction));

            logger.LogInformation($"C# HTTP trigger function GET school '{schoolid}'");

            var response = req.CreateResponse(HttpStatusCode.OK);

            response.Headers.Add("Content-Type", "application/json");

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

            // Once here, query the Schools service to do further work
            try
            {
                logger.LogInformation($"Getting school {schoolGuid}");
                var school = await SchoolsService.GetById(schoolGuid);

                var stringBody = JsonSerializer.Serialize(TranslateSchool(school), JsonOptions);
                response.WriteString(stringBody);
            }
            catch (Exception ex)
            {
                logger.LogInformation($"Exception attempting to retrieve and serialize school: {ex.Message} STACK: {ex.StackTrace}");
                ResponseJsonHandler.SetExceptionToHttpResponse(ex, response, JsonOptions);
            }

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

            return(response);
        }
コード例 #2
0
        public async Task <HttpResponseData> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get")]
            HttpRequestData req,
            FunctionContext executionContext)
        {
            var logger = executionContext.GetLogger(nameof(PortfolioFunction));

            logger.LogInformation("C# HTTP trigger function processed a Portfolio request.");

            var response = req.CreateResponse(HttpStatusCode.OK);

            response.Headers.Add("Content-Type", "application/json");

            try
            {
                logger.LogInformation($"Getting all landings");
                var landings = await LandingsService.GetAll();

                var stringBody = JsonSerializer.Serialize(new RestModels.Home()
                {
                    Landings = landings.Select(landing => LandingFunction.TranslateLanding(landing)).ToList(),
                    Links    = new[]
                    {
                        new RestModels.Link()
                        {
                            Rel      = "self",
                            Href     = "api/portfolio",
                            Method   = "GET",
                            PostData = null
                        }
                    }
                }, JsonOptions);
                response.WriteString(stringBody);
            }
            catch (Exception ex)
            {
                logger.LogInformation($"Exception attempting to retrieve and serialize portfolio: {ex.Message} STACK: {ex.StackTrace}");
                ResponseJsonHandler.SetExceptionToHttpResponse(ex, response, JsonOptions);
            }

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

            return(response);
        }