-
Notifications
You must be signed in to change notification settings - Fork 1
/
PollGetNextEmail.cs
69 lines (61 loc) · 2.66 KB
/
PollGetNextEmail.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.IO;
using System.Net;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using StarApi.SendEmail;
namespace StarApi
{
public static class PollGetNextEmail
{
private static CredentialCache GetCredential()
{
string url = @"https://star.ipo.vote/user/ajaxlogin/";
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential("api", "PCj4DsvyzADn9rY"));
return credentialCache;
}
[FunctionName("PollGetNextEmail")]
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());
}
}
}
private static void Validate(EmailRequest emailRequest)
{
if (emailRequest.apiVersion != "v1") throw new InvalidDataException($"Expected version v1, got: {emailRequest.apiVersion}");
}
}
}