Skip to content

Commit

Permalink
Properly asynchronize the library, and use cancellation tokens (break…
Browse files Browse the repository at this point in the history
…ing change).
  • Loading branch information
mcartoixa committed Oct 26, 2017
1 parent d91a457 commit a47664a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 37 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.sonarqube/
.vs/

bin/
csx/
obj/
packages/
tmp/

*.cache
*.dbmdl
*.DotSettings
*.log
*.suo
*.user
3 changes: 2 additions & 1 deletion NetMonkey/LoggerMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ await request.Content.ReadAsStringAsync()
var timer = new Stopwatch();
timer.Start();

ret=await base.SendAsync(request, cancellationToken);
ret=await base.SendAsync(request, cancellationToken)
.ConfigureAwait(false);

timer.Stop();
if (ret!=null)
Expand Down
63 changes: 32 additions & 31 deletions NetMonkey/MailChimpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http.Headers;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

Expand Down Expand Up @@ -73,8 +74,9 @@ public void Dispose()
/// <summary>Adds the specified member to the specified list.</summary>
/// <param name="listId">The unique id for the list.</param>
/// <param name="member">The new member to add.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The new list member.</returns>
public async Task<Model.ListMember> AddListMember(string listId, Model.ListMember member)
public async Task<Model.ListMember> AddListMember(string listId, Model.ListMember member, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(listId));
if (string.IsNullOrEmpty(listId))
Expand All @@ -99,18 +101,19 @@ public void Dispose()
Encoding.UTF8,
_JsonMediaType
);
using (var response = await _Client.PostAsync(uriBuilder.Uri, content))
using (var response = await _Client.PostAsync(uriBuilder.Uri, content, cancellationToken).ConfigureAwait(false))
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Model.ListMember>(await response.Content.ReadAsStringAsync(), SerializerSettings);
return JsonConvert.DeserializeObject<Model.ListMember>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
else
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync(), SerializerSettings);
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
}

/// <summary>Gets information about a list’s interest categories.</summary>
/// <param name="listId">The unique id for the list.</param>
/// <param name="query">The optional (but recommended) query.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Information about a list’s interest categories.</returns>
public async Task<Model.InterestCategoryResults> GetInterestCategories(string listId, ResultsQuery<Model.InterestCategoryResults> query)
public async Task<Model.InterestCategoryResults> GetInterestCategories(string listId, ResultsQuery<Model.InterestCategoryResults> query, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(listId));
if (string.IsNullOrEmpty(listId))
Expand All @@ -129,19 +132,20 @@ public void Dispose()
if (query!=null)
uriBuilder.Query=query.ToString();

using (var response = await _Client.GetAsync(uriBuilder.Uri))
using (var response = await _Client.GetAsync(uriBuilder.Uri, cancellationToken).ConfigureAwait(false))
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Model.InterestCategoryResults>(await response.Content.ReadAsStringAsync(), SerializerSettings);
return JsonConvert.DeserializeObject<Model.InterestCategoryResults>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
else
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync(), SerializerSettings);
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
}

/// <summary>Gets a list of this category’s interests.</summary>
/// <param name="listId">The unique id for the list.</param>
/// <param name="categoryId">The unique id for the interest category.</param>
/// <param name="query">The optional (but recommended) query.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The list of this category’s interests</returns>
public async Task<Model.InterestResults> GetInterests(string listId, string categoryId, ResultsQuery<Model.InterestResults> query)
public async Task<Model.InterestResults> GetInterests(string listId, string categoryId, ResultsQuery<Model.InterestResults> query, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(listId));
if (string.IsNullOrEmpty(listId))
Expand All @@ -164,17 +168,18 @@ public void Dispose()
if (query!=null)
uriBuilder.Query=query.ToString();

using (var response = await _Client.GetAsync(uriBuilder.Uri))
using (var response = await _Client.GetAsync(uriBuilder.Uri, cancellationToken).ConfigureAwait(false))
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Model.InterestResults>(await response.Content.ReadAsStringAsync(), SerializerSettings);
return JsonConvert.DeserializeObject<Model.InterestResults>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
else
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync(), SerializerSettings);
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
}

