diff options
Diffstat (limited to 'SendGrid/SendGrid/Client.cs')
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 117 |
1 files changed, 10 insertions, 107 deletions
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index 6e86be1..388cc3f 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -1,26 +1,16 @@ using System; -using System.Net.Http; -using System.Net.Http.Headers; using System.Reflection; -using System.Threading.Tasks; -using System.Text; -using SendGrid.Resources; -using System.Net; -using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using SendGrid.CSharp.HTTP.Client; namespace SendGrid { - public class Client + public class SendGridAPIClient { private string _apiKey; - public APIKeys ApiKeys; - public UnsubscribeGroups UnsubscribeGroups; - public Suppressions Suppressions; - public GlobalSuppressions GlobalSuppressions; - public GlobalStats GlobalStats; public string Version; + public dynamic client; private Uri _baseUri; - private const string MediaType = "application/json"; private enum Methods { GET, POST, PATCH, DELETE @@ -31,103 +21,16 @@ namespace SendGrid /// </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/") + public SendGridAPIClient(string apiKey, String baseUri = "https://api.sendgrid.com", String version = "v3") { _baseUri = new Uri(baseUri); _apiKey = apiKey; Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); - ApiKeys = new APIKeys(this); - UnsubscribeGroups = new UnsubscribeGroups(this); - Suppressions = new Suppressions(this); - GlobalSuppressions = new GlobalSuppressions(this); - GlobalStats = new GlobalStats(this); - } - - /// <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 JObject representing the resource's data</param> - /// <returns>An asyncronous task</returns> - private async Task<HttpResponseMessage> RequestAsync(Methods method, string endpoint, JObject data) - { - using (var client = new HttpClient()) - { - try - { - client.BaseAddress = _baseUri; - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaType)); - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey); - client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + Version + ";csharp"); - - switch (method) - { - case Methods.GET: - return await client.GetAsync(endpoint); - case Methods.POST: - return await client.PostAsJsonAsync(endpoint, data); - case Methods.PATCH: - endpoint = _baseUri + endpoint; - StringContent content = new StringContent(data.ToString(), Encoding.UTF8, MediaType); - HttpRequestMessage request = new HttpRequestMessage - { - Method = new HttpMethod("PATCH"), - RequestUri = new Uri(endpoint), - Content = content - }; - return await client.SendAsync(request); - case Methods.DELETE: - return await client.DeleteAsync(endpoint); - default: - HttpResponseMessage response = new HttpResponseMessage(); - response.StatusCode = HttpStatusCode.MethodNotAllowed; - var message = "{\"errors\":[{\"message\":\"Bad method call, supported methods are GET, POST, PATCH and DELETE\"}]}"; - response.Content = new StringContent(message); - return response; - } - } - catch (Exception ex) - { - HttpResponseMessage response = new HttpResponseMessage(); - string message; - message = (ex is HttpRequestException) ? ".NET HttpRequestException" : ".NET Exception"; - message = message + ", raw message: \n\n"; - response.Content = new StringContent(message + ex.Message); - return response; - } - } - } - - /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <returns>The resulting message from the API call</returns> - public async Task<HttpResponseMessage> Get(string endpoint) - { - return await RequestAsync(Methods.GET, endpoint, null); - } - - /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <param name="data">An JObject representing the resource's data</param> - /// <returns>The resulting message from the API call</returns> - public async Task<HttpResponseMessage> Post(string endpoint, JObject data) - { - return await RequestAsync(Methods.POST, endpoint, data); - } - - /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <returns>The resulting message from the API call</returns> - public async Task<HttpResponseMessage> Delete(string endpoint) - { - return await RequestAsync(Methods.DELETE, endpoint, null); - } - - /// <param name="endpoint">Resource endpoint, do not prepend slash</param> - /// <param name="data">An JObject representing the resource's data</param> - /// <returns>The resulting message from the API call</returns> - public async Task<HttpResponseMessage> Patch(string endpoint, JObject data) - { - return await RequestAsync(Methods.PATCH, endpoint, data); + Dictionary<String, String> requestHeaders = new Dictionary<String, String>(); + requestHeaders.Add("Authorization", "Bearer " + apiKey); + requestHeaders.Add("Content-Type", "application/json"); + requestHeaders.Add("User-Agent", "sendgrid/" + Version + " csharp"); + client = new Client(host: baseUri, requestHeaders: requestHeaders, version: version); } } } |