public static async Task<IActionResult> RequestBallot(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                log.LogInformation($"{nameof(RequestBallot)}");

                string body = await new StreamReader(req.Body).ReadToEndAsync().ConfigureAwait(false);
                dynamic request = string.IsNullOrEmpty(body) ? CreateRequest(req.Query)
                    : JsonConvert.DeserializeObject(body);

                EmailTemplate template = EmailTemplate.Factory("ballotLink", request);
                if (template == null)
                {
                    log.LogInformation("Nothing to process");
                }
                else
                {
                    log.LogInformation($"{nameof(RequestBallot)}: {template.TemplateName}");
                    await EmailTemplate.Send(template).ConfigureAwait(false);
                    log.LogInformation($"{nameof(RequestBallot)} Succeeded");
                }
                return new OkObjectResult($"{nameof(RequestBallot)}: Succeeded");
            }
            catch (Exception ex)
            {
                log.LogError($"{nameof(RequestBallot)} FAILED", ex.Message, ex.GetType().Name, ex.StackTrace.Length, ex.InnerException.ToString());
                return new BadRequestObjectResult(ex.ToString());
            }
        }
Esempio n. 2
0
        public static async void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            CookieContainer cookies = StarLogin.Login();
            bool            done    = false;

            while (!done)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://star.ipo.vote/api/v1/getnextemail/");
                    request.CookieContainer = cookies;

                    WebResponse response = request.GetResponse();

                    log.LogInformation(((HttpWebResponse)response).StatusDescription);

                    using Stream dataStream   = response.GetResponseStream();
                    using StreamReader reader = new StreamReader(dataStream);
                    string body = reader.ReadToEnd();
                    log.LogInformation(body);
                    var emailRequest = JsonConvert.DeserializeObject <EmailRequest>(body);

                    if (emailRequest.isEmpty)
                    {
                        done = true;
                    }
                    else
                    {
                        EmailTemplate template = EmailTemplate.Factory(emailRequest.template, emailRequest.fields);
                        log.LogInformation($"{emailRequest.template}: {template.Subject} to {template.ToEmail}");
                        await EmailTemplate.Send(template).ConfigureAwait(false);
                    }
                    response.Close();
                }
                catch (Exception ex)
                {
                    log.LogError($"{nameof(SendEmail)} FAILED", ex.Message, ex.GetType().Name, ex.StackTrace.Length, ex.InnerException.ToString());
                }
            }
        }