diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2015-11-09 23:42:16 -0800 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2015-11-09 23:42:16 -0800 |
commit | 34ef7589b45086844d6235bae73fe82c632a5dfd (patch) | |
tree | 5dedcaabab978e03308bdb0788590c243d971012 | |
parent | 5f50a15f8f4b75c4d555015a3727c0a12d22595a (diff) | |
download | sendgrid-csharp-34ef7589b45086844d6235bae73fe82c632a5dfd.zip sendgrid-csharp-34ef7589b45086844d6235bae73fe82c632a5dfd.tar.gz sendgrid-csharp-34ef7589b45086844d6235bae73fe82c632a5dfd.tar.bz2 |
Add comments
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 49 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/APIKeys.cs | 55 | ||||
-rw-r--r-- | SendGrid/Tests/Tests.csproj | 4 |
3 files changed, 78 insertions, 30 deletions
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index 85c89b9..ac93f69 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -13,30 +13,41 @@ namespace SendGrid public class Client { private HttpResponseMessage _response = new HttpResponseMessage(); - private String _apiKey; - public Uri BaseUri; + private string _apiKey; + private Uri _baseUri; public APIKeys ApiKeys; - public Client(String api_key) + /// <summary> + /// Create a client that connects to the SendGrid Web API + /// </summary> + /// <param name="apiKey">Your SendGrid API Key</param> + /// <param name="baseUri">Base SendGrid API Uri</param> + public Client(string apiKey, string baseUri = "https://api.sendgrid.com/") { - BaseUri = new Uri("https://api.sendgrid.com/"); - _apiKey = api_key; + _baseUri = new Uri(baseUri); + _apiKey = apiKey; ApiKeys = new APIKeys(this); } - async Task RequestAsync(String method, String endpoint, Object data) + /// <summary> + /// Create a client that connects to the SendGrid Web API + /// </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> + /// <returns>An asyncronous task</returns> + private async Task RequestAsync(string method, string endpoint, Object data) { using (var client = new HttpClient()) { try { - client.BaseAddress = BaseUri; + client.BaseAddress = _baseUri; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey); var version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp"); - StringContent content; switch (method.ToLower()) { @@ -47,8 +58,8 @@ namespace SendGrid _response = await client.PostAsJsonAsync(endpoint, data); break; case "patch": - endpoint = BaseUri + endpoint.Remove(0, 1); - content = new StringContent(data.ToString(), Encoding.UTF8, "application/json"); + endpoint = _baseUri + endpoint; + StringContent content = new StringContent(data.ToString(), Encoding.UTF8, "application/json"); HttpRequestMessage request = new HttpRequestMessage { Method = new HttpMethod("PATCH"), @@ -79,25 +90,35 @@ namespace SendGrid } } - public HttpResponseMessage Get(String endpoint) + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + /// <returns>The resulting message from the API call</returns> + public HttpResponseMessage Get(string endpoint) { RequestAsync("GET", endpoint, null).Wait(); return _response; } - public HttpResponseMessage Post(String endpoint, object data) + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + /// <param name="data">An object representing the resource's data</param> + /// <returns>The resulting message from the API call</returns> + public HttpResponseMessage Post(string endpoint, object data) { RequestAsync("POST", endpoint, data).Wait(); return _response; } - public HttpResponseMessage Delete(String endpoint) + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + /// <returns>The resulting message from the API call</returns> + public HttpResponseMessage Delete(string endpoint) { RequestAsync("DELETE", endpoint, null).Wait(); return _response; } - public HttpResponseMessage Patch(String endpoint, object data) + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + /// <param name="data">An object representing the resource's data</param> + /// <returns>The resulting message from the API call</returns> + public HttpResponseMessage Patch(string endpoint, object data) { var json = new JavaScriptSerializer().Serialize(data); RequestAsync("PATCH", endpoint, json).Wait(); diff --git a/SendGrid/SendGrid/Resources/APIKeys.cs b/SendGrid/SendGrid/Resources/APIKeys.cs index 23b06cb..2302de2 100644 --- a/SendGrid/SendGrid/Resources/APIKeys.cs +++ b/SendGrid/SendGrid/Resources/APIKeys.cs @@ -1,9 +1,7 @@ -using System; -using System.Net.Http; +using System.Net.Http; namespace SendGrid.Resources { - public class APIKeysData { public string name { get; set; } @@ -12,36 +10,61 @@ namespace SendGrid.Resources public class APIKeys { public string Name { get; set; } - public string Endpoint { get; set; } + private string _endpoint; private Client _client; - public APIKeys(Client client) + /// <summary> + /// Constructs the SendGrid APIKeys object. + /// See https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html + /// </summary> + /// <param name="client">SendGrid Web API v3 client</param> + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + public APIKeys(Client client, string endpoint = "v3/api_keys") { - Endpoint = "/v3/api_keys"; + _endpoint = endpoint; _client = client; - } + /// <summary> + /// Get a list of active API Keys + /// </summary> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html</returns> public HttpResponseMessage Get() { - return _client.Get(Endpoint); + return _client.Get(_endpoint); } - public HttpResponseMessage Post(String Name) + /// <summary> + /// Create a new API key + /// </summary> + /// <param name="apiKeyName">Name of the new API Key</param> + /// <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 = Name}; - return _client.Post(Endpoint, data); + var data = new APIKeysData() {name = apiKeyName}; + return _client.Post(_endpoint, data); } - public HttpResponseMessage Delete(String ApiKeyID) + /// <summary> + /// Delete a API key + /// </summary> + /// <param name="apiKeyId">ID of the API Key to delete</param> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html</returns> + public HttpResponseMessage Delete(string apiKeyId) { - return _client.Delete(Endpoint + "/" + ApiKeyID); + return _client.Delete(_endpoint + "/" + apiKeyId); } - public HttpResponseMessage Patch(String ApiKeyID, String Name) + /// <summary> + /// Delete a API key + /// </summary> + /// <param name="apiKeyId">ID of the API Key to rename</param> + /// <param name="apiKeyName">New API Key name</param> + /// <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 = Name }; - return _client.Patch(Endpoint + "/" + ApiKeyID, data); + var data = new APIKeysData() { name = apiKeyName }; + return _client.Patch(_endpoint + "/" + apiKeyId, data); } } diff --git a/SendGrid/Tests/Tests.csproj b/SendGrid/Tests/Tests.csproj index 819c0d8..37bacce 100644 --- a/SendGrid/Tests/Tests.csproj +++ b/SendGrid/Tests/Tests.csproj @@ -84,6 +84,10 @@ <Project>{3C687BEF-FF50-44AD-8315-2D4237281AF8}</Project>
<Name>Mail</Name>
</ProjectReference>
+ <ProjectReference Include="..\SendGrid\SendGrid.csproj">
+ <Project>{1c318867-440b-4eb9-99e3-c0cc2c556962}</Project>
+ <Name>SendGrid</Name>
+ </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|