/// <summary>Gets information about all lists in the account.</summary>
/// <param name="query">The optional (but recommended) query.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Information about lists in the account.</returns>
public async Task<Model.ListResults> GetLists(ResultsQuery<Model.ListResults> query)
public async Task<Model.ListResults> GetLists(ResultsQuery<Model.ListResults> query, CancellationToken cancellationToken)
{
var uriBuilder = new UriBuilder(
new Uri(
Expand All @@ -185,19 +190,20 @@ public void Dispose()
if (query!=null)
uriBuilder.Query=query.ToString();

using (var response = await _Client.GetAsync(uriBuilder.Uri))
using (var response = await _Client.GetAsync(uriBuilder.Uri, cancellationToken).ConfigureAwait(false))
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Model.ListResults>(await response.Content.ReadAsStringAsync(), SerializerSettings);
return JsonConvert.DeserializeObject<Model.ListResults>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
else
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync(), SerializerSettings);
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
}

/// <summary>Gets information about a specific list member.</summary>
/// <param name="listId">The unique id for the list.</param>
/// <param name="address">The list member’s email address.</param>
/// <param name="query">The optional (but recommended) query.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Information about a specific list member.</returns>
public async Task<Model.ListMember> GetListMember(string listId, MailAddress address, FieldsQuery<Model.ListMember> query)
public async Task<Model.ListMember> GetListMember(string listId, MailAddress address, FieldsQuery<Model.ListMember> query, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(listId));
if (string.IsNullOrEmpty(listId))
Expand All @@ -220,19 +226,20 @@ public void Dispose()
if (query!=null)
uriBuilder.Query=query.ToString();

using (var response = await _Client.GetAsync(uriBuilder.Uri))
using (var response = await _Client.GetAsync(uriBuilder.Uri, cancellationToken).ConfigureAwait(false))
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Model.ListMember>(await response.Content.ReadAsStringAsync(), SerializerSettings);
return JsonConvert.DeserializeObject<Model.ListMember>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
else
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync(), SerializerSettings);
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
}

/// <summary>Updates information for a specific list member.</summary>
/// <param name="listId">The unique id for the list.</param>
/// <param name="address">The list member’s email address.</param>
/// <param name="member">The member information to update.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The updated information for the list member.</returns>
public async Task<Model.ListMember> UpdateListMember(string listId, MailAddress address, Model.ListMember member)
public async Task<Model.ListMember> UpdateListMember(string listId, MailAddress address, Model.ListMember member, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(listId));
if (string.IsNullOrEmpty(listId))
Expand Down Expand Up @@ -263,11 +270,11 @@ public void Dispose()
_JsonMediaType
)
};
using (var response = await _Client.SendAsync(request))
using (var response = await _Client.SendAsync(request, cancellationToken).ConfigureAwait(false))
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Model.ListMember>(await response.Content.ReadAsStringAsync(), SerializerSettings);
return JsonConvert.DeserializeObject<Model.ListMember>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
else
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync(), SerializerSettings);
throw JsonConvert.DeserializeObject<MailChimpException>(await response.Content.ReadAsStringAsync().ConfigureAwait(false), SerializerSettings);
}

private void Dispose(bool disposing)
Expand All @@ -285,13 +292,7 @@ private void Dispose(bool disposing)
}

/// <summary>Gets the current MailChimp API key.</summary>
public string ApiKey
{
get
{
return _ApiKey;
}
}
public string ApiKey { get { return _ApiKey; } }

/// <summary>Settings for the JSON serializer.</summary>
internal protected JsonSerializerSettings SerializerSettings;
Expand Down
49 changes: 45 additions & 4 deletions NetMonkey/NetMonkey.Tests/MailChimpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace NetMonkey.Tests
Expand All @@ -8,12 +10,51 @@ public class MailChimpClientTests
{

[Fact(Skip="Requires a valid MailChimp API key")]
public async Task List_ShouldReturnData()
public async Task GetList_ShouldReturnData()
{
var client=new MailChimpClient("test-us4");
var client=new MailChimpClient("test");
var query = new ListQuery();
query.IncludeProperty(lr => lr.Lists[0].Id)
.IncludeProperty(lr => lr.Lists[0].Name)
.IncludeProperty(lr => lr.TotalItems);

var results = await client.GetLists(null);
var results = await client.GetLists(query, CancellationToken.None);
Assert.Equal(5, results.TotalItems);
}

[Fact(Skip = "Requires a valid MailChimp API key")]
public async Task GetListMember_ShouldReturnData()
{
var client = new MailChimpClient("test");
var query = new FieldsQuery<Model.ListMember>();
query.IncludeProperty(lm => lm.Id)
.IncludeProperty(lm => lm.Interests)
.IncludeProperty(lm => lm.MergeFields);

//var results = await client.GetListMember("eac8899d91", "140efef0331c72feaecfcd71c7903db9", query);
var results = await client.GetListMember("eac8899d91", new MailAddress("valentin.blanlot@gmail.com"), query, CancellationToken.None);
}

[Fact(Skip = "Requires a valid MailChimp API key")]
public async Task GetInterestCategory_ShouldReturnData()
{
var client = new MailChimpClient("test");
var query = new ResultsQuery<Model.InterestCategoryResults>();
query.IncludeProperty(icr => icr.Categories[0].Id)
.IncludeProperty(icr => icr.Categories[0].Title);

var results = await client.GetInterestCategories("eac8899d91", query, CancellationToken.None);
}

[Fact(Skip = "Requires a valid MailChimp API key")]
public async Task GetInterest_ShouldReturnData()
{
var client = new MailChimpClient("test");
var query = new ResultsQuery<Model.InterestResults>();
query.IncludeProperty(ir => ir.Interests[0].Id)
.IncludeProperty(ir => ir.Interests[0].Name);

var results = await client.GetInterests("eac8899d91", "4a51da5e25", query, CancellationToken.None);
}
}
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 3.0.2.{build}
version: 3.1.0.{build}
os: Windows Server 2012


Expand Down

0 comments on commit a47664a

Please sign in to comment.