Ejemplo n.º 1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "delete", Route = null)] HttpRequestMessage req,
            ILogger log,
            ExecutionContext context)
        {
            log.LogInformation($"C# HTTP trigger function processed this request: {req}");

            // Validation of the inputs
            if (req == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound));
            }

            ShortUrlEntity input = await req.Content.ReadAsAsync <ShortUrlEntity>();

            if (input == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound));
            }

            ShortUrlEntity result;
            var            config = new ConfigurationBuilder()
                                    .SetBasePath(context.FunctionAppDirectory)
                                    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                    .AddEnvironmentVariables()
                                    .Build();

            StorageTableHelper stgHelper = new StorageTableHelper(config["UlsDataStorage"]);

            try
            {
                result = await stgHelper.ArchiveShortUrlEntity(input);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "An unexpected error was encountered.");
                return(req.CreateResponse(HttpStatusCode.BadRequest, ex));
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log,
            ExecutionContext context,
            ClaimsPrincipal principal)
        {
            log.LogInformation($"C# HTTP trigger function processed this request: {req}");

            string         userId = string.Empty;
            ShortUrlEntity input;
            ShortUrlEntity result;

            try
            {
                var invalidRequest = Utility.CatchUnauthorize(principal, log);
                if (invalidRequest != null)
                {
                    return(invalidRequest);
                }
                else
                {
                    userId = principal.FindFirst(ClaimTypes.GivenName).Value;
                    log.LogInformation("Authenticated user {user}.", userId);
                }

                // Validation of the inputs
                if (req == null)
                {
                    return(new BadRequestObjectResult(new { StatusCode = HttpStatusCode.NotFound }));
                }

                using (var reader = new StreamReader(req.Body))
                {
                    var body = reader.ReadToEnd();
                    input = JsonSerializer.Deserialize <ShortUrlEntity>(body, new JsonSerializerOptions {
                        PropertyNameCaseInsensitive = true
                    });
                    if (input == null)
                    {
                        return(new BadRequestObjectResult(new { StatusCode = HttpStatusCode.NotFound }));
                    }
                }

                var config = new ConfigurationBuilder()
                             .SetBasePath(context.FunctionAppDirectory)
                             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                             .AddEnvironmentVariables()
                             .Build();

                StorageTableHelper stgHelper = new StorageTableHelper(config["UlsDataStorage"]);

                result = await stgHelper.ArchiveShortUrlEntity(input);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "An unexpected error was encountered.");
                return(new BadRequestObjectResult(new {
                    message = ex.Message,
                    StatusCode = HttpStatusCode.BadRequest
                }));
            }

            return(new OkObjectResult(result));
        }