Skip to content

Commit 11c67cf

Browse files
committed
Merge pull request #73 from SparkPost/relay_webhooks_review
Relay webhooks review
2 parents 7d01cde + 17852d1 commit 11c67cf

11 files changed

+294
-0
lines changed

src/SparkPost.Tests/DataMapperTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,5 +890,47 @@ public void It_should_map_anything_using_our_conventions()
890890
((string)result["the_date"]).Substring(0, 16).ShouldEqual("2016-01-02T03:04");
891891
}
892892
}
893+
894+
[TestFixture]
895+
public class RelayWebhookTests
896+
{
897+
[SetUp]
898+
public void Setup()
899+
{
900+
relayWebhook = new RelayWebhook();
901+
mapper = new DataMapper("v1");
902+
}
903+
904+
private RelayWebhook relayWebhook;
905+
private DataMapper mapper;
906+
907+
[Test]
908+
public void name()
909+
{
910+
var value = Guid.NewGuid().ToString();
911+
relayWebhook.Name = value;
912+
mapper.ToDictionary(relayWebhook)["name"].ShouldEqual(value);
913+
}
914+
915+
[Test]
916+
public void match_domain()
917+
{
918+
var value = Guid.NewGuid().ToString();
919+
relayWebhook.Match = new RelayWebhookMatch {Domain = value};
920+
mapper.ToDictionary(relayWebhook)["match"]
921+
.CastAs<IDictionary<string, object>>()
922+
["domain"].ShouldEqual(value);
923+
}
924+
925+
[Test]
926+
public void match_protocol()
927+
{
928+
var value = Guid.NewGuid().ToString();
929+
relayWebhook.Match = new RelayWebhookMatch {Protocol = value};
930+
mapper.ToDictionary(relayWebhook)["match"]
931+
.CastAs<IDictionary<string, object>>()
932+
["protocol"].ShouldEqual(value);
933+
}
934+
}
893935
}
894936
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using NUnit.Framework;
2+
using Should;
3+
4+
namespace SparkPost.Tests
5+
{
6+
public class RelayWebhookTests
7+
{
8+
[TestFixture]
9+
public class DefaultTests
10+
{
11+
[Test]
12+
public void It_should_initialize_match()
13+
{
14+
(new RelayWebhook()).Match.ShouldNotBeNull();
15+
}
16+
17+
[Test]
18+
public void It_should_initialize_match_protocol()
19+
{
20+
(new RelayWebhook()).Match.Protocol.ShouldEqual("SMTP");
21+
}
22+
}
23+
}
24+
}

src/SparkPost.Tests/SparkPost.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<Compile Include="TransmissionTests.cs" />
103103
<Compile Include="SubaccountTest.cs" />
104104
<Compile Include="Utilities\SnakeCaseTests.cs" />
105+
<Compile Include="RelayWebhookTests.cs" />
105106
<Compile Include="WebhookTests.cs" />
106107
</ItemGroup>
107108
<ItemGroup>

src/SparkPost/Client.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Client(string apiKey, string apiHost, long subAccountId)
3939
Subaccounts = new Subaccounts(this, requestSender, dataMapper);
4040
MessageEvents = new MessageEvents(this, requestSender);
4141
InboundDomains = new InboundDomains(this, requestSender, dataMapper);
42+
RelayWebhooks = new RelayWebhooks(this, requestSender, dataMapper);
4243

4344
CustomSettings = new Settings();
4445
}
@@ -53,6 +54,7 @@ public Client(string apiKey, string apiHost, long subAccountId)
5354
public ISubaccounts Subaccounts { get; }
5455
public IMessageEvents MessageEvents { get; }
5556
public IInboundDomains InboundDomains { get; }
57+
public IRelayWebhooks RelayWebhooks { get; }
5658
public string Version => "v1";
5759

5860
public Settings CustomSettings { get; }

