aboutsummaryrefslogtreecommitdiff
path: root/Emails.Transactional.Client/TransactionalEmailConfig.cs
blob: 9efeba447ccbeae6bc330f2f8e8a4688f7d4efee (plain)
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
using System;
using System.Collections.Generic;

namespace Emails.Transactional.Client
{
    /// <summary>
    /// A global configuration object for transactional email clients
    /// </summary>
    public class TransactionalEmailConfig
    {
        /// <summary>
        /// The server transaction endpoint location
        /// </summary>
        public Uri ServiceLocation { get; private set; }

        /// <summary>
        /// An email id template/translation table for email template-ids
        /// </summary>
        public IReadOnlyDictionary<string, string> TemplateIdLookup { get; private set; }

        /// <summary>
        /// Adds the mail service location to the current instance
        /// </summary>
        /// <param name="serviceLocation">The address of the remote server transaction endpoint</param>
        /// <returns>A referrence to the current object (fluent api)</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public TransactionalEmailConfig WithUrl(Uri serviceLocation)
        {
            ServiceLocation = serviceLocation ?? throw new ArgumentNullException(nameof(serviceLocation));
            return this;
        }
        /// <summary>
        /// Sets the template lookup table for the current instance
        /// </summary>
        /// <param name="templates">The template-id lookup table to referrence</param>
        /// <returns>A referrence to the current object (fluent api)</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public TransactionalEmailConfig WithTemplates(IReadOnlyDictionary<string, string> templates)
        {
            TemplateIdLookup = templates ?? throw new ArgumentNullException(nameof(templates));
            return this;
        }

        /// <summary>
        /// Gets a new <see cref="EmailTransactionRequest"/> from the specifed
        /// template name.
        /// </summary>
        /// <param name="templateName"></param>
        /// <returns></returns>
        /// <exception cref="KeyNotFoundException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        public EmailTransactionRequest GetTemplateRequest(string templateName)
        {
            //get the template from its template name
            string templateId = TemplateIdLookup[templateName];
            return new EmailTransactionRequest(templateId)
            {
                Endpoint = this.ServiceLocation
            };
        }
    }
}