Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/SparkPost.Tests/DataMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDictionary<string, object>>()
["domain"].ShouldEqual(value);
}

[Test]
public void match_protocol()
{
var value = Guid.NewGuid().ToString();
relayWebhook.Match = new RelayWebhookMatch {Protocol = value};
mapper.ToDictionary(relayWebhook)["match"]
.CastAs<IDictionary<string, object>>()
["protocol"].ShouldEqual(value);
}
}
}
}
24 changes: 24 additions & 0 deletions src/SparkPost.Tests/RelayWebhookTests.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}
1 change: 1 addition & 0 deletions src/SparkPost.Tests/SparkPost.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="TransmissionTests.cs" />
<Compile Include="SubaccountTest.cs" />
<Compile Include="Utilities\SnakeCaseTests.cs" />
<Compile Include="RelayWebhookTests.cs" />
<Compile Include="WebhookTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/SparkPost/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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; }
Expand Down
12 changes: 12 additions & 0 deletions src/SparkPost/DataMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public interface IDataMapper
IDictionary<string, object> ToDictionary(Suppression suppression);
IDictionary<string, object> ToDictionary(Webhook webhook);
IDictionary<string, object> ToDictionary(Subaccount subaccount);
IDictionary<string, object> ToDictionary(RelayWebhook relayWebhook);
IDictionary<string, object> ToDictionary(InboundDomain inboundDomain);
IDictionary<string, object> ToDictionary(RelayWebhookMatch relayWebhookMatch);
IDictionary<string, object> CatchAll(object anything);
object GetTheValue(Type propertyType, object value);
IDictionary<Type, MethodInfo> ToDictionaryMethods();
Expand Down Expand Up @@ -131,6 +133,16 @@ public IDictionary<string, object> ToDictionary(InboundDomain inboundDomain)
return WithCommonConventions(inboundDomain);
}

public IDictionary<string, object> ToDictionary(RelayWebhook relayWebhook)
{
return WithCommonConventions(relayWebhook);
}

public IDictionary<string, object> ToDictionary(RelayWebhookMatch relayWebhookMatch)
{
return WithCommonConventions(relayWebhookMatch);
}

public IDictionary<string, object> ToDictionary(MessageEventsQuery query)
{
return WithCommonConventions(query, new Dictionary<string, object>()
Expand Down
5 changes: 5 additions & 0 deletions src/SparkPost/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public interface IClient
/// Gets access to the inbound domains resource of the SparkPost API.
/// </summary>
IInboundDomains InboundDomains { get; }

/// <summary>
/// Gets access to the relay webhooks resource of the SparkPost API.
/// </summary>
IRelayWebhooks RelayWebhooks { get; }

/// <summary>
/// Gets the API version supported by this client.
Expand Down
48 changes: 48 additions & 0 deletions src/SparkPost/ListRelayWebhookResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace SparkPost
{
public class ListRelayWebhookResponse : Response
{
public IEnumerable<RelayWebhook> 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<RelayWebhook> BuildTheRelayWebhooksFrom(Response response)
{
var results = JsonConvert.DeserializeObject<dynamic>(response.Content).results;

var relayWebhooks = new List<RelayWebhook>();
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
}
};
}
}
}
30 changes: 30 additions & 0 deletions src/SparkPost/RelayWebhook.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
105 changes: 105 additions & 0 deletions src/SparkPost/RelayWebhooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Net;
using System.Threading.Tasks;
using SparkPost.RequestSenders;

namespace SparkPost
{
public interface IRelayWebhooks
{
Task<ListRelayWebhookResponse> List(object query = null);
Task<Response> Create(RelayWebhook webhook);
Task<RetrieveRelayWebhookResponse> Retrieve(string id);
Task<Response> Update(RelayWebhook relayWebhook);
Task<bool> 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<ListRelayWebhookResponse> 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<RetrieveRelayWebhookResponse> 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<Response> 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<Response> 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<bool> 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;
}
}
}
21 changes: 21 additions & 0 deletions src/SparkPost/RetrieveRelayWebhookResponse.cs
Original file line number Diff line number Diff line change
@@ -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<dynamic>(response.Content).results;

result.RelayWebhook = ListRelayWebhookResponse.ConvertToARelayWebhook(results);

return result;
}
}
}
4 changes: 4 additions & 0 deletions src/SparkPost/SparkPost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@
<Compile Include="IMessageEvents.cs" />
<Compile Include="IValueMapper.cs" />
<Compile Include="ListMessageEventsResponse.cs" />
<Compile Include="ListRelayWebhookResponse.cs" />
<Compile Include="ListInboundDomainResponse.cs" />
<Compile Include="MessageEvent.cs" />
<Compile Include="MessageEvents.cs" />
<Compile Include="MessageEventsQuery.cs" />
<Compile Include="MessageEventType.cs" />
<Compile Include="PageLink.cs" />
<Compile Include="RetrieveRelayWebhookResponse.cs" />
<Compile Include="InboundDomainResponse.cs" />
<Compile Include="Utilities\SnakeCase.cs" />
<Compile Include="ValueMappers\AnonymousValueMapper.cs" />
Expand Down Expand Up @@ -128,8 +130,10 @@
<Compile Include="ValueMappers\MapASingleItemUsingToDictionary.cs" />
<Compile Include="ValueMappers\StringObjectDictionaryValueMapper.cs" />
<Compile Include="ValueMappers\StringStringDictionaryValueMapper.cs" />
<Compile Include="RelayWebhook.cs" />
<Compile Include="InboundDomain.cs" />
<Compile Include="Webhook.cs" />
<Compile Include="RelayWebhooks.cs" />
<Compile Include="InboundDomains.cs" />
<Compile Include="Webhooks.cs" />
</ItemGroup>
Expand Down