Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly typed #211

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
ed82d9f
Add an overloaded RequestAsync to accept JArray and also refactor it …
Jericho Feb 1, 2016
eb5a119
Reformat
Jericho Feb 1, 2016
0b66b5c
Strongly typed GlobalStats
Jericho Feb 2, 2016
40fadc9
Strongly typed GlobalSuppressions
Jericho Feb 3, 2016
4659022
Strongly typed UnsubscribeGroups
Jericho Feb 3, 2016
602a9b4
Strongly typed 'Suppression'
Jericho Feb 4, 2016
fb10162
Add overload Client.Post method to accept a JArray
Jericho Feb 5, 2016
35880a9
Strongly typed Bounces
Jericho Feb 15, 2016
8e5f0d4
Strongly typed custom fields
Jericho Feb 15, 2016
b4e1f67
Display the underlying error message when exception occurs
Jericho Feb 15, 2016
5e6b31c
Added missing reference to system serialization
Jericho Feb 16, 2016
68da11f
Strongly typed Lists
Jericho Feb 16, 2016
3a01e79
Added strongly typed Custom Fields
Jericho Feb 16, 2016
ede9da5
Merge pull request #1 from sendgrid/master
Jericho Feb 17, 2016
be134ae
Add an overloaded RequestAsync to accept JArray and also refactor it …
Jericho Feb 1, 2016
7dcfd55
Reformat
Jericho Feb 1, 2016
b9ffdea
Strongly typed GlobalStats
Jericho Feb 2, 2016
017895a
Strongly typed GlobalSuppressions
Jericho Feb 3, 2016
356dc2f
Strongly typed UnsubscribeGroups
Jericho Feb 3, 2016
3a22e63
Strongly typed 'Suppression'
Jericho Feb 4, 2016
3310daa
Add overload Client.Post method to accept a JArray
Jericho Feb 5, 2016
b15c283
Strongly typed Bounces
Jericho Feb 15, 2016
431ab71
Strongly typed custom fields
Jericho Feb 15, 2016
de04c00
Display the underlying error message when exception occurs
Jericho Feb 15, 2016
0285525
Added missing reference to system serialization
Jericho Feb 16, 2016
8c18087
Strongly typed Lists
Jericho Feb 16, 2016
0f32188
Added strongly typed Custom Fields
Jericho Feb 16, 2016
adc84b8
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Feb 17, 2016
7134d77
Strongly typed Segments
Jericho Feb 17, 2016
158d23b
Replace 'new JArray(...)' with JArray.FromObject(...)'
Jericho Feb 17, 2016
a41801b
Stringly typed Contacts
Jericho Feb 20, 2016
3e776ea
Strongly typed Templates and Template Versions
Jericho Feb 25, 2016
b4d66aa
Add cancellation token to async methods
Jericho Feb 29, 2016
b5ccd02
Configure the 'Example' project to be built
Jericho Mar 1, 2016
23964af
Add 3 missing files
Jericho Mar 1, 2016
8f3deb4
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Mar 1, 2016
a59cd03
Get rid of Visual Studio warning: CS4014 Because this call is not awa…
Jericho Mar 1, 2016
d443120
Strongly typed Categories and User
Jericho Mar 1, 2016
595b03a
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Mar 1, 2016
29dad31
Strongly typed Categories and User
Jericho Mar 1, 2016
9b3b281
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Mar 1, 2016
b3426fb
ConfigureAwait(false)
Jericho Mar 4, 2016
60bc811
Strongly typed Api Keys
Jericho Mar 18, 2016
f853d70
Add .editorconfig file
Jericho Mar 18, 2016
edd4ca2
Revert changes I did not intend to commit
Jericho Mar 18, 2016
a015d29
Strongly typed campaigns
Jericho Mar 23, 2016
6e00d13
Fix unit tests
Jericho Mar 24, 2016
18d6856
Implement IDisposable interface
Jericho Apr 10, 2016
0448307
Get rid of the 'The method '...' does not need to use async/await" wa…
Jericho May 10, 2016
83cce39
Fix parameters in XML documentation that do not match the name of the…
Jericho May 10, 2016
6fe8609
Fix bug: Segments.GetRecipientsAsync ignores the segmentId
Jericho May 10, 2016
7eeeb4a
Remove redundant empty parentheses
Jericho May 10, 2016
4be377d
Convert fields to readonly
Jericho May 10, 2016
2461764
Remove redundant comma in array initializer
Jericho May 10, 2016
ec39dfa
Fix the SendGridMessage constructor that ignored the 'header' parameter
Jericho May 10, 2016
8d011fe
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho May 10, 2016
6876a8a
Restore Apikeys.cs which was overwritten in the last merge
Jericho May 10, 2016
3fa56fb
Misc
Jericho May 10, 2016
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
Prev Previous commit
Next Next commit
Strongly typed Lists
  • Loading branch information
Jericho committed Feb 16, 2016
commit 68da11f7f0885594de9cd908c9bf8bc546e46b0c
33 changes: 33 additions & 0 deletions SendGrid/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private static void Main()
UnsubscribeGroups(httpClient);
GlobalSuppressions(httpClient);
GlobalStats(httpClient);
Lists(httpClient);
}

private static void SendAsync(SendGrid.SendGridMessage message)
Expand Down Expand Up @@ -236,5 +237,37 @@ private static void GlobalStats(HttpClient httpClient)
Console.WriteLine("\n\nPress any key to continue");
Console.ReadKey();
}

private static void Lists(HttpClient httpClient)
{
Console.WriteLine("\n***** LISTS *****");

var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
var client = new SendGrid.Client(apiKey: apiKey, httpClient: httpClient);

var firstList = client.Lists.CreateAsync("My first list").Result;
Console.WriteLine("List '{0}' created. Id: {1}", firstList.Name, firstList.Id);

var secondList = client.Lists.CreateAsync("My second list").Result;
Console.WriteLine("List '{0}' created. Id: {1}", secondList.Name, secondList.Id);

client.Lists.UpdateAsync(firstList.Id, "New name").Wait();
Console.WriteLine("List '{0}' updated", firstList.Id);

var lists = client.Lists.GetAllAsync().Result;
Console.WriteLine("All lists retrieved. There are {0} lists", lists.Length);

client.Lists.DeleteAsync(firstList.Id).Wait();
Console.WriteLine("List {0} deleted", firstList.Id);

client.Lists.DeleteAsync(secondList.Id).Wait();
Console.WriteLine("List {0} deleted", secondList.Id);

lists = client.Lists.GetAllAsync().Result;
Console.WriteLine("All lists retrieved. There are {0} lists", lists.Length);

Console.WriteLine("\n\nPress any key to continue");
Console.ReadKey();
}
}
}
2 changes: 2 additions & 0 deletions SendGrid/SendGrid/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private enum Methods
public User User { get; private set; }
public CustomFields CustomFields { get; private set; }
public Contacts Contacts { get; private set; }
public Lists Lists { get; private set; }
public string Version { get; private set; }

/// <summary>
Expand All @@ -52,6 +53,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/", HttpC
Contacts = new Contacts(this);
Bounces = new Bounces(this);
CustomFields = new CustomFields(this);
Lists = new Lists(this);

_httpClient = httpClient ?? new HttpClient();
_httpClient.BaseAddress = _baseUri;
Expand Down
16 changes: 16 additions & 0 deletions SendGrid/SendGrid/Model/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace SendGrid.Model
{
public class List
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("recipient_count")]
public long RecipientCount { get; set; }
}
}
184 changes: 184 additions & 0 deletions SendGrid/SendGrid/Resources/Lists.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using Newtonsoft.Json.Linq;
using SendGrid.Model;
using SendGrid.Utilities;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace SendGrid.Resources
{
public class Lists
{
private string _endpoint;
private Client _client;

/// <summary>
/// Constructs the SendGrid Lists object.
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html
/// </summary>
/// <param name="client">SendGrid Web API v3 client</param>
/// <param name="endpoint">Resource endpoint, do not prepend slash</param>
public Lists(Client client, string endpoint = "v3/contactdb/lists")
{
_endpoint = endpoint;
_client = client;
}

public async Task<List> CreateAsync(string name)
{
var data = new JObject()
{
new JProperty("name", name)
};
var response = await _client.Post(_endpoint, data);
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync();
var bulkUpsertResult = JObject.Parse(responseContent).ToObject<List>();
return bulkUpsertResult;
}

public async Task<List[]> GetAllAsync()
{
var response = await _client.Get(_endpoint);
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync();

// Response looks like this:
// {
// "lists": [
// {
// "id": 1,
// "name": "the jones",
// "recipient_count": 1
// }
// ]
//}
// We use a dynamic object to get rid of the 'lists' property and simply return an array of lists
dynamic dynamicObject = JObject.Parse(responseContent);
dynamic dynamicArray = dynamicObject.lists;

var lists = dynamicArray.ToObject<List[]>();
return lists;
}

public async Task DeleteAsync(long listId)
{
var response = await _client.Delete(string.Format("{0}/{1}", _endpoint, listId));
response.EnsureSuccess();
}

public async Task DeleteAsync(IEnumerable<long> recipientIds)
{
var data = new JArray(recipientIds.ToArray());
var response = await _client.Delete(_endpoint, data);
response.EnsureSuccess();
}

public async Task<List[]> GetAsync(int recordsPerPage = 100, int page = 1)
{
var query = HttpUtility.ParseQueryString(string.Empty);
query["page_size"] = recordsPerPage.ToString(CultureInfo.InvariantCulture);
query["page"] = page.ToString(CultureInfo.InvariantCulture);

var response = await _client.Get(string.Format("{0}?{1}", _endpoint, query));
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync();

// Response looks like this:
// {
// "lists": [
// {
// "id": 1,
// "name": "the jones",
// "recipient_count": 1
// }
// ]
//}
// We use a dynamic object to get rid of the 'lists' property and simply return an array of lists
dynamic dynamicObject = JObject.Parse(responseContent);
dynamic dynamicArray = dynamicObject.lists;

var recipients = dynamicArray.ToObject<List[]>();
return recipients;
}

public async Task<List> GetAsync(long listId)
{
var response = await _client.Get(string.Format("{0}/{1}", _endpoint, listId));
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync();
var list = JObject.Parse(responseContent).ToObject<List>();
return list;
}

public async Task UpdateAsync(long listId, string name)
{
var data = new JObject()
{
new JProperty("name", name)
};
var response = await _client.Patch(string.Format("{0}/{1}", _endpoint, listId), data);
response.EnsureSuccess();
}

public async Task<Recipient[]> GetRecipientsAsync(long listId, int recordsPerPage = 100, int page = 1)
{
var query = HttpUtility.ParseQueryString(string.Empty);
query["page_size"] = recordsPerPage.ToString(CultureInfo.InvariantCulture);
query["page"] = page.ToString(CultureInfo.InvariantCulture);

var response = await _client.Get(string.Format("{0}/{1}/recipients?{2}", _endpoint, listId, query));
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync();

// Response looks like this:
// {
// "recipients": [
// {
// "created_at": 1422395108,
// "email": "e@example.com",
// "first_name": "Ed",
// "id": "YUBh",
// "last_clicked": null,
// "last_emailed": null,
// "last_name": null,
// "last_opened": null,
// "updated_at": 1422395108
// }
// ]
// }
// We use a dynamic object to get rid of the 'recipients' property and simply return an array of recipients
dynamic dynamicObject = JObject.Parse(responseContent);
dynamic dynamicArray = dynamicObject.recipients;

var recipients = dynamicArray.ToObject<Recipient[]>();
return recipients;
}

public async Task AddRecipientAsync(long listId, string recipientId)
{
var response = await _client.Post(string.Format("{0}/{1}/recipients/{2}", _endpoint, listId, recipientId), (JObject)null);
response.EnsureSuccess();
}

public async Task RemoveRecipientAsync(long listId, string recipientId)
{
var response = await _client.Delete(string.Format("{0}/{1}/recipients/{2}", _endpoint, listId, recipientId));
response.EnsureSuccess();
}

public async Task AddRecipientsAsync(long listId, IEnumerable<string> recipientIds)
{
var data = new JArray(recipientIds.ToArray());
var response = await _client.Post(string.Format("{0}/{1}/recipients", _endpoint, listId), data);
response.EnsureSuccess();
}
}
}
2 changes: 2 additions & 0 deletions SendGrid/SendGrid/SendGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Model\CustomField.cs" />
<Compile Include="Model\FieldType.cs" />
<Compile Include="Model\Bounce.cs" />
<Compile Include="Model\List.cs" />
<Compile Include="Model\SuppressionGroup.cs" />
<Compile Include="Model\Stats.cs" />
<Compile Include="Model\Metrics.cs" />
Expand All @@ -79,6 +80,7 @@
<Compile Include="Resources\CustomFields.cs" />
<Compile Include="Resources\GlobalSuppressions.cs" />
<Compile Include="Resources\GlobalStats.cs" />
<Compile Include="Resources\Lists.cs" />
<Compile Include="Resources\Suppressions.cs" />
<Compile Include="Resources\UnsubscribeGroups.cs" />
<Compile Include="Resources\APIKeys.cs" />
Expand Down