Skip to content

Commit

Permalink
Merge pull request #170 from sendgrid/stats
Browse files Browse the repository at this point in the history
Stats
  • Loading branch information
thinkingserious committed Dec 15, 2015
2 parents 8c6b06f + 6b584b6 commit b27983a
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [6.3.4] - 2015-12-15
###Added
- Implemented the global stats /asm/stats endpoint [GET]

## [6.3.3] - 2015-12-14
###Added
- Implemented the global suppressions /asm/suppressions/global endpoint [GET, POST, DELETE]
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ string email = "example@example.com";
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete(email).Result;
```

## Global Stats ##

Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html) for further details.

Global Stats provide all of your user’s email statistics for a given date range. [GET]

```csharp
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
var client = new SendGrid.Client(apiKey);
var startDate = "2015-11-01"; // required
var endDate = "2015-12-01";
var aggregatedBy = "day"; // "week" or "month" are also options
// Leave off .Result for an asyncronous call
HttpResponseMessage responseGet = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
```

#How to: Testing

* Load the solution (We have tested using the Visual Studio Community Edition)
Expand Down
75 changes: 59 additions & 16 deletions SendGrid/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private static void Main()
UnsubscribeGroups();
Suppressions();
GlobalSuppressions();
GlobalStats();
}

private static void SendAsync(SendGrid.SendGridMessage message)
Expand All @@ -33,13 +34,13 @@ private static void SendAsync(SendGrid.SendGridMessage message)
{
transportWeb.DeliverAsync(message).Wait();
Console.WriteLine("Email sent to " + message.To.GetValue(0));
Console.WriteLine("Press any key to continue.");
Console.WriteLine("\n\nPress any key to continue.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Press any key to continue.");
Console.WriteLine("\n\nPress any key to continue.");
Console.ReadKey();
}
}
Expand Down Expand Up @@ -69,7 +70,7 @@ private static void ApiKeys()
HttpResponseMessage responseGet = client.ApiKeys.Get().Result;
Console.WriteLine(responseGet.StatusCode);
Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result);
Console.WriteLine("These are your current API Keys. Press any key to continue.");
Console.WriteLine("These are your current API Keys.\n\nPress any key to continue.");
Console.ReadKey();

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

// PATCH API KEYS
HttpResponseMessage responsePatch = client.ApiKeys.Patch(apiKeyId, "CSharpTestKeyPatched").Result;
Console.WriteLine(responsePatch.StatusCode);
Console.WriteLine(responsePatch.Content.ReadAsStringAsync().Result);
Console.WriteLine("API Key patched. Press any key to continue.");
Console.WriteLine("API Key patched.\n\nPress any key to continue.");
Console.ReadKey();

// DELETE API KEYS
Expand All @@ -96,7 +97,7 @@ private static void ApiKeys()
HttpResponseMessage responseFinal = client.ApiKeys.Get().Result;
Console.WriteLine(responseFinal.StatusCode);
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
Console.WriteLine("API Key Deleted, press any key to end.");
Console.WriteLine("API Key Deleted.\n\nPress any key to end.");
Console.ReadKey();
}

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

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

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

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

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

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

// GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP
// CHECK IF EMAIL IS ON THE GLOBAL SUPPRESSION LIST
var email = "elmer.thomas+test_global@gmail.com";
HttpResponseMessage responseGetUnique = client.GlobalSuppressions.Get(email).Result;
Console.WriteLine(responseGetUnique.StatusCode);
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
Console.WriteLine("Determines if the given email is listed on the Global Suppressions list. Press any key to continue.");
Console.ReadKey();

// ADD EMAILS TO A SUPPRESSION GROUP
// ADD EMAILS TO THE GLOBAL SUPPRESSION LIST
string[] emails = { "example@example.com", "example2@example.com" };
HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result;
var rawString = responsePost.Content.ReadAsStringAsync().Result;
dynamic jsonObject = JObject.Parse(rawString);
Console.WriteLine(responsePost.StatusCode);
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
Console.WriteLine("Emails added to Global Suppression Group. Press any key to continue.");
Console.WriteLine("Emails added to Global Suppression Group.\n\nPress any key to continue.");
Console.ReadKey();

// DELETE EMAILS FROM A SUPPRESSION GROUP
// DELETE EMAILS FROM THE GLOBAL SUPPRESSION GROUP
Console.WriteLine("Deleting emails from Global Suppression Group, please wait.");
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete("example@example.com").Result;
Console.WriteLine(responseDelete1.StatusCode);
Expand All @@ -212,7 +213,49 @@ private static void GlobalSuppressions()
HttpResponseMessage responseFinal2 = client.GlobalSuppressions.Get("example2@example.com").Result;
Console.WriteLine(responseFinal2.StatusCode);
Console.WriteLine(responseFinal2.Content.ReadAsStringAsync().Result);
Console.WriteLine("Emails removed from Global Suppression Group. Press any key to end.");
Console.WriteLine("Emails removed from Global Suppression Group.\n\nPress any key to end.");
Console.ReadKey();
}

private static void GlobalStats()
{
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
var client = new SendGrid.Client(apiKey);

// Global Stats provide all of your user’s email statistics for a given date range.
var startDate = "2015-11-01";
HttpResponseMessage response = client.GlobalStats.Get(startDate).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine("Display global email stats, with start date " + startDate + "and no end date.\n\nPress any key to continue.");
Console.ReadKey();

var endDate = "2015-12-01";
response = client.GlobalStats.Get(startDate, endDate).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + ".\n\nPress any key to continue.");
Console.ReadKey();

var aggregatedBy = "day";
response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
Console.ReadKey();

aggregatedBy = "week";
response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
Console.ReadKey();

aggregatedBy = "month";
response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
Console.ReadKey();
}

Expand Down
2 changes: 2 additions & 0 deletions SendGrid/SendGrid/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Client
public UnsubscribeGroups UnsubscribeGroups;
public Suppressions Suppressions;
public GlobalSuppressions GlobalSuppressions;
public GlobalStats GlobalStats;
public string Version;
private Uri _baseUri;
private const string MediaType = "application/json";
Expand All @@ -39,6 +40,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/")
UnsubscribeGroups = new UnsubscribeGroups(this);
Suppressions = new Suppressions(this);
GlobalSuppressions = new GlobalSuppressions(this);
GlobalStats = new GlobalStats(this);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions SendGrid/SendGrid/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("6.3.3")]
[assembly: AssemblyFileVersion("6.3.3")]
[assembly: AssemblyVersion("6.3.4")]
[assembly: AssemblyFileVersion("6.3.4ss")]
48 changes: 48 additions & 0 deletions SendGrid/SendGrid/Resources/GlobalStats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

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

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

/// <summary>
/// https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html
/// </summary>
/// <param name="startDate">The starting date of the statistics to retrieve, formatted as YYYY-MM-DD.</param>
/// <param name="endDate">The end date of the statistics to retrieve, formatted as YYYY-MM-DD. Defaults to today.</param>
/// <param name="aggregatedBy">How to group the statistics, must be day|week|month</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html</returns>
public async Task<HttpResponseMessage> Get(string startDate, string endDate = null, string aggregatedBy = null)
{
var query = HttpUtility.ParseQueryString(string.Empty);
query["start_date"] = startDate;
if (endDate != null)
{
query["end_date"] = endDate;
}
if (aggregatedBy != null)
{
query["aggregated_by"] = aggregatedBy;
}
return await _client.Get(_endpoint + "?" + query);
}

}
}
2 changes: 2 additions & 0 deletions SendGrid/SendGrid/SendGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -65,6 +66,7 @@
<Compile Include="Client.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\GlobalSuppressions.cs" />
<Compile Include="Resources\GlobalStats.cs" />
<Compile Include="Resources\Suppressions.cs" />
<Compile Include="Resources\UnsubscribeGroups.cs" />
<Compile Include="Resources\APIKeys.cs" />
Expand Down
4 changes: 2 additions & 2 deletions SendGrid/SendGridMail/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("6.3.3")]
[assembly: AssemblyFileVersion("6.3.3")]
[assembly: AssemblyVersion("6.3.4")]
[assembly: AssemblyFileVersion("6.3.4")]
33 changes: 32 additions & 1 deletion SendGrid/UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public class GlobalSuppressions
public Client client = new Client(_apiKey, _baseUri);

[Test]
public void SuppressionsIntegrationTest()
public void GlobalSuppressionsIntegrationTest()
{
string email = "example3@example.com";

Expand Down Expand Up @@ -246,6 +246,37 @@ private void TestDelete(string email)
HttpResponseMessage response = client.GlobalSuppressions.Delete(email).Result;
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
}
}

[TestFixture]
public class GlobalStats
{
static string _baseUri = "https://api.sendgrid.com/";
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
public Client client = new Client(_apiKey, _baseUri);

[Test]
public void GlobalStatsIntegrationTest()
{
string startDate = "2015-11-01";
string endDate = "2015-12-01";
string aggregatedBy = "day";
TestGet(startDate);
TestGet(startDate, endDate);
TestGet(startDate, endDate, aggregatedBy);
aggregatedBy = "week";
TestGet(startDate, endDate, aggregatedBy);
aggregatedBy = "month";
TestGet(startDate, endDate, aggregatedBy);
}

private void TestGet(string startDate, string endDate=null, string aggregatedBy=null)
{
HttpResponseMessage response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
string rawString = response.Content.ReadAsStringAsync().Result;
dynamic jsonObject = JsonConvert.DeserializeObject(rawString);
Assert.IsNotNull(jsonObject);
}
}
}

0 comments on commit b27983a

Please sign in to comment.