Skip to content

Commit b27983a

Browse files
Merge pull request #170 from sendgrid/stats
Stats
2 parents 8c6b06f + 6b584b6 commit b27983a

File tree

9 files changed

+167
-21
lines changed

9 files changed

+167
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [6.3.4] - 2015-12-15
5+
###Added
6+
- Implemented the global stats /asm/stats endpoint [GET]
7+
48
## [6.3.3] - 2015-12-14
59
###Added
610
- Implemented the global suppressions /asm/suppressions/global endpoint [GET, POST, DELETE]

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,22 @@ string email = "[email protected]";
313313
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete(email).Result;
314314
```
315315

316+
## Global Stats ##
317+
318+
Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html) for further details.
319+
320+
Global Stats provide all of your user’s email statistics for a given date range. [GET]
321+
322+
```csharp
323+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
324+
var client = new SendGrid.Client(apiKey);
325+
var startDate = "2015-11-01"; // required
326+
var endDate = "2015-12-01";
327+
var aggregatedBy = "day"; // "week" or "month" are also options
328+
// Leave off .Result for an asyncronous call
329+
HttpResponseMessage responseGet = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
330+
```
331+
316332
#How to: Testing
317333

318334
* Load the solution (We have tested using the Visual Studio Community Edition)

SendGrid/Example/Program.cs

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private static void Main()
2020
UnsubscribeGroups();
2121
Suppressions();
2222
GlobalSuppressions();
23+
GlobalStats();
2324
}
2425

2526
private static void SendAsync(SendGrid.SendGridMessage message)
@@ -33,13 +34,13 @@ private static void SendAsync(SendGrid.SendGridMessage message)
3334
{
3435
transportWeb.DeliverAsync(message).Wait();
3536
Console.WriteLine("Email sent to " + message.To.GetValue(0));
36-
Console.WriteLine("Press any key to continue.");
37+
Console.WriteLine("\n\nPress any key to continue.");
3738
Console.ReadKey();
3839
}
3940
catch (Exception ex)
4041
{
4142
Console.WriteLine(ex.Message);
42-
Console.WriteLine("Press any key to continue.");
43+
Console.WriteLine("\n\nPress any key to continue.");
4344
Console.ReadKey();
4445
}
4546
}
@@ -69,7 +70,7 @@ private static void ApiKeys()
6970
HttpResponseMessage responseGet = client.ApiKeys.Get().Result;
7071
Console.WriteLine(responseGet.StatusCode);
7172
Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result);
72-
Console.WriteLine("These are your current API Keys. Press any key to continue.");
73+
Console.WriteLine("These are your current API Keys.\n\nPress any key to continue.");
7374
Console.ReadKey();
7475

7576
// POST API KEYS
@@ -79,14 +80,14 @@ private static void ApiKeys()
7980
var apiKeyId = jsonObject.api_key_id.ToString();
8081
Console.WriteLine(responsePost.StatusCode);
8182
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
82-
Console.WriteLine("API Key created. Press any key to continue.");
83+
Console.WriteLine("API Key created.\n\nPress any key to continue.");
8384
Console.ReadKey();
8485

8586
// PATCH API KEYS
8687
HttpResponseMessage responsePatch = client.ApiKeys.Patch(apiKeyId, "CSharpTestKeyPatched").Result;
8788
Console.WriteLine(responsePatch.StatusCode);
8889
Console.WriteLine(responsePatch.Content.ReadAsStringAsync().Result);
89-
Console.WriteLine("API Key patched. Press any key to continue.");
90+
Console.WriteLine("API Key patched.\n\nPress any key to continue.");
9091
Console.ReadKey();
9192

9293
// DELETE API KEYS
@@ -96,7 +97,7 @@ private static void ApiKeys()
9697
HttpResponseMessage responseFinal = client.ApiKeys.Get().Result;
9798
Console.WriteLine(responseFinal.StatusCode);
9899
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
99-
Console.WriteLine("API Key Deleted, press any key to end.");
100+
Console.WriteLine("API Key Deleted.\n\nPress any key to end.");
100101
Console.ReadKey();
101102
}
102103

@@ -117,7 +118,7 @@ private static void UnsubscribeGroups()
117118
HttpResponseMessage responseGetUnique = client.UnsubscribeGroups.Get(unsubscribeGroupID).Result;
118119
Console.WriteLine(responseGetUnique.StatusCode);
119120
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
120-
Console.WriteLine("This is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ". Press any key to continue.");
121+
Console.WriteLine("This is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ".\n\nPress any key to continue.");
121122
Console.ReadKey();
122123

123124
// POST UNSUBSCRIBE GROUP
@@ -127,7 +128,7 @@ private static void UnsubscribeGroups()
127128
var unsubscribeGroupId = jsonObject.id.ToString();
128129
Console.WriteLine(responsePost.StatusCode);
129130
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
130-
Console.WriteLine("Unsubscribe Group created. Press any key to continue.");
131+
Console.WriteLine("Unsubscribe Group created.\n\nPress any key to continue.");
131132
Console.ReadKey();
132133

133134
// DELETE UNSUBSCRIBE GROUP
@@ -137,7 +138,7 @@ private static void UnsubscribeGroups()
137138
HttpResponseMessage responseFinal = client.UnsubscribeGroups.Get().Result;
138139
Console.WriteLine(responseFinal.StatusCode);
139140
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
140-
Console.WriteLine("Unsubscribe Group Deleted, press any key to end.");
141+
Console.WriteLine("Unsubscribe Group Deleted.\n\nPress any key to end.");
141142
Console.ReadKey();
142143
}
143144

@@ -161,7 +162,7 @@ private static void Suppressions()
161162
dynamic jsonObject = JObject.Parse(rawString);
162163
Console.WriteLine(responsePost.StatusCode);
163164
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
164-
Console.WriteLine("Emails added to Suppression Group:" + groupID.ToString() + ". Press any key to continue.");
165+
Console.WriteLine("Emails added to Suppression Group:" + groupID.ToString() + ".\n\nPress any key to continue.");
165166
Console.ReadKey();
166167

167168
// DELETE EMAILS FROM A SUPPRESSION GROUP
@@ -173,7 +174,7 @@ private static void Suppressions()
173174
HttpResponseMessage responseFinal = client.Suppressions.Get(groupID).Result;
174175
Console.WriteLine(responseFinal.StatusCode);
175176
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
176-
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end.");
177+
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted.\n\nPress any key to end.");
177178
Console.ReadKey();
178179
}
179180

@@ -182,25 +183,25 @@ private static void GlobalSuppressions()
182183
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
183184
var client = new SendGrid.Client(apiKey);
184185

185-
// GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP
186+
// CHECK IF EMAIL IS ON THE GLOBAL SUPPRESSION LIST
186187
var email = "[email protected]";
187188
HttpResponseMessage responseGetUnique = client.GlobalSuppressions.Get(email).Result;
188189
Console.WriteLine(responseGetUnique.StatusCode);
189190
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
190191
Console.WriteLine("Determines if the given email is listed on the Global Suppressions list. Press any key to continue.");
191192
Console.ReadKey();
192193

193-
// ADD EMAILS TO A SUPPRESSION GROUP
194+
// ADD EMAILS TO THE GLOBAL SUPPRESSION LIST
194195
string[] emails = { "[email protected]", "[email protected]" };
195196
HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result;
196197
var rawString = responsePost.Content.ReadAsStringAsync().Result;
197198
dynamic jsonObject = JObject.Parse(rawString);
198199
Console.WriteLine(responsePost.StatusCode);
199200
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
200-
Console.WriteLine("Emails added to Global Suppression Group. Press any key to continue.");
201+
Console.WriteLine("Emails added to Global Suppression Group.\n\nPress any key to continue.");
201202
Console.ReadKey();
202203

203-
// DELETE EMAILS FROM A SUPPRESSION GROUP
204+
// DELETE EMAILS FROM THE GLOBAL SUPPRESSION GROUP
204205
Console.WriteLine("Deleting emails from Global Suppression Group, please wait.");
205206
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete("[email protected]").Result;
206207
Console.WriteLine(responseDelete1.StatusCode);
@@ -212,7 +213,49 @@ private static void GlobalSuppressions()
212213
HttpResponseMessage responseFinal2 = client.GlobalSuppressions.Get("[email protected]").Result;
213214
Console.WriteLine(responseFinal2.StatusCode);
214215
Console.WriteLine(responseFinal2.Content.ReadAsStringAsync().Result);
215-
Console.WriteLine("Emails removed from Global Suppression Group. Press any key to end.");
216+
Console.WriteLine("Emails removed from Global Suppression Group.\n\nPress any key to end.");
217+
Console.ReadKey();
218+
}
219+
220+
private static void GlobalStats()
221+
{
222+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
223+
var client = new SendGrid.Client(apiKey);
224+
225+
// Global Stats provide all of your user’s email statistics for a given date range.
226+
var startDate = "2015-11-01";
227+
HttpResponseMessage response = client.GlobalStats.Get(startDate).Result;
228+
Console.WriteLine(response.StatusCode);
229+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
230+
Console.WriteLine("Display global email stats, with start date " + startDate + "and no end date.\n\nPress any key to continue.");
231+
Console.ReadKey();
232+
233+
var endDate = "2015-12-01";
234+
response = client.GlobalStats.Get(startDate, endDate).Result;
235+
Console.WriteLine(response.StatusCode);
236+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
237+
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + ".\n\nPress any key to continue.");
238+
Console.ReadKey();
239+
240+
var aggregatedBy = "day";
241+
response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
242+
Console.WriteLine(response.StatusCode);
243+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
244+
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
245+
Console.ReadKey();
246+
247+
aggregatedBy = "week";
248+
response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
249+
Console.WriteLine(response.StatusCode);
250+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
251+
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
252+
Console.ReadKey();
253+
254+
aggregatedBy = "month";
255+
response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
256+
Console.WriteLine(response.StatusCode);
257+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
258+
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
216259
Console.ReadKey();
217260
}
218261

SendGrid/SendGrid/Client.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Client
1717
public UnsubscribeGroups UnsubscribeGroups;
1818
public Suppressions Suppressions;
1919
public GlobalSuppressions GlobalSuppressions;
20+
public GlobalStats GlobalStats;
2021
public string Version;
2122
private Uri _baseUri;
2223
private const string MediaType = "application/json";
@@ -39,6 +40,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/")
3940
UnsubscribeGroups = new UnsubscribeGroups(this);
4041
Suppressions = new Suppressions(this);
4142
GlobalSuppressions = new GlobalSuppressions(this);
43+
GlobalStats = new GlobalStats(this);
4244
}
4345

4446
/// <summary>

SendGrid/SendGrid/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("6.3.3")]
36-
[assembly: AssemblyFileVersion("6.3.3")]
35+
[assembly: AssemblyVersion("6.3.4")]
36+
[assembly: AssemblyFileVersion("6.3.4ss")]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using System.Web;
5+
6+
namespace SendGrid.Resources
7+
{
8+
public class GlobalStats
9+
{
10+
private string _endpoint;
11+
private Client _client;
12+
13+
/// <summary>
14+
/// Constructs the SendGrid GlobalStats object.
15+
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html
16+
/// </summary>
17+
/// <param name="client">SendGrid Web API v3 client</param>
18+
/// <param name="endpoint">Resource endpoint, do not prepend slash</param>
19+
public GlobalStats(Client client, string endpoint = "v3/stats")
20+
{
21+
_endpoint = endpoint;
22+
_client = client;
23+
}
24+
25+
/// <summary>
26+
/// https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html
27+
/// </summary>
28+
/// <param name="startDate">The starting date of the statistics to retrieve, formatted as YYYY-MM-DD.</param>
29+
/// <param name="endDate">The end date of the statistics to retrieve, formatted as YYYY-MM-DD. Defaults to today.</param>
30+
/// <param name="aggregatedBy">How to group the statistics, must be day|week|month</param>
31+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html</returns>
32+
public async Task<HttpResponseMessage> Get(string startDate, string endDate = null, string aggregatedBy = null)
33+
{
34+
var query = HttpUtility.ParseQueryString(string.Empty);
35+
query["start_date"] = startDate;
36+
if (endDate != null)
37+
{
38+
query["end_date"] = endDate;
39+
}
40+
if (aggregatedBy != null)
41+
{
42+
query["aggregated_by"] = aggregatedBy;
43+
}
44+
return await _client.Get(_endpoint + "?" + query);
45+
}
46+
47+
}
48+
}

SendGrid/SendGrid/SendGrid.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
5454
<Private>True</Private>
5555
</Reference>
56+
<Reference Include="System.Web" />
5657
<Reference Include="System.Web.Extensions" />
5758
<Reference Include="System.Xml.Linq" />
5859
<Reference Include="System.Data.DataSetExtensions" />
@@ -65,6 +66,7 @@
6566
<Compile Include="Client.cs" />
6667
<Compile Include="Properties\AssemblyInfo.cs" />
6768
<Compile Include="Resources\GlobalSuppressions.cs" />
69+
<Compile Include="Resources\GlobalStats.cs" />
6870
<Compile Include="Resources\Suppressions.cs" />
6971
<Compile Include="Resources\UnsubscribeGroups.cs" />
7072
<Compile Include="Resources\APIKeys.cs" />

SendGrid/SendGridMail/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@
5858
// by using the '*' as shown below:
5959
// [assembly: AssemblyVersion("1.0.*")]
6060

61-
[assembly: AssemblyVersion("6.3.3")]
62-
[assembly: AssemblyFileVersion("6.3.3")]
61+
[assembly: AssemblyVersion("6.3.4")]
62+
[assembly: AssemblyFileVersion("6.3.4")]

SendGrid/UnitTest/UnitTest.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public class GlobalSuppressions
211211
public Client client = new Client(_apiKey, _baseUri);
212212

213213
[Test]
214-
public void SuppressionsIntegrationTest()
214+
public void GlobalSuppressionsIntegrationTest()
215215
{
216216
string email = "[email protected]";
217217

@@ -246,6 +246,37 @@ private void TestDelete(string email)
246246
HttpResponseMessage response = client.GlobalSuppressions.Delete(email).Result;
247247
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
248248
}
249+
}
250+
251+
[TestFixture]
252+
public class GlobalStats
253+
{
254+
static string _baseUri = "https://api.sendgrid.com/";
255+
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
256+
public Client client = new Client(_apiKey, _baseUri);
257+
258+
[Test]
259+
public void GlobalStatsIntegrationTest()
260+
{
261+
string startDate = "2015-11-01";
262+
string endDate = "2015-12-01";
263+
string aggregatedBy = "day";
264+
TestGet(startDate);
265+
TestGet(startDate, endDate);
266+
TestGet(startDate, endDate, aggregatedBy);
267+
aggregatedBy = "week";
268+
TestGet(startDate, endDate, aggregatedBy);
269+
aggregatedBy = "month";
270+
TestGet(startDate, endDate, aggregatedBy);
271+
}
249272

273+
private void TestGet(string startDate, string endDate=null, string aggregatedBy=null)
274+
{
275+
HttpResponseMessage response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
276+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
277+
string rawString = response.Content.ReadAsStringAsync().Result;
278+
dynamic jsonObject = JsonConvert.DeserializeObject(rawString);
279+
Assert.IsNotNull(jsonObject);
280+
}
250281
}
251282
}

0 commit comments

Comments
 (0)