• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

djsuperchief / Kyameru / 9413600338

07 Jun 2024 07:54AM UTC coverage: 91.729% (+0.07%) from 91.656%
9413600338

push

github

web-flow
Merge pull request #166 from djsuperchief/159-ses-component

159 ses component

477 of 573 branches covered (83.25%)

Branch coverage included in aggregate %.

145 of 151 new or added lines in 7 files covered. (96.03%)

2595 of 2776 relevant lines covered (93.48%)

24.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.91
/source/components/Kyameru.Component.Ses/Implementation/SesTo.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Threading;
5
using System.Threading.Tasks;
6
using Amazon.Runtime.SharedInterfaces;
7
using Amazon.SimpleEmail;
8
using Amazon.SimpleEmail.Model;
9
using Kyameru.Core.Entities;
10

11
namespace Kyameru.Component.Ses;
12

13
public class SesTo : ITo
14
{
15
    private const string CharSet = "UTF-8";
16
    private readonly IAmazonSimpleEmailService sesClient;
17
    private Dictionary<string, string> headers;
18

19
    public event EventHandler<Log> OnLog;
20

21
    public SesTo(IAmazonSimpleEmailService ses)
6✔
22
    {
6✔
23
        sesClient = ses;
6✔
24
    }
6✔
25

26
    public void SetHeaders(Dictionary<string, string> incomingHeaders)
27
    {
6✔
28
        headers = incomingHeaders;
6✔
29
    }
6✔
30

31
    public async Task ProcessAsync(Routable routable, CancellationToken cancellationToken)
32
    {
5✔
33
        Validate(routable);
5✔
34
        if (routable.DataType == "SesMessage")
2✔
35
        {
1✔
36
            await SendEmail(routable, cancellationToken);
1✔
37
        }
1✔
38
        else
39
        {
1✔
40
            await SendTemplate(routable, cancellationToken);
1✔
41
        }
1✔
42
    }
2✔
43

44
    private async Task SendTemplate(Routable routable, CancellationToken cancellationToken)
45
    {
1✔
46
        var message = routable.Body as SesTemplate;
1✔
47
        var request = new SendTemplatedEmailRequest()
1✔
48
        {
1✔
49
            Source = routable.Headers.TryGetValue("SESFrom", headers["from"]),
1✔
50
            Destination = new Destination()
1✔
51
            {
1✔
52
                ToAddresses = routable.Headers["SESTo"].Split(",").ToList(),
1✔
53
                BccAddresses = GetAddresses(routable, "SESBcc"),
1✔
54
                CcAddresses = GetAddresses(routable, "SESCc"),
1✔
55
            },
1✔
56
            Template = message.Template,
1✔
57
            TemplateData = message.TemplateData
1✔
58
        };
1✔
59

60
        var response = await sesClient.SendTemplatedEmailAsync(request, cancellationToken);
1✔
61
        if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
1✔
62
        {
1✔
63
            foreach (var key in response.ResponseMetadata?.Metadata?.Keys)
7!
64
            {
2✔
65
                routable.SetHeader($"SES{key}", response.ResponseMetadata.Metadata[key]);
2✔
66
            }
2✔
67
        }
1✔
68
    }
1✔
69

70
    private async Task SendEmail(Routable routable, CancellationToken cancellationToken)
71
    {
1✔
72
        var request = new SendEmailRequest()
1✔
73
        {
1✔
74
            Destination = new Destination()
1✔
75
            {
1✔
76
                ToAddresses = routable.Headers["SESTo"].Split(",").ToList(),
1✔
77
                BccAddresses = GetAddresses(routable, "SESBcc"),
1✔
78
                CcAddresses = GetAddresses(routable, "SESCc"),
1✔
79
            },
1✔
80
            Source = routable.Headers.TryGetValue("SESFrom", headers["from"]),
1✔
81
            Message = GetEmailMessage(routable)
1✔
82
        };
1✔
83

84
        var response = await sesClient.SendEmailAsync(request, cancellationToken);
1✔
85
        if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
1✔
86
        {
1✔
87
            foreach (var key in response.ResponseMetadata?.Metadata?.Keys)
7!
88
            {
2✔
89
                routable.SetHeader($"SES{key}", response.ResponseMetadata.Metadata[key]);
2✔
90
            }
2✔
91
        }
1✔
92
    }
1✔
93

94
    private Message GetEmailMessage(Routable routable)
95
    {
1✔
96
        var message = new Message()
1✔
97
        {
1✔
98
            Body = new Body()
1✔
99
        };
1✔
100
        var bodyData = routable.Body as SesMessage;
1✔
101
        if (!string.IsNullOrWhiteSpace(bodyData.BodyHtml))
1✔
102
        {
1✔
103
            message.Body.Html = new Content()
1✔
104
            {
1✔
105
                Charset = CharSet,
1✔
106
                Data = bodyData.BodyHtml
1✔
107
            };
1✔
108

109
        }
1✔
110

111
        if (!string.IsNullOrWhiteSpace(bodyData.BodyText))
1✔
112
        {
1✔
113
            message.Body.Text = new Content()
1✔
114
            {
1✔
115
                Charset = CharSet,
1✔
116
                Data = bodyData.BodyText
1✔
117
            };
1✔
118
        }
1✔
119

120
        if (!string.IsNullOrWhiteSpace(bodyData.Subject))
1✔
121
        {
1✔
122
            message.Subject = new Content()
1✔
123
            {
1✔
124
                Charset = CharSet,
1✔
125
                Data = bodyData.Subject
1✔
126
            };
1✔
127
        }
1✔
128

129
        return message;
1✔
130
    }
1✔
131

132
    private List<string> GetAddresses(Routable routable, string key)
133
    {
4✔
134
        var output = routable.Headers.TryGetValue(key);
4✔
135
        if (!string.IsNullOrWhiteSpace(output))
4!
NEW
136
        {
×
NEW
137
            return output.Split(',').ToList();
×
138
        }
139

140
        return new List<string>();
4✔
141
    }
4✔
142

143
    private void Validate(Routable routable)
144
    {
5✔
145
        headers.TryGetValue("from", out var from);
5✔
146
        routable.Headers.TryGetValue("SESFrom", from);
5✔
147

148
        if (string.IsNullOrWhiteSpace(from))
5!
NEW
149
        {
×
NEW
150
            throw new Exceptions.MissingInformationException(string.Format(Resources.EXCEPTION_MISSINGINFORMATION, "From Address"));
×
151
        }
152

153
        if (routable.Headers["DataType"] != "SesMessage" && routable.Headers["DataType"] != "SesTemplate")
5✔
154
        {
1✔
155
            throw new Exceptions.DataTypeException(Resources.EXCEPTION_INCORRECTDATATYPE);
1✔
156
        }
157

158
        if (routable.DataType == "SesMessage")
4✔
159
        {
3✔
160
            var message = routable.Body as SesMessage;
3✔
161
            if (string.IsNullOrWhiteSpace(message.BodyHtml) && string.IsNullOrWhiteSpace(message.BodyText))
3✔
162
            {
1✔
163
                throw new Exceptions.MissingInformationException(Resources.EXCEPTION_BODYMISSING);
1✔
164
            }
165
        }
2✔
166
        else
167
        {
1✔
168
            var message = routable.Body as SesTemplate;
1✔
169
            if (string.IsNullOrWhiteSpace(message.Template) || string.IsNullOrWhiteSpace(message.TemplateData))
1!
NEW
170
            {
×
NEW
171
                throw new Exceptions.MissingInformationException(Resources.EXCEPTION_TEMPLATEMISSINGDATA);
×
172
            }
173
        }
1✔
174

175
        var to = routable.Headers.TryGetValue("SESTo");
3✔
176
        if (string.IsNullOrWhiteSpace(to) || to.Split(',').Count() <= 0)
3✔
177
        {
1✔
178
            throw new Exceptions.MissingInformationException(string.Format(Resources.EXCEPTION_MISSINGINFORMATION, "To Addresses"));
1✔
179
        }
180
    }
2✔
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc