diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2015-11-09 21:06:15 -0800 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2015-11-09 21:06:15 -0800 |
commit | 5f50a15f8f4b75c4d555015a3727c0a12d22595a (patch) | |
tree | df031221230b6b9d3ff560dccf8aa216086c0e12 | |
parent | 526eb12ea6a33344ec48b93e7bae3895ecc12e85 (diff) | |
download | sendgrid-csharp-5f50a15f8f4b75c4d555015a3727c0a12d22595a.zip sendgrid-csharp-5f50a15f8f4b75c4d555015a3727c0a12d22595a.tar.gz sendgrid-csharp-5f50a15f8f4b75c4d555015a3727c0a12d22595a.tar.bz2 |
Implemented return values for exceptions
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 34 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/APIKeys.cs | 11 |
2 files changed, 13 insertions, 32 deletions
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index 0b27872..85c89b9 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -6,12 +6,13 @@ using System.Threading.Tasks; using System.Text; using SendGrid.Resources; using System.Web.Script.Serialization; +using System.Net; namespace SendGrid { public class Client { - private HttpResponseMessage _response; + private HttpResponseMessage _response = new HttpResponseMessage(); private String _apiKey; public Uri BaseUri; public APIKeys ApiKeys; @@ -43,7 +44,6 @@ namespace SendGrid _response = await client.GetAsync(endpoint); break; case "post": - content = new StringContent(data.ToString(), Encoding.UTF8, "application/json"); _response = await client.PostAsJsonAsync(endpoint, data); break; case "patch": @@ -61,38 +61,20 @@ namespace SendGrid _response = await client.DeleteAsync(endpoint); break; default: - _response = null; + _response.StatusCode = HttpStatusCode.MethodNotAllowed; + _response.Content = new StringContent("Bad method call: " + method); break; } - - if (_response == null) - { - //Console.WriteLine("Bad method call: " + method); - //Console.ReadKey(); - } - string responseBodyAsText = await _response.Content.ReadAsStringAsync(); - responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); - if (_response.IsSuccessStatusCode) - { - //Console.WriteLine(_response.StatusCode); - //Console.WriteLine(responseBodyAsText); - //Console.ReadKey(); - } - else - { - //Console.WriteLine(responseBodyAsText); - //Console.ReadKey(); - } } catch (HttpRequestException hre) { - //Console.WriteLine(hre.ToString()); - //Console.ReadKey(); + _response.StatusCode = HttpStatusCode.InternalServerError; + _response.Content = new StringContent(hre.ToString()); } catch (Exception ex) { - //Console.WriteLine(ex.ToString()); - //Console.ReadKey(); + _response.StatusCode = HttpStatusCode.InternalServerError; + _response.Content = new StringContent(ex.ToString()); } } } diff --git a/SendGrid/SendGrid/Resources/APIKeys.cs b/SendGrid/SendGrid/Resources/APIKeys.cs index ed10344..23b06cb 100644 --- a/SendGrid/SendGrid/Resources/APIKeys.cs +++ b/SendGrid/SendGrid/Resources/APIKeys.cs @@ -1,6 +1,5 @@ using System; using System.Net.Http; -using System.Web.Script.Serialization; namespace SendGrid.Resources { @@ -30,8 +29,8 @@ namespace SendGrid.Resources public HttpResponseMessage Post(String Name) { - var Data = new APIKeysData() {name = Name}; - return _client.Post(Endpoint, Data); + var data = new APIKeysData() {name = Name}; + return _client.Post(Endpoint, data); } public HttpResponseMessage Delete(String ApiKeyID) @@ -41,9 +40,9 @@ namespace SendGrid.Resources public HttpResponseMessage Patch(String ApiKeyID, String Name) { - var Data = new APIKeysData() { name = Name }; - return _client.Patch(Endpoint + "/" + ApiKeyID, Data); + var data = new APIKeysData() { name = Name }; + return _client.Patch(Endpoint + "/" + ApiKeyID, data); } } -} +}
\ No newline at end of file |