Skip to content

Commit

Permalink
Merge branch 'subaccounts'
Browse files Browse the repository at this point in the history
  • Loading branch information
feinoujc committed May 26, 2015
2 parents 7d4c110 + 25b9af7 commit b554aef
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Mandrill.net/IMandrillSubaccountsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Mandrill.Model;

namespace Mandrill
{
public interface IMandrillSubaccountsApi
{
Task<IList<MandrillSubaccountResponse>> ListAsync(string q = null);
Task<MandrillSubaccountResponse> AddAsync(string id, string name = null, string notes = null, int? customQuota = null);
Task<MandrillSubaccountInfo> InfoAsync(string id);
Task<MandrillSubaccountResponse> UpdateAsync(string id, string name = null, string notes = null, int? customQuota = null);
Task<MandrillSubaccountResponse> DeleteAsync(string id);
Task<MandrillSubaccountResponse> PauseAsync(string id);
Task<MandrillSubaccountResponse> ResumeAsync(string id);
}

public static class MandrillSubaccountsApiSynchronousExtensions
{
public static IList<MandrillSubaccountResponse> List(this IMandrillSubaccountsApi subaccounts, string q = null)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.ListAsync(q));
}

public static MandrillSubaccountResponse Add(this IMandrillSubaccountsApi subaccounts, string id, string name = null, string notes = null, int? customQuota = null)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.AddAsync(id, name, notes, customQuota));
}

public static MandrillSubaccountInfo Info(this IMandrillSubaccountsApi subaccounts, string id)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.InfoAsync(id));
}

public static MandrillSubaccountResponse Update(this IMandrillSubaccountsApi subaccounts, string id, string name = null, string notes = null, int? customQuota = null)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.UpdateAsync(id, name, notes, customQuota));
}

public static MandrillSubaccountResponse Delete(this IMandrillSubaccountsApi subaccounts, string id)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.DeleteAsync(id));
}

public static MandrillSubaccountResponse Pause(this IMandrillSubaccountsApi subaccounts, string id)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.PauseAsync(id));
}

public static MandrillSubaccountResponse Resume(this IMandrillSubaccountsApi subaccounts, string id)
{
return AsyncHelper.InvokeSync(subaccounts, api => api.ResumeAsync(id));
}
}
}
7 changes: 7 additions & 0 deletions src/Mandrill.net/Mandrill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
<Compile Include="IMandrillTagsApi.cs" />
<Compile Include="IMandrillTemplatesApi.cs" />
<Compile Include="IMandrillUsersApi.cs" />
<Compile Include="IMandrillSubaccountsApi.cs" />
<Compile Include="MandrillApi.cs" />
<Compile Include="MandrillMessagesApi.cs" />
<Compile Include="MandrillSendersApi.cs" />
<Compile Include="MandrillSubaccountsApi.cs" />
<Compile Include="MandrillUsersApi.cs" />
<Compile Include="MandrillWhitelistsApi.cs" />
<Compile Include="Model\Messages\MandrillMergeVarContent.cs" />
Expand All @@ -78,6 +80,11 @@
<Compile Include="Model\Senders\MandrillSenderValidDetails.cs" />
<Compile Include="Model\Senders\MandrillSenderVerifyDomainRequest.cs" />
<Compile Include="Model\Senders\MandrillSenderVerifyDomain.cs" />
<Compile Include="Model\Subaccounts\MandrillSubaccountInfo.cs" />
<Compile Include="Model\Subaccounts\MandrillSubaccountsRequest.cs" />
<Compile Include="Model\Subaccounts\MandrillSubaccountStatus.cs" />
<Compile Include="Model\Subaccounts\MandrillSubaccountResponse.cs" />
<Compile Include="Model\Subaccounts\MandrillSubaccountStats.cs" />
<Compile Include="Model\Tags\MandrillTagRequest.cs" />
<Compile Include="Model\Tags\MandrillTagInfo.cs" />
<Compile Include="Model\MandrillStats.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/Mandrill.net/MandrillApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class MandrillApi
private MandrillTagsApi _tags;
private MandrillWhitelistsApi _whitelists;
private MandrillSendersApi _senders;
private MandrillSubaccountsApi _subaccounts;


