diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2015-12-14 10:55:32 -0800 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2015-12-14 10:55:32 -0800 |
commit | d2ca4a5032484335b866b25401f167cdf0bef101 (patch) | |
tree | 51c8b3d22470df62a04106307d2c8d273a1005c3 | |
parent | 8c6b06f0d70a7004b33543299fa9fbc2d14160f3 (diff) | |
download | sendgrid-csharp-d2ca4a5032484335b866b25401f167cdf0bef101.zip sendgrid-csharp-d2ca4a5032484335b866b25401f167cdf0bef101.tar.gz sendgrid-csharp-d2ca4a5032484335b866b25401f167cdf0bef101.tar.bz2 |
Implementing Stats [GET]
-rw-r--r-- | SendGrid/Example/Program.cs | 25 | ||||
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 2 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/GlobalStats.cs | 48 | ||||
-rw-r--r-- | SendGrid/SendGrid/SendGrid.csproj | 2 |
4 files changed, 74 insertions, 3 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 041e6dd..e4bb41a 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -10,6 +10,7 @@ namespace Example {
private static void Main()
{
+ /*
// Test sending email
var to = "example@example.com";
var from = "example@example.com";
@@ -20,6 +21,8 @@ namespace Example UnsubscribeGroups();
Suppressions();
GlobalSuppressions();
+ */
+ GlobalStats();
}
private static void SendAsync(SendGrid.SendGridMessage message)
@@ -182,7 +185,7 @@ namespace Example 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);
@@ -190,7 +193,7 @@ namespace Example 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;
@@ -200,7 +203,7 @@ namespace Example Console.WriteLine("Emails added to Global Suppression Group. Press 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);
@@ -216,5 +219,21 @@ namespace Example 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-12-01";
+ HttpResponseMessage responseGetUnique = client.GlobalStats.Get(startDate).Result;
+ Console.WriteLine(responseGetUnique.StatusCode);
+ Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
+ Console.WriteLine("Display global email stats, with start date " + startDate + "and no end date. Press any key to continue.");
+ Console.ReadKey();
+
+ // TODO: Cover case for the remaining two parameters
+ }
+
}
}
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index 31d49b5..6e86be1 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -17,6 +17,7 @@ namespace SendGrid public UnsubscribeGroups UnsubscribeGroups; public Suppressions Suppressions; public GlobalSuppressions GlobalSuppressions; + public GlobalStats GlobalStats; public string Version; private Uri _baseUri; private const string MediaType = "application/json"; @@ -39,6 +40,7 @@ namespace SendGrid UnsubscribeGroups = new UnsubscribeGroups(this); Suppressions = new Suppressions(this); GlobalSuppressions = new GlobalSuppressions(this); + GlobalStats = new GlobalStats(this); } /// <summary> diff --git a/SendGrid/SendGrid/Resources/GlobalStats.cs b/SendGrid/SendGrid/Resources/GlobalStats.cs new file mode 100644 index 0000000..73f0b33 --- /dev/null +++ b/SendGrid/SendGrid/Resources/GlobalStats.cs @@ -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); + } + + } +}
\ No newline at end of file diff --git a/SendGrid/SendGrid/SendGrid.csproj b/SendGrid/SendGrid/SendGrid.csproj index 1ba1fb7..2f5bcf6 100644 --- a/SendGrid/SendGrid/SendGrid.csproj +++ b/SendGrid/SendGrid/SendGrid.csproj @@ -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" /> @@ -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" /> |