diff --git a/src/SparkPost.Tests/DataMapperTests.cs b/src/SparkPost.Tests/DataMapperTests.cs index 9a7a0f49..d82a0ce0 100644 --- a/src/SparkPost.Tests/DataMapperTests.cs +++ b/src/SparkPost.Tests/DataMapperTests.cs @@ -890,5 +890,47 @@ public void It_should_map_anything_using_our_conventions() ((string)result["the_date"]).Substring(0, 16).ShouldEqual("2016-01-02T03:04"); } } + + [TestFixture] + public class RelayWebhookTests + { + [SetUp] + public void Setup() + { + relayWebhook = new RelayWebhook(); + mapper = new DataMapper("v1"); + } + + private RelayWebhook relayWebhook; + private DataMapper mapper; + + [Test] + public void name() + { + var value = Guid.NewGuid().ToString(); + relayWebhook.Name = value; + mapper.ToDictionary(relayWebhook)["name"].ShouldEqual(value); + } + + [Test] + public void match_domain() + { + var value = Guid.NewGuid().ToString(); + relayWebhook.Match = new RelayWebhookMatch {Domain = value}; + mapper.ToDictionary(relayWebhook)["match"] + .CastAs>() + ["domain"].ShouldEqual(value); + } + + [Test] + public void match_protocol() + { + var value = Guid.NewGuid().ToString(); + relayWebhook.Match = new RelayWebhookMatch {Protocol = value}; + mapper.ToDictionary(relayWebhook)["match"] + .CastAs>() + ["protocol"].ShouldEqual(value); + } + } } } \ No newline at end of file diff --git a/src/SparkPost.Tests/RelayWebhookTests.cs b/src/SparkPost.Tests/RelayWebhookTests.cs new file mode 100644 index 00000000..710e8585 --- /dev/null +++ b/src/SparkPost.Tests/RelayWebhookTests.cs @@ -0,0 +1,24 @@ +using NUnit.Framework; +using Should; + +namespace SparkPost.Tests +{ + public class RelayWebhookTests + { + [TestFixture] + public class DefaultTests + { + [Test] + public void It_should_initialize_match() + { + (new RelayWebhook()).Match.ShouldNotBeNull(); + } + + [Test] + public void It_should_initialize_match_protocol() + { + (new RelayWebhook()).Match.Protocol.ShouldEqual("SMTP"); + } + } + } +} \ No newline at end of file diff --git a/src/SparkPost.Tests/SparkPost.Tests.csproj b/src/SparkPost.Tests/SparkPost.Tests.csproj index 4c5379ff..851cfef8 100644 --- a/src/SparkPost.Tests/SparkPost.Tests.csproj +++ b/src/SparkPost.Tests/SparkPost.Tests.csproj @@ -102,6 +102,7 @@ + diff --git a/src/SparkPost/Client.cs b/src/SparkPost/Client.cs index af0e34f4..6254845b 100644 --- a/src/SparkPost/Client.cs +++ b/src/SparkPost/Client.cs @@ -39,6 +39,7 @@ public Client(string apiKey, string apiHost, long subAccountId) Subaccounts = new Subaccounts(this, requestSender, dataMapper); MessageEvents = new MessageEvents(this, requestSender); InboundDomains = new InboundDomains(this, requestSender, dataMapper); + RelayWebhooks = new RelayWebhooks(this, requestSender, dataMapper); CustomSettings = new Settings(); } @@ -53,6 +54,7 @@ public Client(string apiKey, string apiHost, long subAccountId) public ISubaccounts Subaccounts { get; } public IMessageEvents MessageEvents { get; } public IInboundDomains InboundDomains { get; } + public IRelayWebhooks RelayWebhooks { get; } public string Version => "v1"; public Settings CustomSettings { get; } diff --git a/src/SparkPost/DataMapper.cs b/src/SparkPost/DataMapper.cs index 5c817591..bc6759fb 100644 --- a/src/SparkPost/DataMapper.cs +++ b/src/SparkPost/DataMapper.cs @@ -22,7 +22,9 @@ public interface IDataMapper IDictionary ToDictionary(Suppression suppression); IDictionary ToDictionary(Webhook webhook); IDictionary ToDictionary(Subaccount subaccount); + IDictionary ToDictionary(RelayWebhook relayWebhook); IDictionary ToDictionary(InboundDomain inboundDomain); + IDictionary ToDictionary(RelayWebhookMatch relayWebhookMatch); IDictionary CatchAll(object anything); object GetTheValue(Type propertyType, object value); IDictionary ToDictionaryMethods(); @@ -131,6 +133,16 @@ public IDictionary ToDictionary(InboundDomain inboundDomain) return WithCommonConventions(inboundDomain); } + public IDictionary ToDictionary(RelayWebhook relayWebhook) + { + return WithCommonConventions(relayWebhook); + } + + public IDictionary ToDictionary(RelayWebhookMatch relayWebhookMatch) + { + return WithCommonConventions(relayWebhookMatch); + } + public IDictionary ToDictionary(MessageEventsQuery query) { return WithCommonConventions(query, new Dictionary() diff --git a/src/SparkPost/IClient.cs b/src/SparkPost/IClient.cs index 24c260d4..85ba6a21 100644 --- a/src/SparkPost/IClient.cs +++ b/src/SparkPost/IClient.cs @@ -44,6 +44,11 @@ public interface IClient /// Gets access to the inbound domains resource of the SparkPost API. /// IInboundDomains InboundDomains { get; } + + /// + /// Gets access to the relay webhooks resource of the SparkPost API. + /// + IRelayWebhooks RelayWebhooks { get; } /// /// Gets the API version supported by this client. diff --git a/src/SparkPost/ListRelayWebhookResponse.cs b/src/SparkPost/ListRelayWebhookResponse.cs new file mode 100644 index 00000000..f35c9430 --- /dev/null +++ b/src/SparkPost/ListRelayWebhookResponse.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace SparkPost +{ + public class ListRelayWebhookResponse : Response + { + public IEnumerable RelayWebhooks { get; set; } + + public static ListRelayWebhookResponse CreateFromResponse(Response response) + { + var result = new ListRelayWebhookResponse(); + + LeftRight.SetValuesToMatch(result, response); + + result.RelayWebhooks = BuildTheRelayWebhooksFrom(response); + + return result; + } + + private static IEnumerable BuildTheRelayWebhooksFrom(Response response) + { + var results = JsonConvert.DeserializeObject(response.Content).results; + + var relayWebhooks = new List(); + foreach (var r in results) + relayWebhooks.Add(ConvertToARelayWebhook(r)); + + return relayWebhooks; + } + + internal static RelayWebhook ConvertToARelayWebhook(dynamic item) + { + return new RelayWebhook + { + Id = item.id, + Name = item.name, + Target = item.target, + AuthToken = item.auth_token, + Match = new RelayWebhookMatch + { + Protocol = item.match.protocol, + Domain = item.match.domain + } + }; + } + } +} \ No newline at end of file diff --git a/src/SparkPost/RelayWebhook.cs b/src/SparkPost/RelayWebhook.cs new file mode 100644 index 00000000..d81b208a --- /dev/null +++ b/src/SparkPost/RelayWebhook.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace SparkPost +{ + public class RelayWebhook + { + public RelayWebhook() + { + Match = new RelayWebhookMatch(); + } + + public string Id { get; set; } + public string Target { get; set; } + public string Name { get; set; } + public string AuthToken { get; set; } + public RelayWebhookMatch Match { get; set; } + } + + public class RelayWebhookMatch + { + public RelayWebhookMatch() + { + Protocol = "SMTP"; + } + + public string Protocol { get; set; } + public string Domain { get; set; } + } +} \ No newline at end of file diff --git a/src/SparkPost/RelayWebhooks.cs b/src/SparkPost/RelayWebhooks.cs new file mode 100644 index 00000000..e9471fdb --- /dev/null +++ b/src/SparkPost/RelayWebhooks.cs @@ -0,0 +1,105 @@ +using System.Net; +using System.Threading.Tasks; +using SparkPost.RequestSenders; + +namespace SparkPost +{ + public interface IRelayWebhooks + { + Task List(object query = null); + Task Create(RelayWebhook webhook); + Task Retrieve(string id); + Task Update(RelayWebhook relayWebhook); + Task Delete(string id); + } + + public class RelayWebhooks : IRelayWebhooks + { + private readonly IClient client; + private readonly IDataMapper dataMapper; + private readonly IRequestSender requestSender; + + public RelayWebhooks(IClient client, IRequestSender requestSender, IDataMapper dataMapper) + { + this.client = client; + this.requestSender = requestSender; + this.dataMapper = dataMapper; + } + + public async Task List(object query = null) + { + if (query == null) query = new {}; + var request = new Request + { + Url = $"/api/{client.Version}/relay-webhooks", + Method = "GET", + Data = query + }; + + var response = await requestSender.Send(request); + if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response); + + return ListRelayWebhookResponse.CreateFromResponse(response); + } + + public async Task Retrieve(string id) + { + var request = new Request + { + Url = $"/api/{client.Version}/relay-webhooks/{id}", + Method = "GET" + }; + + var response = await requestSender.Send(request); + if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response); + + return RetrieveRelayWebhookResponse.CreateFromResponse(response); + } + + public async Task Create(RelayWebhook relayWebhook) + { + var request = new Request + { + Url = $"api/{client.Version}/relay-webhooks", + Method = "POST", + Data = dataMapper.ToDictionary(relayWebhook) + }; + + var response = await requestSender.Send(request); + if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response); + + var createWebhookResponse = new Response(); + LeftRight.SetValuesToMatch(createWebhookResponse, response); + return createWebhookResponse; + } + + public async Task Update(RelayWebhook relayWebhook) + { + var request = new Request + { + Url = $"api/{client.Version}/relay-webhooks/{relayWebhook.Id}", + Method = "PUT", + Data = dataMapper.ToDictionary(relayWebhook) + }; + + var response = await requestSender.Send(request); + if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response); + + var createWebhookResponse = new Response(); + LeftRight.SetValuesToMatch(createWebhookResponse, response); + return createWebhookResponse; + } + + public async Task Delete(string id) + { + var request = new Request + { + Url = $"/api/{client.Version}/relay-webhooks/{id}", + Method = "DELETE" + }; + + var response = await requestSender.Send(request); + return response.StatusCode == HttpStatusCode.OK; + } + } +} \ No newline at end of file diff --git a/src/SparkPost/RetrieveRelayWebhookResponse.cs b/src/SparkPost/RetrieveRelayWebhookResponse.cs new file mode 100644 index 00000000..5d182f68 --- /dev/null +++ b/src/SparkPost/RetrieveRelayWebhookResponse.cs @@ -0,0 +1,21 @@ +using Newtonsoft.Json; + +namespace SparkPost +{ + public class RetrieveRelayWebhookResponse : Response + { + public RelayWebhook RelayWebhook { get; set; } + + public static RetrieveRelayWebhookResponse CreateFromResponse(Response response) + { + var result = new RetrieveRelayWebhookResponse(); + LeftRight.SetValuesToMatch(result, response); + + var results = JsonConvert.DeserializeObject(response.Content).results; + + result.RelayWebhook = ListRelayWebhookResponse.ConvertToARelayWebhook(results); + + return result; + } + } +} \ No newline at end of file diff --git a/src/SparkPost/SparkPost.csproj b/src/SparkPost/SparkPost.csproj index 93fc4940..6510c1a7 100644 --- a/src/SparkPost/SparkPost.csproj +++ b/src/SparkPost/SparkPost.csproj @@ -59,12 +59,14 @@ + + @@ -128,8 +130,10 @@ + +