public MandrillApi(string apiKey) : this(apiKey, DefaultHttpClientFactory())
{
Expand Down Expand Up @@ -86,6 +88,11 @@ public IMandrillWhitelistsApi Whitelists
get { return _whitelists ?? (_whitelists = new MandrillWhitelistsApi(this)); }
}

public IMandrillSubaccountsApi Subaccounts
{
get { return _subaccounts ?? (_subaccounts = new MandrillSubaccountsApi(this)); }
}

internal async Task<TResponse> PostAsync<TRequest, TResponse>(string requestUri, TRequest value)
where TRequest : MandrillRequestBase
{
Expand Down
78 changes: 78 additions & 0 deletions src/Mandrill.net/MandrillSubaccountsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Mandrill.Model;

namespace Mandrill
{
internal class MandrillSubaccountsApi : IMandrillSubaccountsApi
{
public MandrillApi MandrillApi { get; set; }

public MandrillSubaccountsApi(MandrillApi mandrillApi)
{
MandrillApi = mandrillApi;
}

public async Task<IList<MandrillSubaccountResponse>> ListAsync(string q = null)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, IList<MandrillSubaccountResponse>>("subaccounts/list.json", new MandrillSubaccountsRequest()
{
Q = q
});
}

public async Task<MandrillSubaccountResponse> AddAsync(string id, string name = null, string notes = null, int? customQuota = null)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, MandrillSubaccountResponse>("subaccounts/add.json", new MandrillSubaccountsRequest()
{
Id = id,
Name = name,
Notes = notes,
CustomQuota = customQuota
});
}

public async Task<MandrillSubaccountInfo> InfoAsync(string id)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, MandrillSubaccountInfo>("subaccounts/info.json", new MandrillSubaccountsRequest()
{
Id = id,
});
}

public async Task<MandrillSubaccountResponse> UpdateAsync(string id, string name = null, string notes = null, int? customQuota = null)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, MandrillSubaccountResponse>("subaccounts/update.json", new MandrillSubaccountsRequest()
{
Id = id,
Name = name,
Notes = notes,
CustomQuota = customQuota
});
}

public async Task<MandrillSubaccountResponse> DeleteAsync(string id)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, MandrillSubaccountInfo>("subaccounts/delete.json", new MandrillSubaccountsRequest()
{
Id = id,
});
}

public async Task<MandrillSubaccountResponse> PauseAsync(string id)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, MandrillSubaccountInfo>("subaccounts/pause.json", new MandrillSubaccountsRequest()
{
Id = id,
});
}

