public AlexaFunction(IAlexaRequestVerifier reqVerifier, IAdventureSampleProcessor adventureProcessor)
        {
            _reqVerifier = reqVerifier ?? throw new ArgumentNullException($"{nameof(reqVerifier)} cannot be null");

            _adventureProcessor = adventureProcessor ??
                                  throw new ArgumentNullException($"{nameof(adventureProcessor)} cannot be null");
        }
        private async Task SendAlexaRequestAsync(HttpRequest req, ILogger logger, IAlexaRequestVerifier alexaVerifier, IAdventureSampleProcessor advSampleProcessor)
        {
            try
            {
                IActionResult res = await AlexaFunction.Run(req, logger, alexaVerifier, advSampleProcessor);

                ProcessActionResult(res);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        }
Esempio n. 3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log,
            [Inject] IAlexaRequestVerifier alexaVerifier,
            [Inject] IAdventureSampleProcessor adventureProcessor)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string textContent = null;

            //IServiceProvider servProv = _serviceProvider.Value;

            //IAlexaRequestVerifier alexaVerifier = servProv.GetRequiredService<IAlexaRequestVerifier>();

            bool isValid = false;

            try
            {
                isValid = await alexaVerifier.IsCertificateValidAsync(req);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error processing certificate");
            }

            if (!isValid)
            {
                return(new BadRequestResult());
            }

            AlexaRequest alexaRequest = null;

            try
            {
                using (StreamReader sr = new StreamReader(req.Body))
                {
                    //This allows you to do one Read operation.
                    textContent = sr.ReadToEnd();
                }

                alexaRequest = JsonConvert.DeserializeObject <AlexaRequest>(textContent);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Error processing alexa request: {textContent}");
            }

            if (alexaVerifier.IsRequestValid(alexaRequest))
            {
                var alexaResponse = await adventureProcessor.ProcessAdventureRequestAsync(alexaRequest);

                return((ActionResult) new OkObjectResult(alexaResponse));
            }
            else
            {
                log.LogError($"Alexa request is not valid: {textContent}");
            }


            return(new BadRequestResult());
        }
 public AlexaRequestVerification(RequestDelegate next, IAlexaRequestVerifier alexaCertVerifier)
 {
     _next         = next;
     _certVerifier = alexaCertVerifier;
 }