src/SparkPost/DataMapper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public interface IDataMapper
2222
IDictionary<string, object> ToDictionary(Suppression suppression);
2323
IDictionary<string, object> ToDictionary(Webhook webhook);
2424
IDictionary<string, object> ToDictionary(Subaccount subaccount);
25+
IDictionary<string, object> ToDictionary(RelayWebhook relayWebhook);
2526
IDictionary<string, object> ToDictionary(InboundDomain inboundDomain);
27+
IDictionary<string, object> ToDictionary(RelayWebhookMatch relayWebhookMatch);
2628
IDictionary<string, object> CatchAll(object anything);
2729
object GetTheValue(Type propertyType, object value);
2830
IDictionary<Type, MethodInfo> ToDictionaryMethods();
@@ -131,6 +133,16 @@ public IDictionary<string, object> ToDictionary(InboundDomain inboundDomain)
131133
return WithCommonConventions(inboundDomain);
132134
}
133135

136+
public IDictionary<string, object> ToDictionary(RelayWebhook relayWebhook)
137+
{
138+
return WithCommonConventions(relayWebhook);
139+
}
140+
141+
public IDictionary<string, object> ToDictionary(RelayWebhookMatch relayWebhookMatch)
142+
{
143+
return WithCommonConventions(relayWebhookMatch);
144+
}
145+
134146
public IDictionary<string, object> ToDictionary(MessageEventsQuery query)
135147
{
136148
return WithCommonConventions(query, new Dictionary<string, object>()

src/SparkPost/IClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public interface IClient
4444
/// Gets access to the inbound domains resource of the SparkPost API.
4545
/// </summary>
4646
IInboundDomains InboundDomains { get; }
47+
48+
/// <summary>
49+
/// Gets access to the relay webhooks resource of the SparkPost API.
50+
/// </summary>
51+
IRelayWebhooks RelayWebhooks { get; }
4752

4853
/// <summary>
4954
/// Gets the API version supported by this client.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace SparkPost
5+
{
6+
public class ListRelayWebhookResponse : Response
7+
{
8+
public IEnumerable<RelayWebhook> RelayWebhooks { get; set; }
9+
10+
public static ListRelayWebhookResponse CreateFromResponse(Response response)
11+
{
12+
var result = new ListRelayWebhookResponse();
13+
14+
LeftRight.SetValuesToMatch(result, response);
15+
16+
result.RelayWebhooks = BuildTheRelayWebhooksFrom(response);
17+
18+
return result;
19+
}
20+
21+
private static IEnumerable<RelayWebhook> BuildTheRelayWebhooksFrom(Response response)
22+
{
23+
var results = JsonConvert.DeserializeObject<dynamic>(response.Content).results;
24+
25+
var relayWebhooks = new List<RelayWebhook>();
26+
foreach (var r in results)
27+
relayWebhooks.Add(ConvertToARelayWebhook(r));
28+
29+
return relayWebhooks;
30+
}
31+
32+
internal static RelayWebhook ConvertToARelayWebhook(dynamic item)
33+
{
34+
return new RelayWebhook
35+
{
36+
Id = item.id,
37+
Name = item.name,
38+
Target = item.target,
39+
AuthToken = item.auth_token,
40+
Match = new RelayWebhookMatch
41+
{
42+
Protocol = item.match.protocol,
43+
Domain = item.match.domain
44+
}
45+
};
46+
}
47+
}
48+
}

src/SparkPost/RelayWebhook.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SparkPost
5+
{
6+
public class RelayWebhook
7+
{
8+
public RelayWebhook()
9+
{
10+
Match = new RelayWebhookMatch();
11+
}
12+
13+
public string Id { get; set; }
14+
public string Target { get; set; }
15+
public string Name { get; set; }
16+
public string AuthToken { get; set; }
17+
public RelayWebhookMatch Match { get; set; }
18+
}
19+
20+
public class RelayWebhookMatch
21+
{
22+
public RelayWebhookMatch()
23+
{
24+
Protocol = "SMTP";
25+
}
26+
27+
public string Protocol { get; set; }
28+
public string Domain { get; set; }
29+
}
30+
}

src/SparkPost/RelayWebhooks.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
using SparkPost.RequestSenders;
4+
5+
namespace SparkPost
6+
{
7+
public interface IRelayWebhooks
8+
{
9+
Task<ListRelayWebhookResponse> List(object query = null);
10+
Task<Response> Create(RelayWebhook webhook);
11+
Task<RetrieveRelayWebhookResponse> Retrieve(string id);
12+
Task<Response> Update(RelayWebhook relayWebhook);
13+
Task<bool> Delete(string id);
14+
}
15+
16+
public class RelayWebhooks : IRelayWebhooks
17+
{
18+
private readonly IClient client;
19+
private readonly IDataMapper dataMapper;
20+
private readonly IRequestSender requestSender;
21+
22+
public RelayWebhooks(IClient client, IRequestSender requestSender, IDataMapper dataMapper)
23+
{
24+
this.client = client;
25+
this.requestSender = requestSender;
26+
this.dataMapper = dataMapper;
27+
}
28+
29+
public async Task<ListRelayWebhookResponse> List(object query = null)
30+
{
31+
if (query == null) query = new {};
32+
var request = new Request
33+
{
34+
Url = $"/api/{client.Version}/relay-webhooks",
35+
Method = "GET",
36+
Data = query
37+
};
38+
39+
var response = await requestSender.Send(request);
40+
if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response);
41+
42+
return ListRelayWebhookResponse.CreateFromResponse(response);
43+
}
44+
45+
public async Task<RetrieveRelayWebhookResponse> Retrieve(string id)
46+
{
47+
var request = new Request
48+
{
49+
Url = $"/api/{client.Version}/relay-webhooks/{id}",
50+
Method = "GET"
51+
};
52+
53+
var response = await requestSender.Send(request);
54+
if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response);
55+
56+
return RetrieveRelayWebhookResponse.CreateFromResponse(response);
57+
}
58+
59+
public async Task<Response> Create(RelayWebhook relayWebhook)
60+
{
61+
var request = new Request
62+
{
63+
Url = $"api/{client.Version}/relay-webhooks",
64+
Method = "POST",
65+
Data = dataMapper.ToDictionary(relayWebhook)
66+
};
67+
68+
var response = await requestSender.Send(request);
69+
if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response);
70+
71+
var createWebhookResponse = new Response();
72+
LeftRight.SetValuesToMatch(createWebhookResponse, response);
73+
return createWebhookResponse;
74+
}
75+
76+
public async Task<Response> Update(RelayWebhook relayWebhook)
77+
{
78+
var request = new Request
79+
{
80+
Url = $"api/{client.Version}/relay-webhooks/{relayWebhook.Id}",
81+
Method = "PUT",
82+
Data = dataMapper.ToDictionary(relayWebhook)
83+
};
84+
85+
var response = await requestSender.Send(request);
86+
if (response.StatusCode != HttpStatusCode.OK) throw new ResponseException(response);
87+
88+
var createWebhookResponse = new Response();
89+
LeftRight.SetValuesToMatch(createWebhookResponse, response);
90+
return createWebhookResponse;
91+
}
92+
93+
public async Task<bool> Delete(string id)
94+
{
95+
var request = new Request
96+
{
97+
Url = $"/api/{client.Version}/relay-webhooks/{id}",
98+
Method = "DELETE"
99+
};
100+
101+
var response = await requestSender.Send(request);
102+
return response.StatusCode == HttpStatusCode.OK;
103+
}
104+
}
105+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Newtonsoft.Json;
2+
3+
namespace SparkPost
4+
{
5+
public class RetrieveRelayWebhookResponse : Response
6+
{
7+
public RelayWebhook RelayWebhook { get; set; }
8+
9+
public static RetrieveRelayWebhookResponse CreateFromResponse(Response response)
10+
{
11+
var result = new RetrieveRelayWebhookResponse();
12+
LeftRight.SetValuesToMatch(result, response);
13+
14+
var results = JsonConvert.DeserializeObject<dynamic>(response.Content).results;
15+
16+
result.RelayWebhook = ListRelayWebhookResponse.ConvertToARelayWebhook(results);
17+
18+
return result;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)