public async Task<MandrillSubaccountResponse> ResumeAsync(string id)
{
return await MandrillApi.PostAsync<MandrillSubaccountsRequest, MandrillSubaccountInfo>("subaccounts/resume.json", new MandrillSubaccountsRequest()
{
Id = id,
});
}
}
}
10 changes: 10 additions & 0 deletions src/Mandrill.net/Model/Subaccounts/MandrillSubaccountInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Mandrill.Model
{
public class MandrillSubaccountInfo : MandrillSubaccountResponse
{
[JsonProperty("last_30_days")]
public MandrillSubaccountStats Last30Days { get; set; }
}
}
31 changes: 31 additions & 0 deletions src/Mandrill.net/Model/Subaccounts/MandrillSubaccountResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Mandrill.Model
{
public class MandrillSubaccountResponse
{
public string Id { get; set; }

public string Name { get; set; }

public int CustomQuota { get; set; }

public MandrillSubaccountStatus Status { get; set; }

public int Reputation { get; set; }

[JsonConverter(typeof (IsoDateTimeConverter))]
public DateTime CreatedAt { get; set; }

[JsonConverter(typeof (IsoDateTimeConverter))]
public string FirstSentAt { get; set; }

public int SentWeekly { get; set; }

public int SentMonthly { get; set; }

public int SentTotal { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/Mandrill.net/Model/Subaccounts/MandrillSubaccountStats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Mandrill.Model
{
public class MandrillSubaccountStats : MandrillAggregateStatisticsBase
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Mandrill.Model
{
public enum MandrillSubaccountStatus
{
Active,
Paused
}
}
11 changes: 11 additions & 0 deletions src/Mandrill.net/Model/Subaccounts/MandrillSubaccountsRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Mandrill.Model
{
internal class MandrillSubaccountsRequest : MandrillRequestBase
{
public string Id { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
public int? CustomQuota { get; set; }
public string Q { get; set; }
}
}
130 changes: 130 additions & 0 deletions tests/Tests/Subaccounts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Mandrill.Model;
using NUnit.Framework;
using Mandrill;

namespace Tests
{
[Category("subaccounts")]
internal class Subaccounts : IntegrationTest
{
[Category("subaccounts/add.json")]
private class Add : Subaccounts
{
[Test]
public void Can_add_subaccount()
{
var id = Guid.NewGuid().ToString("N");
var notes = "created by test at " + DateTime.UtcNow.ToString("s");
var result = Api.Subaccounts.Add(id, name: "test", notes: notes, customQuota: null);
result.Id.Should().Be(id);
}
}

[Category("subaccounts/list.json")]
private class List : Subaccounts
{
[Test]
public void Can_list_subaccount()
{
var results = Api.Subaccounts.List(q:null);
results.Count.Should().BeGreaterOrEqualTo(0);
}
[Test]
public void Can_filter_subaccount()
{
var results = Api.Subaccounts.List(q: Guid.NewGuid().ToString("N"));
results.Count.Should().Be(0);
}
}

[Category("subaccounts/update.json")]
private class Update : Subaccounts
{
[Test]
public void Can_update_subaccount()
{
var id = Guid.NewGuid().ToString("N");
var notes = "created by test at " + DateTime.UtcNow.ToString("s");
Api.Subaccounts.Add(id, name: "test", notes: notes, customQuota: null);

var result = Api.Subaccounts.Update(id, name: "test", notes: "update", customQuota: 5000);
result.CustomQuota.Should().Be(5000);
}

}

[Category("subaccounts/pause.json")]
private class Pause : Subaccounts
{
[Test]
public void Can_pause_subaccount()
{
var id = Guid.NewGuid().ToString("N");
var notes = "created by test at " + DateTime.UtcNow.ToString("s");
Api.Subaccounts.Add(id, name: "test", notes: notes, customQuota: null);

var result = Api.Subaccounts.Pause(id);
result.Status.Should().Be(MandrillSubaccountStatus.Paused);
}

}

[Category("subaccounts/resume.json")]
private class Resume : Subaccounts
{
[Test]
public void Can_resume_subaccount()
{
var id = Guid.NewGuid().ToString("N");
var notes = "created by test at " + DateTime.UtcNow.ToString("s");
Api.Subaccounts.Add(id, name: "test", notes: notes, customQuota: null);

var result = Api.Subaccounts.Pause(id);
result.Status.Should().Be(MandrillSubaccountStatus.Paused);

result = Api.Subaccounts.Resume(id);
result.Status.Should().Be(MandrillSubaccountStatus.Active);
}

}

[Category("subaccounts/delete.json")]
private class Delete : Subaccounts
{
[Test]
public void Can_delete_subaccount()
{
var id = Guid.NewGuid().ToString("N");
var notes = "created by test at " + DateTime.UtcNow.ToString("s");
Api.Subaccounts.Add(id, name: "test", notes: notes, customQuota: null);

var result = Api.Subaccounts.Delete(id);
result.Id.Should().Be(id);
}

}

[Category("subaccounts/info.json")]
private class Info : Subaccounts
{
[Test]
public void Can_get_info_subaccount()
{
var id = Guid.NewGuid().ToString("N");
var notes = "created by test at " + DateTime.UtcNow.ToString("s");
Api.Subaccounts.Add(id, name: "test", notes: notes, customQuota: null);

var result = Api.Subaccounts.Info(id);
result.Id.Should().Be(id);
result.Last30Days.Clicks.Should().Be(0);
}

}
}
}
1 change: 1 addition & 0 deletions tests/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Senders.cs" />
<Compile Include="Subaccounts.cs" />
<Compile Include="Whitelists.cs" />
<Compile Include="Tags.cs" />
<Compile Include="IntegrationTest.cs" />
Expand Down

0 comments on commit b554aef

Please sign in to comment.