diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2015-11-16 19:41:55 -0800 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2015-11-16 19:41:55 -0800 |
commit | f7610d321d601d5a6b7dabe8af0cfa30bad39fe5 (patch) | |
tree | 87ebccec8cce7d677f4d2c4e272e64bc32702479 | |
parent | e93e813e701498841e687ed9eb354b37dc7da7eb (diff) | |
download | sendgrid-csharp-f7610d321d601d5a6b7dabe8af0cfa30bad39fe5.zip sendgrid-csharp-f7610d321d601d5a6b7dabe8af0cfa30bad39fe5.tar.gz sendgrid-csharp-f7610d321d601d5a6b7dabe8af0cfa30bad39fe5.tar.bz2 |
Re-factoring based on @deckarep's feedback
- Favor using var instead of string declarations for type inference.
- Use a better type other than object
-rw-r--r-- | SendGrid/Example/Program.cs | 19 | ||||
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 18 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/APIKeys.cs | 11 |
3 files changed, 21 insertions, 27 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 6953c27..c0f4295 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -12,9 +12,9 @@ namespace Example private static void Main()
{
// Test sending email
- string to = "example@example.com";
- string from = "example@example.com";
- string fromName = "Jane Doe";
+ var to = "example@example.com";
+ var from = "example@example.com";
+ var fromName = "Jane Doe";
SendEmail(to, from, fromName);
// Test viewing, creating, modifying and deleting API keys through our v3 Web API
ApiKeys();
@@ -23,8 +23,8 @@ namespace Example private static void SendAsync(SendGrid.SendGridMessage message)
{
// Create credentials, specifying your user Name and password.
- string username = Environment.GetEnvironmentVariable("SENDGRID_USERNAME");
- string password = Environment.GetEnvironmentVariable("SENDGRID_PASSWORD");
+ var username = Environment.GetEnvironmentVariable("SENDGRID_USERNAME");
+ var password = Environment.GetEnvironmentVariable("SENDGRID_PASSWORD");
//string apikey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
var credentials = new NetworkCredential(username, password);
@@ -68,7 +68,6 @@ namespace Example {
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
var client = new SendGrid.Client(apiKey);
- string _api_key_id;
// GET API KEYS
HttpResponseMessage responseGet = client.ApiKeys.Get();
@@ -79,16 +78,16 @@ namespace Example // POST API KEYS
HttpResponseMessage responsePost = client.ApiKeys.Post("CSharpTestKey");
- string rawString = responsePost.Content.ReadAsStringAsync().Result;
+ var rawString = responsePost.Content.ReadAsStringAsync().Result;
dynamic jsonObject = JObject.Parse(rawString);
- _api_key_id = jsonObject.api_key_id.ToString();
+ 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.ReadKey();
// PATCH API KEYS
- HttpResponseMessage responsePatch = client.ApiKeys.Patch(_api_key_id, "CSharpTestKeyPatched");
+ HttpResponseMessage responsePatch = client.ApiKeys.Patch(apiKeyId, "CSharpTestKeyPatched");
Console.WriteLine(responsePatch.StatusCode);
Console.WriteLine(responsePatch.Content.ReadAsStringAsync().Result);
Console.WriteLine("API Key patched. Press any key to continue.");
@@ -96,7 +95,7 @@ namespace Example // DELETE API KEYS
Console.WriteLine("Deleting API Key, please wait.");
- client.ApiKeys.Delete(_api_key_id);
+ client.ApiKeys.Delete(apiKeyId);
HttpResponseMessage responseFinal = client.ApiKeys.Get();
Console.WriteLine(responseFinal.StatusCode);
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index 1cf483d..e642485 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -5,8 +5,8 @@ using System.Reflection; using System.Threading.Tasks; using System.Text; using SendGrid.Resources; -using System.Web.Script.Serialization; using System.Net; +using Newtonsoft.Json.Linq; namespace SendGrid { @@ -33,9 +33,9 @@ namespace SendGrid /// </summary> /// <param name="method">HTTP verb, case-insensitive</param> /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <param name="data">An object representing the resource's data</param> + /// <param name="data">An JObject representing the resource's data</param> /// <returns>An asyncronous task</returns> - private async Task<HttpResponseMessage> RequestAsync(string method, string endpoint, Object data) + private async Task<HttpResponseMessage> RequestAsync(string method, string endpoint, JObject data) { using (var client = new HttpClient()) { @@ -92,14 +92,13 @@ namespace SendGrid /// <returns>The resulting message from the API call</returns> public HttpResponseMessage Get(string endpoint) { - HttpResponseMessage response = new HttpResponseMessage(); return RequestAsync("GET", endpoint, null).Result; } /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <param name="data">An object representing the resource's data</param> + /// <param name="data">An JObject representing the resource's data</param> /// <returns>The resulting message from the API call</returns> - public HttpResponseMessage Post(string endpoint, object data) + public HttpResponseMessage Post(string endpoint, JObject data) { return RequestAsync("POST", endpoint, data).Result; } @@ -112,12 +111,11 @@ namespace SendGrid } /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <param name="data">An object representing the resource's data</param> + /// <param name="data">An JObject representing the resource's data</param> /// <returns>The resulting message from the API call</returns> - public HttpResponseMessage Patch(string endpoint, object data) + public HttpResponseMessage Patch(string endpoint, JObject data) { - var json = new JavaScriptSerializer().Serialize(data); - return RequestAsync("PATCH", endpoint, json).Result; + return RequestAsync("PATCH", endpoint, data).Result; } } } diff --git a/SendGrid/SendGrid/Resources/APIKeys.cs b/SendGrid/SendGrid/Resources/APIKeys.cs index 4644d6f..a3d8d35 100644 --- a/SendGrid/SendGrid/Resources/APIKeys.cs +++ b/SendGrid/SendGrid/Resources/APIKeys.cs @@ -1,12 +1,9 @@ using System.Net.Http; +using System.Runtime.InteropServices; +using Newtonsoft.Json.Linq; namespace SendGrid.Resources { - public class APIKeysData - { - public string name { get; set; } - } - public class APIKeys { private string _endpoint; @@ -40,7 +37,7 @@ namespace SendGrid.Resources /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html</returns> public HttpResponseMessage Post(string apiKeyName) { - var data = new APIKeysData() {name = apiKeyName}; + var data = new JObject {{"name", apiKeyName}}; return _client.Post(_endpoint, data); } @@ -62,7 +59,7 @@ namespace SendGrid.Resources /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html</returns> public HttpResponseMessage Patch(string apiKeyId, string apiKeyName) { - var data = new APIKeysData() { name = apiKeyName }; + var data = new JObject { { "name", apiKeyName } }; return _client.Patch(_endpoint + "/" + apiKeyId, data); } |