Exemple #1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, methods: new string[] { "GET", "POST", "PUT", "OPTIONS" })] HttpRequestMessage req,
            TraceWriter log,
            [Inject(typeof(IUsersApi))] IUsersApi usersApi)
        {
            var httpMethods = new Dictionary <string, Func <HttpRequestMessage, TraceWriter, Task <HttpResponseMessage> > >
            {
                { "GET", async(r, l) => await usersApi.Get(r, l) },
                { "POST", async(r, l) => await usersApi.Post(r, l) },
                { "PUT", async(r, l) => await usersApi.Put(r, l) },
            };

            var response = httpMethods.ContainsKey(req.Method.Method) ? await httpMethods[req.Method.Method](req, log)
                : req.CreateResponse(req.Method.Method == "OPTIONS" ? HttpStatusCode.OK : HttpStatusCode.NotFound);

            AddCORSHeader(req, response, $"GET, POST, PUT, OPTIONS");

            return(response);
        }